Ad
Interview Questions
Algorithms
Logic
Code
Diff
  • def swapPixel(array, index1, index2):
        temp = getPixel(array, index1)
        putPixel(array, index1, getPixel(array, index2))
        putPixel(array, index2, temp)
    
    def horizontalFlipPair(array, index):
        return (int((len(array)/2)+((len(array)/2)-int(index/len(array[0]))))-1)*len(array[0])+index%len(array[0])
    
    def simpleFlipPair(array, index):
        return int(2*(((len(array)-1)/2-int(index/len(array[0])))*len(array[0]))+index)
    
    def flipImage(array):
        for i in range(int(len(array)/2*len(array[0]))):
            swapPixel(array, i, simpleFlipPair(array, i))
    
        
        
    • def swapPixel(array, index1, index2):
    • temp = getPixel(array, index1)
    • putPixel(array, index1, getPixel(array, index2))
    • putPixel(array, index2, temp)
    • def horizontalFlipPair(array, index):
    • return (int((len(array)/2)+((len(array)/2)-int(index/len(array[0]))))-1)*len(array[0])+index%len(array[0])
    • def flipImage(array):
    • for i in range(int(len(array)*len(array[0])/2)):
    • swapPixel(array, i, horizontalFlipPair(array, i))
    • def simpleFlipPair(array, index):
    • w = len(array[0])
    • m = (len(array)-1)/2
    • l = int(index/w)
    • a = (m-l)*w
    • return int(2*(a)+index)
    • return int(2*(((len(array)-1)/2-int(index/len(array[0])))*len(array[0]))+index)
    • def flipImage(array):
    • for i in range(int(len(array)/2*len(array[0]))):
    • swapPixel(array, i, simpleFlipPair(array, i))
Interview Questions
Algorithms
Logic

Flip an image vertically (i.e. top becomes the bottom and vice versa) where you are only able to get the pixel data based on it's index within the image. For example, the 4th index on a 3 x 3 image would be the center square.
012
345
678

You can only access the pixel data using the getPixel(index) function.
You can also modify the pixel Data within the Array using the putPixel(index, value) function.
You do not neccesarily need to flip the image in place to perform the flip, but it is required for the array object to be overwritten with the new array once it has completed.
An example Array has been provided:
array=[[0,1,2,3],[4,5,6,7],[8,9,10,11],[12,13,14,15],[16,17,18,19]]
This should return:
[[16, 17, 18, 19], [12, 13, 14, 15], [8, 9, 10, 11], [4, 5, 6, 7], [0, 1, 2, 3]]

A maximum of two for loops are allowed to be used, and bonus points are awarded for simplicity and efficiency.

def swapPixel(array, index1, index2):
    temp = getPixel(array, index1)
    putPixel(array, index1, getPixel(array, index2))
    putPixel(array, index2, temp)

def horizontalFlipPair(array, index):
    return (int((len(array)/2)+((len(array)/2)-int(index/len(array[0]))))-1)*len(array[0])+index%len(array[0])

def flipImage(array):
    for i in range(int(len(array)*len(array[0])/2)):
        swapPixel(array, i, horizontalFlipPair(array, i))

def simpleFlipPair(array, index):
    w = len(array[0])
    m = (len(array)-1)/2
    l = int(index/w)
    a = (m-l)*w
    return int(2*(a)+index)

def flipImage(array):
    for i in range(int(len(array)/2*len(array[0]))):
        swapPixel(array, i, simpleFlipPair(array, i))

This is the fastest way I have found of calculating this. Approximately nine times faster than the standard solution.

Code
Diff
  • def euler(num):
        return sum(range(3,num,3)) + sum(range(5,num,5)) - sum(range(15,num,15))
    • def euler(num):
    • return sum([i for i in range(num) if i%3==0 or i%5==0])
    • return sum(range(3,num,3)) + sum(range(5,num,5)) - sum(range(15,num,15))