Skip to content

Instantly share code, notes, and snippets.

@brainix
Last active September 25, 2020 09:15
Show Gist options
  • Save brainix/8ca3fec196d1def81bc58f5b92f651cc to your computer and use it in GitHub Desktop.
Save brainix/8ca3fec196d1def81bc58f5b92f651cc to your computer and use it in GitHub Desktop.
>>> def hanoi(num, from_, using, to):
... if num:
... yield from hanoi(num-1, from_, to, using)
... yield f'move disc {num} from peg {from_} to peg {to}'
... yield from hanoi(num-1, using, from_, to)
...
>>> for move in hanoi(4, 1, 2, 3):
... print(move)
...
move disc 1 from peg 1 to peg 2
move disc 2 from peg 1 to peg 3
move disc 1 from peg 2 to peg 3
move disc 3 from peg 1 to peg 2
move disc 1 from peg 3 to peg 1
move disc 2 from peg 3 to peg 2
move disc 1 from peg 1 to peg 2
move disc 4 from peg 1 to peg 3
move disc 1 from peg 2 to peg 3
move disc 2 from peg 2 to peg 1
move disc 1 from peg 3 to peg 1
move disc 3 from peg 2 to peg 3
move disc 1 from peg 1 to peg 2
move disc 2 from peg 1 to peg 3
move disc 1 from peg 2 to peg 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment