Skip to content

Commit 922f754

Browse files
committed
updated Byte21 to use comprehensions
1 parent 15a1060 commit 922f754

1 file changed

Lines changed: 161 additions & 63 deletions

File tree

days/07-09-data-structures/code/Byte21.ipynb

Lines changed: 161 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
},
1010
{
1111
"cell_type": "code",
12-
"execution_count": 31,
12+
"execution_count": 4,
1313
"metadata": {},
1414
"outputs": [],
1515
"source": [
@@ -24,7 +24,7 @@
2424
},
2525
{
2626
"cell_type": "code",
27-
"execution_count": 32,
27+
"execution_count": 5,
2828
"metadata": {},
2929
"outputs": [],
3030
"source": [
@@ -33,31 +33,59 @@
3333
" pass"
3434
]
3535
},
36+
{
37+
"cell_type": "markdown",
38+
"metadata": {},
39+
"source": [
40+
"**Standard solution**"
41+
]
42+
},
43+
{
44+
"cell_type": "code",
45+
"execution_count": 6,
46+
"metadata": {},
47+
"outputs": [
48+
{
49+
"name": "stdout",
50+
"output_type": "stream",
51+
"text": [
52+
"440 ns ± 29.6 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)\n"
53+
]
54+
}
55+
],
56+
"source": [
57+
"def get_all_jeeps():\n",
58+
" jeeps = cars['Jeep']\n",
59+
" models = ', '.join(jeeps)\n",
60+
" return models\n",
61+
"\n",
62+
"%timeit get_all_jeeps()"
63+
]
64+
},
3665
{
3766
"cell_type": "code",
38-
"execution_count": 33,
67+
"execution_count": 7,
3968
"metadata": {},
4069
"outputs": [
4170
{
42-
"data": {
43-
"text/plain": [
44-
"'Grand Cherokee, Cherokee, Trailhawk, Trackhawk'"
45-
]
46-
},
47-
"execution_count": 33,
48-
"metadata": {},
49-
"output_type": "execute_result"
71+
"name": "stdout",
72+
"output_type": "stream",
73+
"text": [
74+
"394 ns ± 13.3 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)\n"
75+
]
5076
}
5177
],
5278
"source": [
53-
"jeeps = cars['Jeep']\n",
54-
"models = ', '.join(jeeps)\n",
55-
"print(models)"
79+
"def get_all_jeeps():\n",
80+
" return ', '.join(cars['Jeep'])\n",
81+
"\n",
82+
"# print(get_all_jeeps())\n",
83+
"%timeit get_all_jeeps()\n"
5684
]
5785
},
5886
{
5987
"cell_type": "code",
60-
"execution_count": 34,
88+
"execution_count": 8,
6189
"metadata": {},
6290
"outputs": [],
6391
"source": [
@@ -66,33 +94,71 @@
6694
" pass"
6795
]
6896
},
97+
{
98+
"cell_type": "markdown",
99+
"metadata": {},
100+
"source": [
101+
"**Standard loop**"
102+
]
103+
},
69104
{
70105
"cell_type": "code",
71-
"execution_count": 74,
106+
"execution_count": 9,
72107
"metadata": {},
73108
"outputs": [
74109
{
75-
"data": {
76-
"text/plain": [
77-
"['Fairlane', 'Barina', '350Z', 'Accord', 'Cherokee']"
78-
]
79-
},
80-
"execution_count": 74,
81-
"metadata": {},
82-
"output_type": "execute_result"
110+
"name": "stdout",
111+
"output_type": "stream",
112+
"text": [
113+
"1.14 µs ± 24 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)\n",
114+
"['Falcon', 'Commodore', 'Maxima', 'Civic', 'Grand Cherokee']\n"
115+
]
83116
}
84117
],
85118
"source": [
86-
"firstmodels = []\n",
87-
"for maker in cars:\n",
88-
" models = cars[maker]\n",
89-
" firstmodels.append(models[0])\n",
90-
"firstmodels"
119+
"def get_first_model_each_manufacturer():\n",
120+
" firstmodels = []\n",
121+
" for maker in cars:\n",
122+
" models = cars[maker]\n",
123+
" firstmodels.append(models[0])\n",
124+
" return firstmodels\n",
125+
"\n",
126+
"%timeit get_first_model_each_manufacturer()\n",
127+
"print(get_first_model_each_manufacturer())"
128+
]
129+
},
130+
{
131+
"cell_type": "markdown",
132+
"metadata": {},
133+
"source": [
134+
"**Solution using list comprehenson**"
135+
]
136+
},
137+
{
138+
"cell_type": "code",
139+
"execution_count": 10,
140+
"metadata": {},
141+
"outputs": [
142+
{
143+
"name": "stdout",
144+
"output_type": "stream",
145+
"text": [
146+
"829 ns ± 67.3 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)\n",
147+
"['Falcon', 'Commodore', 'Maxima', 'Civic', 'Grand Cherokee']\n"
148+
]
149+
}
150+
],
151+
"source": [
152+
"def get_first_model_each_manufacturer():\n",
153+
" return [models[0] for models in cars.values()]\n",
154+
"\n",
155+
"%timeit get_first_model_each_manufacturer()\n",
156+
"print(get_first_model_each_manufacturer())"
91157
]
92158
},
93159
{
94160
"cell_type": "code",
95-
"execution_count": 75,
161+
"execution_count": 11,
96162
"metadata": {},
97163
"outputs": [],
98164
"source": [
@@ -103,56 +169,91 @@
103169
" pass"
104170
]
105171
},
172+
{
173+
"cell_type": "markdown",
174+
"metadata": {},
175+
"source": [
176+
"**Standard Solution**"
177+
]
178+
},
106179
{
107180
"cell_type": "code",
108-
"execution_count": 76,
181+
"execution_count": 15,
109182
"metadata": {},
110183
"outputs": [
111184
{
112185
"name": "stdout",
113186
"output_type": "stream",
114187
"text": [
188+
"8.89 µs ± 496 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n",
115189
"['Trailblazer', 'Trailhawk']\n"
116190
]
117191
}
118192
],
119193
"source": [
120-
"grep='trail'\n",
121-
"search_result = []\n",
122-
"search_string = grep\n",
123-
"for models in cars.values():\n",
124-
" for model in models:\n",
125-
" if search_string.lower() in model.lower():\n",
126-
" search_result.append(model)\n",
127-
" search_result.sort()\n",
128-
"print(search_result)\n",
129-
" "
194+
"def get_all_matching_models(grep='trail'):\n",
195+
" search_result = []\n",
196+
" search_string = grep\n",
197+
" for models in cars.values():\n",
198+
" for model in models:\n",
199+
" if search_string.lower() in model.lower():\n",
200+
" search_result.append(model)\n",
201+
" search_result.sort()\n",
202+
" return search_result\n",
203+
"\n",
204+
"%timeit get_all_matching_models()\n",
205+
"print(get_all_matching_models())"
130206
]
131207
},
132208
{
133-
"cell_type": "code",
134-
"execution_count": 77,
209+
"cell_type": "markdown",
135210
"metadata": {},
136-
"outputs": [],
137211
"source": [
138-
"def sort_car_models():\n",
139-
" \"\"\"sort the car models (values) and return the resulting cars dict\"\"\"\n",
140-
" pass"
212+
"**Using List Comprehension**"
141213
]
142214
},
143215
{
144216
"cell_type": "code",
145-
"execution_count": 69,
217+
"execution_count": 13,
146218
"metadata": {},
147219
"outputs": [
148220
{
149221
"name": "stdout",
150222
"output_type": "stream",
151223
"text": [
152-
"{'Ford': ['Fairlane', 'Falcon', 'Festiva', 'Focus'], 'Holden': ['Barina', 'Captiva', 'Commodore', 'Trailblazer'], 'Nissan': ['350Z', 'Maxima', 'Navara', 'Pulsar'], 'Honda': ['Accord', 'Civic', 'Jazz', 'Odyssey'], 'Jeep': ['Cherokee', 'Grand Cherokee', 'Trackhawk', 'Trailhawk']}\n"
224+
"5.57 µs ± 567 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n",
225+
"['Trailblazer', 'Trailhawk']\n"
153226
]
154227
}
155228
],
229+
"source": [
230+
"def get_all_matching_models(grep='trail'):\n",
231+
" grep = grep.lower()\n",
232+
" models = sum(cars.values(), []) # flatten list of lists\n",
233+
" matching_models = [model for model in models\n",
234+
" if grep in model.lower()]\n",
235+
" return sorted(matching_models)\n",
236+
"\n",
237+
"%timeit get_all_matching_models()\n",
238+
"print(get_all_matching_models())"
239+
]
240+
},
241+
{
242+
"cell_type": "code",
243+
"execution_count": null,
244+
"metadata": {},
245+
"outputs": [],
246+
"source": [
247+
"def sort_car_models():\n",
248+
" \"\"\"sort the car models (values) and return the resulting cars dict\"\"\"\n",
249+
" pass"
250+
]
251+
},
252+
{
253+
"cell_type": "code",
254+
"execution_count": null,
255+
"metadata": {},
256+
"outputs": [],
156257
"source": [
157258
"new_cars = {}\n",
158259
"for maker in cars:\n",
@@ -164,20 +265,9 @@
164265
},
165266
{
166267
"cell_type": "code",
167-
"execution_count": 95,
268+
"execution_count": null,
168269
"metadata": {},
169-
"outputs": [
170-
{
171-
"data": {
172-
"text/plain": [
173-
"['Accord', 'Commodore', 'Falcon']"
174-
]
175-
},
176-
"execution_count": 95,
177-
"metadata": {},
178-
"output_type": "execute_result"
179-
}
180-
],
270+
"outputs": [],
181271
"source": [
182272
"def get_all_matching_models(grep='trail'):\n",
183273
" search_result = []\n",
@@ -196,7 +286,15 @@
196286
"execution_count": null,
197287
"metadata": {},
198288
"outputs": [],
199-
"source": []
289+
"source": [
290+
"def get_all_matching_models(grep='trail'):\n",
291+
" search_result = []\n",
292+
" for models in cars.values():\n",
293+
" search_result = [model for model in models if (grep.lower() == model.lower())]\n",
294+
" return search_result\n",
295+
"\n",
296+
"get_all_matching_models('CO')"
297+
]
200298
},
201299
{
202300
"cell_type": "code",

0 commit comments

Comments
 (0)