Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.