-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday25_speakPython14.py
More file actions
171 lines (124 loc) Β· 3.08 KB
/
Copy pathday25_speakPython14.py
File metadata and controls
171 lines (124 loc) Β· 3.08 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
"""π§ **Day 25 β Speak Python 14**
### **Phase 3: Recursion & Thinking in Steps**
**Focus Areas:**
* Introduction to Recursion
* Factorial & Fibonacci
* Sum & Reverse with Recursion
* Mini Project with Recursion"""
"""β
Problem 1: Factorial with Recursion
π **Task:**
Write a function to calculate factorial of a number using recursion.
π **Example:**
```python
factorial(5) β 120
factorial(0) β 1```
π‘ **Hint:**
`n! = n \* (n-1)!` with base case `factorial(0) = 1`.
"""
# def factorial(n):
# if n == 0:
# return 1
# return factorial(n-1) * n
# print(factorial(0))
# print(factorial(5))
"""β
Problem 2: Fibonacci with Recursion
π **Task:**
Write a recursive function to get the nth Fibonacci number.
π **Example:**
```python
fibonacci(5) β 5
fibonacci(7) β 13
```
π‘ **Hint:**
`fibonacci(n) = fibonacci(n-1) + fibonacci(n-2)` with base cases `fibonacci(0)=0`, `fibonacci(1)=1`."""
# def fibonacci(n):
# if n == 0 or n == 1:
# return n
# return fibonacci(n-1) + fibonacci(n-2)
# print(fibonacci(5))
# print(fibonacci(7))
# print(fibonacci(13))
# for n in range(1, 10+1):
# print(fibonacci(n))
"""β
Problem 3: Recursive Sum of List
π **Task:**
Write a recursive function that returns the sum of all numbers in a list.
π **Example:**
```python
recursive\_sum(\[1, 2, 3, 4]) β 10
recursive\_sum(\[5, 10, 15]) β 30```
"""
# def recursive_sum(lst):
# if len(lst) == 1:
# return lst[0]
# return lst[0] + recursive_sum(lst[1:])
# print(recursive_sum([1, 2, 3, 4]))
# print(recursive_sum([5, 10, 15]))
"""β
Problem 4: Reverse String with Recursion
π **Task:**
Write a recursive function that reverses a string.
π **Example:**
```python
reverse\_string("python") β "nohtyp"
reverse\_string("Jisan") β "nasiJ"
```
"""
# def reverse_string(s):
# if len(s) == 0:
# return s
# return reverse_string(s[1:]) + s[0]
# print(reverse_string("python"))
# print(reverse_string("Jisan"))
"""βοΈ Micro Project β Recursive Directory Printer
π **Task:**
Simulate printing files/folders in a nested structure using recursion.
π **Example:**
```python
files = {
"root": {
"file1.txt": None,
"docs": {
"resume.docx": None,
"notes.txt": None
}
}
}
```
π Write a function `print\_files(files, indent=0)` that prints the structure like:
```
root
file1.txt
docs
resume.docx
notes.txt
```
"""
files = {
"root": {
"file1.txt": None,
"docs": {
"resume.docx": None,
"notes.txt": None
}
}
}
files2 = {
"root": {
"file1.txt": None,
"docs": {
"resume.docx": None,
"notes.txt": None,
"text": {
"hello.txt": None,
"welcome.txt": None
}
}
}
}
def print_files(files, indent=0):
for name, content in files.items():
print(" " * indent + name)
if isinstance(content, dict):
print_files(content, indent+1)
print_files(files)
print_files(files2)