-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday37_speakPython26.py
More file actions
200 lines (145 loc) Β· 4.42 KB
/
Copy pathday37_speakPython26.py
File metadata and controls
200 lines (145 loc) Β· 4.42 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
"""π§ Day 37 β Speak Python 26
Phase 7: Real-World Thinking (Recursion + Data Structures + OOP + Debugging + Mini Project)
"""
# ---------------------------------------------------
"""β
Problem Set
π Recursion (2 problems)
1. Sum of Digits (Recursive)
Write a recursive function that returns the sum of digits of a number.
Input: sum_digits(1234)
Output: 10"""
def sum_digits(d):
if d // 10 == 0:
return d
return (d % 10) + sum_digits(d // 10)
# print(sum_digits(1234))
# ---
"""2. Reverse String (Recursive)
Write a recursive function to reverse a string.
Input: "hello"
Output: "olleh" """
def rvs_str(s):
if len(s) == 1:
return s
return rvs_str(s[1:len(s)]) + s[0]
# print(rvs_str("jisan"))
# ---------------------------------------------------
"""β‘ Other Concepts (2 problems)
3. List Flattening
Given: nested_list = [[1, 2], [3, 4], [5, 6]]
Flatten this into a single list: [1, 2, 3, 4, 5, 6]"""
def unpack_nested_lst(nested_list):
"""This function unpack 2D list"""
try:
single_list = []
for lst in nested_list:
for n in lst[0: len(lst)]:
single_list.append(n)
return single_list
except TypeError:
return "Only allow '2D' list"
nested_list = [[1, 2], [3, 4], [5, 6]]
nst = [7, 6, 7]
nst_lst = [
[8, 8, 7, 6],
[5, 5, 4, 4],
[7, 2, 1, 0]
]
# print(unpack_nested_lst.__doc__)
# print(unpack_nested_lst(nested_list))
# print(unpack_nested_lst(nst))
# print(unpack_nested_lst(nst_lst))
# ---
"""4. Dictionary Merge
Given:
dict1 = {"a": 1, "b": 2}
dict2 = {"b": 3, "c": 4}
Merge them into: {"a": 1, "b": 3, "c": 4}"""
def int_dict_marger(dict1, dict2):
try:
marge = dict1.copy()
for k, v in dict2.items():
marge[k] = dict1.get(k, 0) + v
return marge
except TypeError:
return "Function only marge int"
dict1 = {"a": 1, "b": 2}
dict2 = {"b": 3, "c": 4}
test1 = {"math": 87, "cham": 76}
test2 = {"phy": 93, "cs": 99}
# print(int_dict_marger(dict1, dict2))
# print(int_dict_marger(test1, test2))
# ---------------------------------------------------
"""π οΈ Debugging Task
Buggy code:
class Car:
def __init__(self, model, year):
self.model = model
year = year
c = Car("Tesla", 2025)
print(c.year)
π Fix the error so it correctly prints 2025."""
# fixed code:
class Car:
def __init__(self, model, year):
self.model = model
self.year = year
c = Car("Tesla", 2025)
# print(c.year)
# ---------------------------------------------------
"""π Mini Project β Notes App (OOP + File Handling)
Create a simple Notes App.
Features:
- add_note(title, content) β add a new note
- view_notes() β display all notes
- delete_note(title) β delete a note
- search_note(keyword) β search notes by keyword
All notes should be saved in notes.json (you can use your FileUtilites library)."""
from file_utilites import save_json, load_json
class SimpleNotes:
def __init__(self, file="notes.json"):
self.file = file
self.notes = load_json(self.file)
def add_note(self, title, content):
if title not in self.notes:
self.notes[title] = content
save_json(self.notes, self.file)
else:
print("alredy exist")
def view_notes(self):
if self.notes:
for title, cont in self.notes.items():
print(f"Title: {title}")
print(f"Content: {cont}")
else:
print("Notes are empty")
def delete_note(self, title):
if title in self.notes:
del self.notes[title]
save_json(self.notes, self.file)
else:
print("This note not exist")
def search_note(self, title):
if title in self.notes:
print(self.notes[title])
else:
print("This note not exist")
def clear_notes(self):
if self.notes:
self.notes = {}
save_json(self.notes, self.file)
else:
print("Notes are empty")
sn = SimpleNotes()
sn.add_note("Test", "This is a note class")
sn.add_note("Learn Regex", "Learn regex from cs50p")
sn.add_note("Bash Script", "Bash script is use to creat autometion")
sn.view_notes()
sn.delete_note("test")
sn.delete_note("Test")
sn.view_notes()
sn.search_note("test")
sn.search_note("Learn Regex")
sn.view_notes()
sn.clear_notes()
sn.view_notes()