Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions tower_of_hanoi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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()