-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday35_speakPython24.py
More file actions
155 lines (106 loc) · 3.66 KB
/
Copy pathday35_speakPython24.py
File metadata and controls
155 lines (106 loc) · 3.66 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
"""🧠 Day 35 – Speak Python 24
Phase 6: Balanced Learning (Recursion + Data Structures + OOP + Debugging + Mini Project)
✅ Problem Set
🔁 Recursion (2 problems)
1. Power Function (Recursive)
Write a recursive function to calculate a^b (a raised to the power of b).
Input: power(2, 5)
Output: 32"""
# def power(a, b):
# if b == 0:
# return 1
# return a * power(a, b-1)
# print(power(2, 5))
"""2. Palindrome Check (Recursive)
Write a recursive function to check if a string is a palindrome.
Input: "madam" → Output: True
Input: "python" → Output: False"""
# def is_palindrome(s):
# try:
# if len(s) == 0:
# return True
# elif s[0] != s[-1]:
# return False
# return is_palindrome(s[1:-1])
# except RecursionError:
# return "maximam"
# print(is_palindrome("madam"))
# print(is_palindrome("python"))
# print(is_palindrome("racecar"))
"""⚡ Other Concepts (2 problems)
3. Set Comprehension
Write a set comprehension that stores only the even numbers from 1 to 20.
Output: {2, 4, 6, 8, ..., 20}"""
# even_set = {n for n in range(1, 11) if n % 2 == 0}
# print(even_set)
"""4. Dictionary Filtering Given:
scores = {"Alice": 85, "Bob": 72, "Charlie": 90, "David": 60}
Create a new dictionary with only those who scored 80 or above.
Output: {"Alice": 85, "Charlie": 90}"""
scores = {"Alice": 85, "Bob": 72, "Charlie": 90, "David": 60}
# hiscores = {}
# for name, score in scores.items():
# if score > 80:
# hiscores[name] = score
# print(hiscores)
"""🛠️ Debugging Task
Buggy code:
class Person:
def __init__(self, name):
name = name
p = Person("Jisan")
print(p.name) # Expected: Jisan
👉 Find the error and fix it."""
# Fixed code:
# class Person:
# def __init__(self, name):
# self.name = name
# p = Person("Jisan")
# print(p.name)
"""📂 Mini Project
Contact Book (OOP + File Handling)
একটা ছোট contact manager বানাও।
Features:
add_contact(name, phone) → নতুন contact যোগ করবে
view_contacts() → সব contact দেখাবে
remove_contact(name) → নাম দিয়ে contact delete করবে
Contacts গুলো contacts.json এ save হবে (তোমার FileUtilites library ব্যবহার করতে পারো)।"""
from file_utilites import save_json, load_json
class ContactBook:
def __init__(self, filename="contacts.json"):
self.file = filename
self.contact = load_json(self.file)
def add_contact(self, name, phone):
if name not in self.contact:
self.contact[name] = str(phone)
save_json(self.contact, self.file)
else:
print(f"{name} already exist")
def view_contacts(self):
if not self.contact:
print("Contacts are empty")
else:
for n, (name, phone) in enumerate(self.contact.items(), 1):
print(f"{n}. Name: {name}, Phone: {phone}")
def remove_contact(self, name):
if name in self.contact:
del self.contact[name]
save_json(self.contact, self.file)
else:
print(f"Contact '{name}' not found.")
def update_contact(self, name, new_phone):
if name in self.contact:
self.contact[name] = str(new_phone)
save_json(self.contact, self.file)
else:
print(f"Contact '{name}' not found.")
# test's
cb = ContactBook()
cb.add_contact("Jisan", 88987)
cb.add_contact("Sonnic", 88323)
cb.add_contact("Stranger", 99733)
cb.view_contacts()
cb.remove_contact("Sonnic")
cb.view_contacts()
cb.update_contact("Jisan", 99878)
cb.view_contacts()