Skip to content

Commit efa3997

Browse files
Merge pull request steam-bell-92#546 from Facelessism/hanoi
Refactor the `utilities/Tower-of-Hanoi`
2 parents 30ab791 + bc0cad1 commit efa3997

1 file changed

Lines changed: 41 additions & 30 deletions

File tree

utilities/Tower-of-Hanoi/Tower-of-Hanoi.py

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,28 @@
22
print("TOWER OF HANOI")
33
print("=" * 50)
44

5+
def display_towers(towers):
6+
print(f"Tower A: {towers['A']}")
7+
print(f"Tower B: {towers['B']}")
8+
print(f"Tower C: {towers['C']}")
9+
10+
def move_disk(towers, source, destination):
11+
source_tower = towers[source]
12+
dest_tower = towers[destination]
13+
14+
if not source_tower:
15+
print(f"Invalid move! Tower {source} is empty.")
16+
return None
17+
disk = source_tower[-1]
18+
19+
if dest_tower and dest_tower[-1] < disk:
20+
print("Invalid move! Cannot place larger disk on smaller disk.")
21+
return None
22+
disk = source_tower.pop()
23+
dest_tower.append(disk)
24+
return disk
25+
26+
527
while True:
628
try:
729
n = int(input("\nEnter the number of disks (1-8): "))
@@ -12,54 +34,43 @@
1234
except ValueError:
1335
print("Please enter a valid number!")
1436

37+
1538
tower_A = list(range(n, 0, -1))
1639
tower_B = []
1740
tower_C = []
1841

42+
towers = {
43+
'A': tower_A,
44+
'B': tower_B,
45+
'C': tower_C
46+
}
47+
1948
stack = [(n, 'A', 'C', 'B')]
2049
move_count = 0
2150

2251
print(f"\nSolving Tower of Hanoi with {n} disks...")
2352
print("Moving from Tower A to Tower C using Tower B as auxiliary")
2453
print("\nInitial State:")
25-
print(f"Tower A: {tower_A}")
26-
print(f"Tower B: {tower_B}")
27-
print(f"Tower C: {tower_C}")
54+
display_towers(towers)
2855
print("\n" + "=" * 50)
2956

3057
while stack:
3158
disks, source, destination, auxiliary = stack.pop()
32-
3359
if disks == 1:
34-
move_count += 1
35-
36-
if source == 'A':
37-
source_tower = tower_A
38-
elif source == 'B':
39-
source_tower = tower_B
40-
else:
41-
source_tower = tower_C
42-
43-
if destination == 'A':
44-
dest_tower = tower_A
45-
elif destination == 'B':
46-
dest_tower = tower_B
47-
else:
48-
dest_tower = tower_C
49-
50-
disk = source_tower.pop()
51-
dest_tower.append(disk)
52-
53-
print(f"Move {move_count}: Move disk {disk} from Tower {source} to Tower {destination}")
54-
print(f"Tower A: {tower_A}")
55-
print(f"Tower B: {tower_B}")
56-
print(f"Tower C: {tower_C}")
57-
print("-" * 50)
60+
disk = move_disk(towers, source, destination)
61+
if disk is not None:
62+
move_count += 1
63+
64+
print(
65+
f"Move {move_count}: Move disk {disk} "
66+
f"from Tower {source} to Tower {destination}"
67+
)
68+
display_towers(towers)
69+
print("-" * 50)
70+
5871
else:
5972
stack.append((disks - 1, auxiliary, destination, source))
60-
6173
stack.append((1, source, destination, auxiliary))
62-
6374
stack.append((disks - 1, source, auxiliary, destination))
6475

6576
print("\n" + "=" * 50)

0 commit comments

Comments
 (0)