-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhanoi.py
More file actions
22 lines (20 loc) · 756 Bytes
/
hanoi.py
File metadata and controls
22 lines (20 loc) · 756 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
def hanoi(n,source,spare,target):
'''
objective: to transfer discs from source to target with the heavier disc
below the lighter one
input parameters:
n: number of discs
source: tower from which disc need to be transferred
spare: tower which is used as an intermediate to transfer disc
from source to destination
target: tower to which the discs needs to be transferred
return value: none
'''
assert n>0
if n==1:
print('Move a disc from ',source,' to ',target)
else:
hanoi(n-1,source,target,spare)
print('Move a disc from ',source,' to ',target)
hanoi(n-1,spare,source,target)
hanoi(3,1,2,3)