Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0318609

Browse files
Added tower_of_hanoi_stack.py
1 parent 0df3f1b commit 0318609

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

‎tower_of_hanoi_stack.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
from stack import Stack
2+
3+
4+
class TowerOfHanoi:
5+
class __EmptySourceRodError(Exception):
6+
def __str__(self) -> str:
7+
return "Number of disks in the source rod cannot be less than 1."
8+
9+
class __AlreadySolvedError(Exception):
10+
def __str__(self) -> str:
11+
return "The puzzle has already been solved. Please reset the puzzle before adding more disks."
12+
13+
def __init__(self) -> None:
14+
self.__source_rod: Stack = Stack()
15+
self.__auxiliary_rod: Stack = Stack()
16+
self.__destination_rod: Stack = Stack()
17+
self.__no_of_disks: int = 0
18+
self.__solved: bool = False
19+
20+
def solved(self) -> bool:
21+
return self.__solved
22+
23+
def add_disks(self, no_of_disks: int) -> None:
24+
if not self.__solved:
25+
self.__no_of_disks += no_of_disks
26+
self.__source_rod.empty_stack()
27+
for disk_id in reversed(range(1, self.__no_of_disks + 1)):
28+
self.__source_rod.push(f'disk {disk_id}')
29+
else:
30+
raise self.__AlreadySolvedError
31+
32+
def display(self) -> None:
33+
print(self.__source_rod, end=' - ')
34+
print(self.__auxiliary_rod, end=' - ')
35+
print(self.__destination_rod)
36+
37+
def __tower_of_hanoi(self, disks: int, source: Stack, destination: Stack, auxiliary: Stack) -> None:
38+
if disks == 1:
39+
source.pop()
40+
destination.push('disk 1')
41+
elif disks > 1:
42+
self.__tower_of_hanoi(disks - 1, source=source, destination=auxiliary, auxiliary=destination)
43+
source.pop()
44+
destination.push(f'disk {disks}')
45+
self.__tower_of_hanoi(disks - 1, source=auxiliary, destination=destination, auxiliary=source)
46+
47+
def start(self) -> None:
48+
if self.__no_of_disks < 1:
49+
raise self.__EmptySourceRodError
50+
self.__tower_of_hanoi(len(self.__source_rod), self.__source_rod, self.__destination_rod, self.__auxiliary_rod)
51+
self.__solved = True
52+
53+
def reset(self) -> None:
54+
self.__source_rod.empty_stack()
55+
self.__auxiliary_rod.empty_stack()
56+
self.__destination_rod.empty_stack()
57+
self.__no_of_disks = 0
58+
self.__solved = False
59+
60+
61+
if __name__ == '__main__':
62+
tower = TowerOfHanoi()
63+
tower.add_disks(int(input('Enter the number of the disks : ')))
64+
print("Adding 2 more disks")
65+
tower.add_disks(2)
66+
print("Unsolved")
67+
tower.display()
68+
tower.start()
69+
print("Solved")
70+
tower.display()
71+
tower.reset()
72+
print("Reset")
73+
tower.display()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /