Python/other/tower_of_hanoi.py

29 lines
682 B
Python
Raw Normal View History

def moveTower(height, fromPole, toPole, withPole):
2019-10-05 05:14:13 +00:00
"""
2016-08-14 12:13:32 +00:00
>>> moveTower(3, 'A', 'B', 'C')
moving disk from A to B
moving disk from A to C
moving disk from B to C
moving disk from A to B
moving disk from C to A
moving disk from C to B
moving disk from A to B
2019-10-05 05:14:13 +00:00
"""
2016-08-14 12:13:32 +00:00
if height >= 1:
2019-10-05 05:14:13 +00:00
moveTower(height - 1, fromPole, withPole, toPole)
2016-08-14 12:13:32 +00:00
moveDisk(fromPole, toPole)
2019-10-05 05:14:13 +00:00
moveTower(height - 1, withPole, toPole, fromPole)
def moveDisk(fp, tp):
print("moving disk from", fp, "to", tp)
2016-08-14 12:13:32 +00:00
def main():
2019-10-05 05:14:13 +00:00
height = int(input("Height of hanoi: ").strip())
moveTower(height, "A", "B", "C")
2016-08-14 12:13:32 +00:00
2019-10-05 05:14:13 +00:00
if __name__ == "__main__":
2016-08-14 12:13:32 +00:00
main()