-
Notifications
You must be signed in to change notification settings - Fork 213
Expand file tree
/
Copy pathrecursion.py
More file actions
35 lines (31 loc) · 1.32 KB
/
recursion.py
File metadata and controls
35 lines (31 loc) · 1.32 KB
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
# "Towers of Hanoi" Game
# A recursive solution almost forces itself on the programmer,
# while the iterative solution of the game is hard to find and to grasp.
#
# "Recursion"
# Recursion is a method of programming or coding a problem,
# in which a function calls itself one or more times in its body.
# Usually, it is returning the return value of this function call.
# If a function definition satisfies the condition of recursion, we call this function a recursive function.
def hanoi(n, source, helper, target):
if n > 0:
# move tower of size n-1 to helper:
hanoi(n-1, source, target, helper)
# move disk from source to target:
if source:
target.append(source.pop())
# move tower of size n-1 from helper to target:
hanoi(n-1, helper, source, target)
if __name__ == "__main__":
height = int(input("Enter the height of the tower: "))
source = [disk for disk in range(1, height+1)]
helper = []
target = []
print("Before calling the recursive function...")
print("Source tower: ", str(source))
print("Target tower: ", str(target))
# call the hanoi function to start recursie execution
hanoi(height, source, helper, target)
print("After recursive calls...")
print("Source tower: ", str(source))
print("Target tower: ", str(target))