-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtower.py
More file actions
33 lines (27 loc) · 920 Bytes
/
tower.py
File metadata and controls
33 lines (27 loc) · 920 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
def hanoi(n, source, spare, target):
'''
objective: to solve the problem of tower of hanoi for n user input disks
parameters: -> n: no of disks
-> source: the source pole holding all disks
-> spare: spare pole used to hold temporary disks
-> target: target pole holding final poles
'''
assert n>0
if n == 1:
print("Move disk from ", source, "to ", target)
else:
hanoi(n-1, source, target, spare)
print("Move disk from ", source, "to ", target)
hanoi(n-1, spare, source, target)
def main():
'''
objective: to solve the problem of tower of hanoi for n user input disks
input parameters: -> n: no of disks
'''
no = int(input("Enter number of disks: "))
source = 1
spare = 2
target = 3
hanoi(no, source, spare, target)
if __name__ == '__main__':
main()