pancake sort

🏠

https://www.pramp.com/challenge/3QnxW6xoPLTNl5jX5LM1

 1 def flip(arr, k):
 2   arr[:k+1] = reversed(arr[:k+1])
 3 
 4 def pancake_sort(arr):
 5   for i in reversed(range(len(arr))):
 6     m = max(arr[:i+1])
 7     mi = arr.index(m)
 8     flip(arr, mi)
 9     flip(arr, i)
10 
11 arr = [1, 5, 4, 3, 2]
12 pancake_sort(arr)
13 
14 print(arr)

See also:

reversing a subarray