Created
December 23, 2013 16:59
-
-
Save metula/8100598 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def hanoi_towers(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 hanoi_towers(start, target, via, n-1) \ | |
+ [str.format("disk {} from {} to {}", n, start, target)] \ | |
+ hanoi_towers(via, start, target, n-1) | |
def hanoi_move(start, via, target,n , k): | |
""" finds the k-th move in an Hanoi Towers instance with n discs """ | |
if n <= 0: | |
return "zero or fewer disks" | |
elif k <= 0 or k >= 2**n or type(k) != int: | |
return "number of moves is illegal" | |
elif k == 2**(n-1): | |
return str.format("disk {} from {} to {}", n, start, target) | |
elif k < 2**(n-1): | |
return hanoi_move(start, target, via, n-1, k) | |
else: | |
return hanoi_move(via, start, target, n-1, k-2**(n-1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment