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()