Skip to content

Instantly share code, notes, and snippets.

@phg1024
Last active October 13, 2016 06:28
Show Gist options
  • Save phg1024/a575054b6c9a9bce87ce3fb2510c8eb9 to your computer and use it in GitHub Desktop.
Save phg1024/a575054b6c9a9bce87ce3fb2510c8eb9 to your computer and use it in GitHub Desktop.
all_plates = 'ABC'
state = {
'A': [],
'B': [],
'C': []
}
def print_state():
print 'A:', state['A'], 'B:', state['B'], 'C:', state['C']
def move_pancakes(n, plate_from, plate_to):
if n == 1:
print plate_from, '->', plate_to
top = state[plate_from][-1]
state[plate_from] = state[plate_from][:-1]
state[plate_to].append(top)
print_state()
else:
plate_avail = [x for x in all_plates if x not in [plate_from, plate_to]]
move_pancakes(n-1, plate_from, plate_avail[0])
move_pancakes(1, plate_from, plate_to)
move_pancakes(n-1, plate_avail[0], plate_to)
def move(n):
print '[%d pancakes]' % n
state['A'] = list(reversed(range(1,n+1)))
state['B'] = []
state['C'] = []
print_state()
move_pancakes(n, 'A', 'C')
move(2)
move(3)
move(8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment