-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhanoi.py
More file actions
30 lines (24 loc) · 826 Bytes
/
Copy pathhanoi.py
File metadata and controls
30 lines (24 loc) · 826 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
# Recursive function for Tower of Hanoi
def hanoi(disks, source, helper, destination):
# Base Condition
if (disks == 1):
print('Disk {} moves from tower {} to tower {}.'.format(disks, source, destination))
return
# Recursive calls in which function calls itself
# print("1st recursion : {}".format(disks-1))
# print(" ")
hanoi(disks - 1, source, destination, helper)
# print("2nd recursion : {}".format(disks-1))
# print(" ")
print('Sachin {} moves from tower {} to tower {}.'.format(disks, source, destination))
hanoi(disks - 1, helper, source, destination)
# Driver code
disks = int(input('Number of disks to be displaced: '))
'''
Tower names passed as arguments:
Source: A
Helper: B
Destination: C
'''
# Actual function call
hanoi(disks, 'A', 'B', 'C')