Last active
September 25, 2020 09:15
-
-
Save brainix/8ca3fec196d1def81bc58f5b92f651cc 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(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