Skip to content

Instantly share code, notes, and snippets.

@kuanb
Created August 8, 2014 17:28
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 kuanb/e7155e51d6ba9950dbfd to your computer and use it in GitHub Desktop.
Save kuanb/e7155e51d6ba9950dbfd to your computer and use it in GitHub Desktop.
Adjacent Hanoi
def adjacent_hanoi(num_discs, start_peg, end_peg):
"""
For this problem, discs should always start on the first peg and
end on the last peg.
num_discs: an integer number of discs
start_peg: starting peg
end_peg: ending peg
returns: integer number of moves, actual moves required
"""
num_moves = 0
moves = []
if num_discs > 0:
a = adjacent_hanoi(num_discs-1, start_peg, end_peg)
#resolving the tuple issue, pulling the num_moves out
num_moves += 1
num_moves += a[0]
moves += a[1]
#concept is to: "Move disc", num_discs, "from peg", start_peg, "to peg", 2, "(middle)"
b = adjacent_hanoi(num_discs-1, end_peg, start_peg)
#resolving the tuple issue, pulling the num_moves out
num_moves += 1
num_moves += b[0]
moves.append((num_discs, start_peg, 2))
moves += b[1]
#concept is to: "Move disc", num_discs, "from peg", 2 , "(middle) to peg", end_peg
c = adjacent_hanoi(num_discs-1, start_peg, end_peg)
num_moves += c[0]
moves.append((num_discs, 2, end_peg))
moves += c[1]
return (num_moves, moves)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment