forked from harshulmca17/MCA-101-_2017
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hnoi.py
31 lines (21 loc) · 715 Bytes
/
hnoi.py
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
def hnoi(n, source, spare, target):
'''
Objective : To print steps for tower of hnoi problem.
Input Parameters:
n : no. of disks.
source: name of the source disk
target: name of the target disk.
spare: name of the disk other then source and target disks.
Output : Steps of the solution.
'''
#Approach : Using Recursion
assert n>0
if n==1:
print("Move a disk from ",source, "to ", target)
elif n == 0:
return
else:
hnoi(n-1, source, target, spare)
print("Move a disk from ",source, "to ", target)
hnoi(n-1, spare, source, target)
hnoi(3,1,2,3)