-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday24_speakPython13.py
More file actions
146 lines (103 loc) Β· 3.42 KB
/
Copy pathday24_speakPython13.py
File metadata and controls
146 lines (103 loc) Β· 3.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
"""π§ **Day 24 β Speak Python 13**
π **Phase 2: Data Structure Pro Pack**
**Focus Areas:**
* Custom Sorting
* Searching Algorithms
* Nested List + Dictionary Combo
* Micro Project (Leaderboard style)
"""
"""β
Problem 1: Sort Students by Score
π **Task:**
You are given a list of students where each student is represented as a tuple `(name, score)`.
Write a function that sorts the students by their score in **descending order**.
π **Example:**
```python
students = [("Jisan", 85), ("Rahim", 92), ("Karim", 78)]
sort_students(students)
β [("Rahim", 92), ("Jisan", 85), ("Karim", 78)]
```
π‘ **Hint:** Use `sorted(list, key=..., reverse=True)`."""
def sort_students(students):
return sorted(students, key=lambda x: x[1], reverse=True)
students = [("Jisan", 85), ("Rahim", 92), ("Karim", 78)]
print(sort_students(students))
"""β
Problem 2: Linear Search
π **Task:**
Write a function that searches for a number in a list and returns its index if found, otherwise return `-1`.
π **Example:**
```python
linear_search([10, 20, 30, 40], 30) β 2
linear_search([5, 6, 7], 10) β -1
```
π‘ **Hint:** Use a loop with index check.
"""
def linear_search(lst, n):
for i, item in enumerate(lst):
if item == n:
return i
return -1
print(linear_search([10, 20, 30, 40], 30))
print(linear_search([10, 20, 30, 40], 40))
print(linear_search([5, 6, 7], 10))
"""β
Problem 3: Nested Data β Employee Manager
π **Task:**
You have employee data stored in a nested dictionary:
```python
employees = {
"101": {"name": "Jisan", "role": "Developer", "salary": 50000},
"102": {"name": "Rahim", "role": "Designer", "salary": 40000},
}
```
π Write a function `get_employee(emp_id, field)` that returns employee information.
π **Example:**
```python
get_employee("101", "name") β "Jisan"
get_employee("102", "salary") β 40000
get_employee("103", "role") β "Not Found"
"""
employees = {
"101": {"name": "Jisan", "role": "Developer", "salary": 50000},
"102": {"name": "Rahim", "role": "Designer", "salary": 40000},
}
def get_employee(emp_id, field):
if emp_id in employees:
return employees[emp_id].get(field, "Not Found")
return "Not Found"
print(get_employee("101", "name"))
print(get_employee("102", "salary"))
print(get_employee("103", "role"))
"""βοΈ Micro Project β Leaderboard System
π **Task:**
Build a simple leaderboard using dictionary/list.
π Functions:
1. `add_player(name, score)` β Add a new player.
2. `update_score(name, score)` β Update an existing playerβs score.
3. `top_players(n)` β Return the top **N** players sorted by score.
π **Example:**
```python
add_player("Jisan", 85)
add_player("Rahim", 95)
add_player("Karim", 70)
top_players(2) β [("Rahim", 95), ("Jisan", 85)]"""
leaderboard = []
def add_player(name, score):
leaderboard.append((name, score))
def top_players(n):
return sorted(leaderboard, key=lambda x: x[1], reverse=True)[:n]
def update_score(name, score):
global leaderboard
leaderboard = [(n, s) if n != name else (name, score) for (n, s) in leaderboard]
def remove_player(name):
for (n, s) in leaderboard:
if n == name:
leaderboard.remove((n, s))
add_player("Jisan", 85)
add_player("Rahim", 95)
add_player("Karim", 70)
print(leaderboard)
print(top_players(1))
update_score("Karim", 99)
print(leaderboard)
print(top_players(2))
remove_player("Karim")
print(leaderboard)