-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday22_speakPython11.py
More file actions
148 lines (107 loc) Β· 3.51 KB
/
Copy pathday22_speakPython11.py
File metadata and controls
148 lines (107 loc) Β· 3.51 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
"""π§ **Day 22 β Speak Python 11**
Focus Areas:
* Sets & Operations
* Dictionary + List Combo
* Data Filtering
* Micro Project with Real-Life Idea"""
"""β
Problem 1: Unique Elements with Sets
π **Task:**
Write a function that takes two lists and returns the **unique common elements** between them.
π **Example:**
```python
common_unique([1, 2, 3, 4], [3, 4, 4, 5, 6]) β {3, 4}
common_unique(["apple", "banana"], ["banana", "orange"]) β {"banana"}
```
π‘ **Hint:** Use `set()` and intersection `&`.
β±οΈ **Target Time:** 10 minutes"""
# def common_unique(lst1, lst2):
# s1 = set(lst1)
# s2 = set(lst2)
# return s1.intersection(s2)
# print(common_unique([1, 2, 3, 4], [3, 4, 4, 5, 6]))
# print(common_unique(["apple", "banana"], ["banana", "orange"]))
"""π§ͺ Problem 2: Invert a Dictionary
π **Task:**
Write a function that inverts a dictionary: keys become values and values become keys.
π **Example:**
```python
invert_dict({"a": 1, "b": 2, "c": 3}) β {1: "a", 2: "b", 3: "c"}
invert_dict({"Jisan": 85, "Rahim": 90}) β {85: "Jisan", 90: "Rahim"}
```
π― **Focus:** Key-value flipping.
π‘ **Hint:** Loop or dictionary comprehension.
β±οΈ **Target Time:** 12 minutes"""
# def invert_dict(dict):
# invert_dict = {}
# for d in dict:
# invert_dict.update({dict.get(d) : d})
# return invert_dict
# print(invert_dict({"a": 1, "b": 2, "c": 3}))
# print(invert_dict({"Jisan": 85, "Rahim": 90}))
"""βοΈ Problem 3: Micro Project β Library System
π **Task:**
Create a mini library system using dictionary. Books are keys, availability (`True`/`False`) is value.
π Functions:
1. `add_book(book)` β Add a book to library (default available).
2. `borrow_book(book)` β If available, mark as borrowed.
3. `return_book(book)` β Mark as available again.
4. `check_availability(book)` β Return status.
π **Example:**
```python
add_book("Python Basics")
borrow_book("Python Basics")
check_availability("Python Basics") β False
return_book("Python Basics")
check_availability("Python Basics") β True
```
π― **Focus:** CRUD with dictionaries.
β±οΈ **Target Time:** 20 minutes"""
library = {}
def add_book(book):
library.update({book : True})
def borrow_book(book):
if book in library:
library.update({book : False})
else:
print(f"{book} Not available!")
def return_book(book):
if book in library:
library[book] = True
else:
print(f"{book} Not in library!")
def check_availability(book):
if book in library:
return library.get(book)
return False
# run function
# add_book("book")
# print(library)
# borrow_book("book")
# borrow_book("bo")
# print(check_availability("book"))
# return_book("book")
# print(library)
# print(check_availability("book"))
# add_book("Python Basics")
# borrow_book("Python Basics")
# print(check_availability("Python Basics")) #β False
# return_book("Python Basics")
# print(check_availability("Python Basics")) #β True
# print(library)
"""β
Bonus Challenge (Optional)
π **Task:**
Write a function that finds the **second largest number** in a list.
π **Example:**
```python
second_largest([10, 20, 4, 45, 99]) β 45
second_largest([5, 5, 5]) β None
```
π‘ **Hint:** Use `set()` to remove duplicates, then sort."""
# def second_largest(lst):
# unique = list(set(lst))
# if len(unique) < 2:
# return None
# unique.sort()
# return unique[-2]
# print(second_largest([10, 20, 4, 45, 99]))
# print(second_largest([5, 5, 5]))