-
Notifications
You must be signed in to change notification settings - Fork 0
/
tower_of_hanoi.py
53 lines (43 loc) · 1.07 KB
/
tower_of_hanoi.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 25 20:49:21 2020
@author: aksha
"""
def move_statement(to,fro):
"""
Parameters
----------
to : integer
destination of moving spikes
fro : integer
origination of spikes.
Returns
-------
None.
"""
print(f"moving from tower {str(to)} to tower {str(fro)}")
def towers(n,to,fro,spare):
"""
Implementation of tower of hanoi:
Parameters
---------
n : number of spikes
to : destination of moving spikes
fro : origination of spikes.
spare : 3rd tower
Returns
-------
None.
"""
if n==1:
move_statement(to, fro)
else:
towers(n-1,to,spare,fro)
towers(1,to,fro,spare)
towers(n-1,spare,fro,to)
if __name__=="__main__":
to = int(input("Enter origination tower ="))
fro = int(input("Enter destination tower ="))
spare = int(input("Enter spare tower ="))
n = int(input("Enter number of spikes ="))
towers(n,to,fro,spare)