Skip to content

Instantly share code, notes, and snippets.

@lettergram
Created March 18, 2015 23:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lettergram/ab4edcee87cdd71df80b to your computer and use it in GitHub Desktop.
Save lettergram/ab4edcee87cdd71df80b to your computer and use it in GitHub Desktop.
# Sorts Pancakes
def sortPancakes(stack):
sorted_index = 10
for i in reversed(range(len(stack))):
stack = flip(stack, findLargestPancake(stack, i))
print("Flip Up", stack)
stack = flip(stack, i)
print("Flip Down", stack)
return stack
# All of the pancakes are sorted after index
# Returns the index of largest unsorted pancake
def findLargestPancake(stack, index):
largest_pancake = stack[index]
largest_index = index;
for i in range(index):
if stack[i] > largest_pancake:
largest_pancake = stack[i]
largest_index = i
print ""
print("Insert Spatula in index", largest_index, "Size", largest_pancake)
return largest_index
# Slide spatula under pancake at index and flip to top
def flip(stack, index):
newStack = stack[:(index + 1)]
newStack.reverse()
newStack += stack[(index + 1):]
return newStack
stack = [1, 4, 5, 2, 3, 8, 6, 7, 9, 0]
print"\nUnsorted:"
print stack
print "\nIterating:"
stack = sortPancakes(stack)
print "\nSorted:"
print stack
print ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment