-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday39_speakPython28.py
More file actions
286 lines (206 loc) · 6.21 KB
/
Copy pathday39_speakPython28.py
File metadata and controls
286 lines (206 loc) · 6.21 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
"""🔄 পরিবর্তন করি Day 39 কে একটু advanced করে:
🧠 Day 39 – Speak Python 28 (Advanced Version)
Phase 7: Real-World Thinking (Recursion + Data Structures + OOP + Debugging + Mini Project)"""
"""✅ Problem Set (New & Different)
🔁 Recursion (2 problems – নতুন ধরণের)
1. Nested Sum (Recursive)
Write a recursive function to calculate the sum of all numbers inside a nested list.
Input: [1, [2, [3, 4], 5], 6]
Output: 21"""
def nest_lst_sum(lst):
total = 0
for n in lst:
if isinstance(n, list):
total += nest_lst_sum(n)
else:
total += n
return total
print(nest_lst_sum([1, [2, [3,[3,[8, 9], 5], 4], 5], 6]))
"""2. Directory Traversal (Recursive)
Simulate a recursive function that prints all files in a nested dictionary structure.
files = {
"root": {
"docs": {"a.txt": None, "b.txt": None},
"images": {"photo.png": None, "logo.jpg": None},
"src": {
"main.py": None,
"utils": {"helper.py": None}
}
}
}
Output:
root/docs/a.txt
root/docs/b.txt
root/images/photo.png
root/images/logo.jpg
root/src/main.py
root/src/utils/helper.py"""
def dir_traverse(diractory, path=""):
for name, cont in diractory.items():
new_path = f"{path}/{name}" if path else name
if isinstance(cont, dict):
dir_traverse(cont, new_path)
else:
print(new_path)
files = {
"root": {
"docs": {"a.txt": None, "b.txt": None},
"images": {"photo.png": None, "logo.jpg": None},
"src": {
"main.py": None,
"utils": {"helper.py": None}
}
}
}
# dir_traverse(files)
# ⚡ Other Concepts (2 problems – নতুন ধরণের)
"""3. Set Operations
Given two sets:
A = {1, 2, 3, 4}
B = {3, 4, 5, 6}
Find:
Union
Intersection
Difference (A - B)"""
A = {1, 2, 3, 4}
B = {3, 4, 5, 6}
# builtin method
def main():
print(A.union(B))
print(A.intersection(B))
print(A.difference(B))
# main()
# my wone
def my_union(a, b) -> set:
un = []
for n in a:
if n not in un:
un.append(n)
for n in b:
if n not in un:
un.append(n)
return set(un)
def my_itst(a, b) -> set:
un = []
for n in a:
if n in a and n in b:
un.append(n)
for n in b:
if n in a and n in b:
un.append(n)
return set(un)
def diff_set(a, b):
diff = []
for n in a:
if n not in b:
diff.append(n)
return set(diff)
# print(my_union(A, B))
# print(my_itst(A, B))
# print(diff_set(A, B))
"""4. Frequency Counter (Dictionary)
Write a function that counts the frequency of each character in a string.
Input: "programming"
Output: {'p': 1, 'r': 2, 'o': 1, 'g': 2, 'a': 1, 'm': 2, 'i': 1, 'n': 1}"""
def frq_counter(s) -> str:
frq = {}
for char in s:
frq[char] = frq.get(char, 0) + 1
return frq
# print(frq_counter("jjijsadjfsaldfjsa"))
# print(frq_counter("programming"))
"""🛠️ Debugging Task (নতুন)
Buggy code:
class BankAccount:
def __init__(self, balance):
balance = balance
def deposit(self, amount):
self.balance += amount
acc = BankAccount(100)
acc.deposit(50)
print(acc.balance) # Expected: 150
👉 Fix the error so it works correctly."""
# fixed code:
class BankAccount:
def __init__(self, balance):
self.balance = balance
def deposit(self, amount):
self.balance += amount
acc = BankAccount(100)
acc.deposit(50)
# print(acc.balance) # Expected: 150
"""📂 Mini Project – Password Manager (OOP + File Handling)
Features:
add_password(service, password) → save a password for a service
view_passwords() → display all saved passwords
remove_password(service) → delete password for a service
Save all data in passwords.json"""
from file_utilites import save_json, load_json
class PasswordManager:
def __init__(self, file="passwords.json"):
self.file = file
self.passwords = load_json(self.file)
def add_password(self, service, password):
if service not in self.passwords:
self.passwords[service] = password
save_json(self.passwords, self.file)
else:
print(f"{service} alredy exist")
def view_passwords(self):
if self.passwords:
for i, (serv, pas) in enumerate(self.passwords.items(), start=1):
print(f"{i}. {serv}: {pas}")
else:
print(f"Password manager are empty")
def update_password(self, serv, new_pas):
if serv in self.passwords:
self.passwords[serv] = new_pas
save_json(self.passwords, self.file)
else:
print(f"{serv} not exist")
def remove_password(self, serv):
if serv in self.passwords:
del self.passwords[serv]
save_json(self.passwords, self.file)
else:
print(f"{serv} not exist")
# test's
# pm = PasswordManager()
# pm.add_password("email", "jisanpanda")
# pm.add_password("facebook", "panda_980")
# pm.add_password("twitter", "check_hello")
# pm.view_passwords()
# pm.update_password("email", "hello_jisan")
# pm.view_passwords()
# pm.remove_password("twitter")
# pm.view_passwords()
# small project
def main():
pm = PasswordManager()
while True:
print("___Menu Bar___")
print("1. Add")
print("2. Remove")
print("3. Update")
print("4. View")
print("5. Exit")
choise = int(input("Opsion: "))
if choise == 1:
service = input("Enter servies name: ")
password = input("Enter your password: ")
pm.add_password(service, password)
elif choise == 2:
service = input("Enter servies name: ")
pm.remove_password(service)
elif choise == 3:
service = input("Enter servies name: ")
new_password = input("Enter your password: ")
pm.update_password(service, new_password)
elif choise == 4:
pm.view_passwords()
elif choise == 5:
break
else:
print("Error: Invalid choise")
# if __name__ == "__main__":
# main()