-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday29_speakPython18.py
More file actions
182 lines (124 loc) Β· 3.3 KB
/
Copy pathday29_speakPython18.py
File metadata and controls
182 lines (124 loc) Β· 3.3 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
"""π§ **Day 29 β Speak Python 18**
### **Phase 6: Balanced Learning (Recursion + Core Topics)**
**Focus Areas:**
* Recursion practice
* Core Python concepts (OOP, File Handling, etc.)
* Debugging real problems
* Mini Project for fun π"""
"""### β
Problem 1: Recursion β Factorial Sum
π **Task:**
Write a recursive function that calculates the sum of factorials from 1 to `n`.
π Example:
```
factorial_sum(3) β 1! + 2! + 3! = 9
factorial_sum(4) β 33
```
π‘ **Hint:**
Use a helper factorial function + recursion."""
# def factorial(n):
# if n == 0:
# return 1
# return n * factorial(n-1)
# def factorial_sum(n):
# if n == 1:
# return 1
# return factorial(n) + factorial_sum(n-1)
# print(factorial_sum(3))
# print(factorial_sum(4))
"""β
Problem 2: Recursion β Reverse String
π **Task:**
Reverse a string using recursion.
π Example:
```
reverse_string("hello") β "olleh"
```
π‘ **Hint:**
Last char + reverse(rest)."""
# def reverse_string(s):
# if len(s) == 0:
# return s
# return reverse_string(s[1:]) + s[0]
# print(reverse_string("Jisan"))
# print(reverse_string("hello"))
"""β
Problem 3: OOP β Bank Account Class
π **Task:**
Create a `BankAccount` class with:
* `deposit(amount)`
* `withdraw(amount)`
* `get_balance()`
π Example:
```python
acc = BankAccount(1000)
acc.deposit(500) # Balance = 1500
acc.withdraw(200) # Balance = 1300"""
# class BankAccount:
# def __init__(self, balance):
# self.balance = balance
# def deposit(self, amount):
# if amount > 0:
# self.balance += amount
# else:
# print("Invalid Amount")
# def withdraw(self, amount):
# if self.balance > amount:
# self.balance -= amount
# else:
# print("Incorrect Amount")
# acc = BankAccount(1000)
# acc.deposit(500)
# print(acc.balance) # Balance = 1500
# acc.withdraw(200) # Balance = 1300
# print(acc.balance)
"""β
Problem 4: File Handling β Line Counter
π **Task:**
Write a program to count how many lines are in a file `sample.txt`.
π Example:
```
sample.txt β
Python
is
fun
Output: 3 lines"""
# with open("sample.txt", "w") as f:
# f.write("Python\nis\nfun")
# with open("sample.txt", "r") as f:
# data = f.readlines()
# print(f"Output: {len(data)} lines")
"""βοΈ Mini Project β To-Do List Manager (File + OOP)
π **Task:**
Make a `Todo` class where you can add tasks, view tasks, and save them to a file.
π Example:
```python
todo = Todo()
todo.add("Learn Python")
todo.add("Build Project")
todo.show()"""
# class Todo:
# todo = []
# def add(self, task):
# self.todo.append(task)
# def show(self):
# tasks = self.todo
# for n, task in enumerate(tasks,start=1):
# print(n, task)
# todo = Todo()
# todo.add("Learn Python")
# todo.add("Build Project")
# todo.show()
"""π Debugging Task β Fix the Code
β Wrong Code:
```python
def fibonacci(n):
if n == 0 or n == 1:
return 0 # β Wrong for n=1
return fibonacci(n-1) + fibonacci(n-2)
print(fibonacci(5))
```
β
Expected Output:
fibonacci(5) β 5"""
# β
fixed code:
def fibonacci(n):
if n == 0 or n == 1:
return n
return fibonacci(n-1) + fibonacci(n-2)
print(fibonacci(5))