-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10_Recursion_In_Python.py
More file actions
267 lines (212 loc) · 7.37 KB
/
10_Recursion_In_Python.py
File metadata and controls
267 lines (212 loc) · 7.37 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# Topic: Recursion in Python
# Author: Hamna Munir
# Description:
# This file explains recursion, how it works internally,
# and includes examples of simple, nested, and advanced recursion.
# ============================================================
# ============================================================
# 1. What is Recursion?
# ============================================================
"""
Definition:
Recursion is a process in which a function calls itself
directly or indirectly to solve a problem.
It breaks down a big problem into smaller subproblems
until it reaches a simple base case.
Every recursive function must have:
➤ A Base Case – the stopping condition
➤ A Recursive Case – the step where the function calls itself
"""
# Example: Simple recursive function to print numbers
def print_numbers(n):
if n == 0: # Base Case
return
print(n)
print_numbers(n - 1) # Recursive Case
print("Printing numbers using recursion:")
print_numbers(5)
# Output:
# 5
# 4
# 3
# 2
# 1
# ============================================================
# 2. How Recursion Works Internally (Call Stack)
# ============================================================
"""
Concept:
Each time a recursive function is called, Python pushes a new frame
onto the *call stack*. When the function returns, the frame is popped off.
For example, in print_numbers(3):
→ print_numbers(3)
→ print_numbers(2)
→ print_numbers(1)
→ print_numbers(0) (base case reached)
Then it unwinds back up the stack.
"""
# ============================================================
# 3. Factorial Using Recursion
# ============================================================
"""
Factorial of n = n × (n-1) × (n-2) × ... × 1
Example: 5! = 5×4×3×2×1 = 120
"""
def factorial(n):
if n == 0 or n == 1: # Base Case
return 1
else:
return n * factorial(n - 1) # Recursive Case
print("\nFactorial of 5 is:", factorial(5))
# Output: 120
# ============================================================
# 4. Fibonacci Series Using Recursion
# ============================================================
"""
Fibonacci Sequence:
0, 1, 1, 2, 3, 5, 8, 13, ...
Formula:
fib(n) = fib(n-1) + fib(n-2)
"""
def fibonacci(n):
if n <= 1: # Base Case
return n
else:
return fibonacci(n - 1) + fibonacci(n - 2)
print("\nFibonacci sequence (first 6 numbers):")
for i in range(6):
print(fibonacci(i), end=" ")
# Output: 0 1 1 2 3 5
# ============================================================
# 5. Sum of Natural Numbers Using Recursion
# ============================================================
def recursive_sum(n):
if n == 0:
return 0
else:
return n + recursive_sum(n - 1)
print("\n\nSum of first 5 natural numbers:", recursive_sum(5))
# Output: 15
# ============================================================
# 6. Palindrome Check Using Recursion
# ============================================================
def is_palindrome(s):
if len(s) <= 1:
return True
if s[0] != s[-1]:
return False
return is_palindrome(s[1:-1])
word = "madam"
print(f"\nIs '{word}' a palindrome?", is_palindrome(word))
# Output: True
# ============================================================
# 7. Reverse a String Using Recursion
# ============================================================
def reverse_string(s):
if len(s) == 0:
return ""
else:
return s[-1] + reverse_string(s[:-1])
text = "Hamna"
print("\nReversed string:", reverse_string(text))
# Output: anmaH
# ============================================================
# 8. Nested Recursion
# ============================================================
"""
Concept:
A recursive function calling itself more than once in the same call.
Example: McCarthy 91 function
"""
def mcCarthy_91(n):
if n > 100:
return n - 10
else:
return mcCarthy_91(mcCarthy_91(n + 11))
print("\nMcCarthy 91 output for n = 99:", mcCarthy_91(99))
# Output: 91
# ============================================================
# 9. Indirect Recursion
# ============================================================
"""
Concept:
In indirect recursion, one function calls another, and that function
calls the first one again.
"""
def funcA(n):
if n > 0:
print(n)
funcB(n - 1)
def funcB(n):
if n > 1:
print(n)
funcA(n // 2)
print("\nExample of Indirect Recursion:")
funcA(5)
# Output may vary based on call pattern
# ============================================================
# 10. Tail Recursion
# ============================================================
"""
Concept:
If the recursive call is the *last* thing executed in the function,
it’s called a Tail Recursive Function.
Python doesn’t optimize tail recursion (unlike some languages),
but it’s a good concept to know.
"""
def tail_factorial(n, accumulator=1):
if n == 0:
return accumulator
else:
return tail_factorial(n - 1, n * accumulator)
print("\nTail Recursive Factorial of 5:", tail_factorial(5))
# Output: 120
# ============================================================
# 11. Advantages and Disadvantages of Recursion
# ============================================================
"""
Advantages:
- Elegant and easy to understand for repetitive, tree-like problems
- Reduces code size
- Used in divide-and-conquer algorithms (like QuickSort, MergeSort)
Disadvantages:
- Consumes more memory (stack frames)
- May cause stack overflow for deep recursion
- Usually slower than iteration
"""
# ============================================================
# 12. Recursion vs Iteration
# ============================================================
"""
| Feature | Recursion | Iteration |
|------------------|----------------------------------------|----------------------------------|
| Code Structure | Function calls itself | Uses loops (for/while) |
| Memory Usage | High (stack frames) | Low |
| Speed | Usually slower | Usually faster |
| Base Condition | Required (to stop recursion) | Loop condition controls exit |
| Example Use Case | Factorial, Tree Traversal, Fibonacci | Counting, Repeated tasks |
"""
# Iterative factorial (for comparison)
def factorial_iterative(n):
result = 1
for i in range(1, n + 1):
result *= i
return result
print("\nIterative Factorial of 5:", factorial_iterative(5))
# Output: 120
# ============================================================
# 13. Summary
# ============================================================
"""
📘 Summary of Recursion:
1️⃣ Recursion → Function calls itself.
2️⃣ Must have Base Case to avoid infinite recursion.
3️⃣ Uses Call Stack internally.
4️⃣ Common examples: Factorial, Fibonacci, Palindrome.
5️⃣ Indirect & Nested Recursion → Functions call each other or themselves multiple times.
6️⃣ Tail Recursion → Last operation in function is recursive call.
7️⃣ Compare recursion with iteration for efficiency.
"""
# ============================================================
# END OF FILE
# ============================================================