forked from vipinkjonwal/pythonCodes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhanoi.py
More file actions
37 lines (33 loc) · 1000 Bytes
/
hanoi.py
File metadata and controls
37 lines (33 loc) · 1000 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
def towerOfHanoi(numDisks, source, spare, destination):
'''
Objective : To solve the problem of tower of hanoi.
Input Variable :
numDisks : integer - Number of disks.
source : source tower.
spare : spare tower.
destination : destination tower.
Return value : None.
'''
#Approach : Using recursion.
assert numDisks>0
if numDisks==1:
print('Move a disk from',source,'to',destination)
else:
towerOfHanoi(numDisks-1,source,destination,spare)
print('Move a disk from',source,'to',destination)
towerOfHanoi(numDisks-1,spare,source,destination)
def main():
'''
Objective : To solve the problem of tower of hanoi.
Input Variable : None.
Return value : None.
'''
#Approach : Invoke towerHanoiFunction.
source='A'
spare='B'
destination='C'
print('\n\t\t***** TOWERS OF HANOI *****\n')
numDisks=int(input('Enter the number of disks: '))
towerOfHanoi(numDisks,source,spare,destination)
if __name__ == '__main__':
main()