 # pancake flip

Apr 20th, 2021
92
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. from random import randrange
2. array = [9,0,1,5,4,2]
3. num_elements = 100
4.
5. def generateArray():
6.     for i in range(num_elements):
7.         array.append(randrange(1,100))
8.
9. def flip(index):
10.     mid = index//2
11.     for i in range(0,mid+1):
12.         temp = array[i]
13.         array[i] = array[index-i]
14.         array[index-i] = temp
15.
16. def solve():
17.     for i in range(len(array)-1, 0, -1):
18.         max_index = 0
19.         for j in range(1,i+1): # i+1 is not necessary because it will just flip back anyways
20.             if (array[j] > array[max_index]):
21.                 max_index = j
22.         flip(max_index)
23.         #print("max_index: " + array)
24.         flip(i)
25.         #print("flip to index: " + array)
26.
27. def main():
28.     #generateArray()
29.     print(array)
30.     solve()
31.     print(array)
32.
33.
34. if __name__ == '__main__':
35.     main()