Skip to content

Instantly share code, notes, and snippets.

@metula
Created December 18, 2013 21:49
Show Gist options
  • Save metula/8030414 to your computer and use it in GitHub Desktop.
Save metula/8030414 to your computer and use it in GitHub Desktop.
def HanoiTowers(start, via, target, n):
""" computes a list of discs steps to move a stack of n discs from
rod "start" to rod "target" employing intermidiate rod "via" """
if n == 0:
return []
else:
return HanoiTowers(start, target, via, n-1) \
+ [str.format("disk {} from {} to {}", n, start, target)] \
+ HanoiTowers(via, start, target, n-1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment