Python/other/tower_of_hanoi.py

29 lines
700 B
Python
Raw Permalink Normal View History

def move_tower(height, from_pole, to_pole, with_pole):
2019-10-05 05:14:13 +00:00
"""
>>> move_tower(3, 'A', 'B', 'C')
2016-08-14 12:13:32 +00:00
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:
move_tower(height - 1, from_pole, with_pole, to_pole)
move_disk(from_pole, to_pole)
move_tower(height - 1, with_pole, to_pole, from_pole)
2019-10-05 05:14:13 +00:00
def move_disk(fp, tp):
2019-10-05 05:14:13 +00:00
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())
move_tower(height, "A", "B", "C")
2019-10-05 05:14:13 +00:00
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()