FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Awesome-Python-Scripts/Basic-Scripts/recursion.py at master · powerexploit/Awesome-Python-Scripts · GitHub
powerexploit
/
Awesome-Python-Scripts
Public
Notifications
You must be signed in to change notification settings
Fork
212
Star
243
Code
Issues
23
Pull requests
82
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Awesome-Python-Scripts
/
Basic-Scripts
/
recursion.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
35 lines (31 loc) · 1.32 KB
Breadcrumbs
Awesome-Python-Scripts
/
Basic-Scripts
/
recursion.py
Copy path
File metadata and controls
35 lines (31 loc) · 1.32 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
))
Back
|
FazBrowse Home
|
New Git URL