-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday23_speakPython12.py
More file actions
161 lines (113 loc) Β· 3.46 KB
/
Copy pathday23_speakPython12.py
File metadata and controls
161 lines (113 loc) Β· 3.46 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
"""π§ **Day 23 β Speak Python 12**
π **Phase 1: Data Structure Boost**
ΰ¦ΰ¦ΰ¦ΰ§ΰ¦° ফΰ§ΰ¦ΰ¦Ύΰ¦Έ:
* Sorting & Searching
* Nested Dictionary & List
* Dictionary + Set Combo
* Micro Project"""
"""β
Problem 1: Find Maximum & Minimum in a List
π **Task:**
Write a function that takes a list of numbers and returns both the maximum and minimum.
π **Example:**
```python
find_max_min([3, 7, 1, 9, 2]) β (9, 1)
find_max_min([-5, 10, 0]) β (10, -5)
```
π‘ **Hint:** Use `max()` and `min()` or manual loop.
"""
def find_max_min(lst):
return (max(lst), min(lst))
print(find_max_min([3, 7, 1, 9, 2]))
print(find_max_min([-5, 10, 0]))
"""β
Problem 2: Count Occurrences of Each Number
π **Task:**
Write a function that counts how many times each number appears in a list.
π **Example:**
```python
count_occurrences([1, 2, 2, 3, 1, 4, 2])
β {1: 2, 2: 3, 3: 1, 4: 1}
```
π‘ **Hint:** Use dictionary to store counts."""
def count_occurrences(lst):
frq = {}
for n in lst:
frq[n] = frq.get(n, 0) + 1
return frq
print(count_occurrences([1, 2, 2, 3, 1, 4, 2]))
print(count_occurrences([81, 82, 72, 3, 81, 44, 82]))
"""β
Problem 3: Nested Dictionary β Student Records
π **Task:**
You have student data stored in a nested dictionary like this:
```python
students = {
"Jisan": {"math": 85, "english": 78},
"Rahim": {"math": 90, "english": 82},
}
```
π Write a function `get_score(name, subject)` that returns the score of the student in that subject.
π **Example:**
```python
get_score("Jisan", "math") β 85
get_score("Rahim", "english") β 82
get_score("Hasan", "math") β "Not Found"
```
"""
students = {
"Jisan": {"math": 85, "english": 78},
"Rahim": {"math": 90, "english": 82},
}
def get_score(name, subject):
if name in students:
return students[name].get(subject, "Not Found")
return "Not Found"
print(get_score("Jisan", "math"))
print(get_score("Rahim", "english"))
print(get_score("Hasan", "math"))
"""β
Problem 4: Set + Dictionary Combo β Unique Words Counter
π **Task:**
Write a function that counts how many **unique words** are in a given sentence.
π **Example:**
```python
unique_word_count("I love Python because I love coding")
β {'I': 1, 'love': 2, 'Python': 1, 'because': 1, 'coding': 1}
```
π‘ **Hint:** Split sentence β use set to find uniques β dictionary to count."""
def unique_word_count(s):
words = s.split(" ")
count = {}
for w in words:
count[w] = count.get(w, 0) + 1
return count
print(unique_word_count("I love Python because I love coding"))
"""βοΈ **Micro Project β To-Do List Manager (Data Structure Version)**
π **Task:**
Build a simple **To-Do List Manager** where tasks are stored in a list/dictionary.
π Functions:
1. `add_task(task)` β Add a task
2. `remove_task(task)` β Remove a task
3. `view_tasks()` β Show all tasks
π **Example:**
```python
add_task("Study Python")
add_task("Do Homework")
view_tasks() β ["Study Python", "Do Homework"]
remove_task("Do Homework")
view_tasks() β ["Study Python"]
```
π― **Focus:** CRUD idea with list/dictionary"""
To_Do = []
def add_task(task):
To_Do.append(task)
def remove_task(task):
if task in To_Do:
To_Do.remove(task)
else:
print("Not found!")
def view_tasks():
return To_Do
# test
add_task("Study Python")
add_task("Do Homework")
print(view_tasks())
remove_task("Do Homework")
print(view_tasks())