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)/2l = int(index/w)a = (m-l)*wreturn 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))
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))
# TODO: Replace examples and use TDD development by writing your own tests
# These are some of the methods available:
# test.expect(boolean, [optional] message)
# test.assert_equals(actual, expected, [optional] message)
# test.assert_not_equals(actual, expected, [optional] message)
# You can use Test.describe and Test.it to write BDD style test groupings
flipImage(array)
test.assert_equals(array, arrayFlipped)
This is the fastest way I have found of calculating this. Approximately nine times faster than the standard solution.