From abe44e622f8850ce9c923c213596ea652e8d0e6b Mon Sep 17 00:00:00 2001 From: Prathima Kadari Date: Sun, 23 May 2021 17:01:54 +0530 Subject: [PATCH] Added tower of hanoi program --- tower_of_hanoi.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 tower_of_hanoi.py diff --git a/tower_of_hanoi.py b/tower_of_hanoi.py new file mode 100644 index 0000000..73388d6 --- /dev/null +++ b/tower_of_hanoi.py @@ -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()