|
9 | 9 | }, |
10 | 10 | { |
11 | 11 | "cell_type": "code", |
12 | | - "execution_count": 31, |
| 12 | + "execution_count": 4, |
13 | 13 | "metadata": {}, |
14 | 14 | "outputs": [], |
15 | 15 | "source": [ |
|
24 | 24 | }, |
25 | 25 | { |
26 | 26 | "cell_type": "code", |
27 | | - "execution_count": 32, |
| 27 | + "execution_count": 5, |
28 | 28 | "metadata": {}, |
29 | 29 | "outputs": [], |
30 | 30 | "source": [ |
|
33 | 33 | " pass" |
34 | 34 | ] |
35 | 35 | }, |
| 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 | + }, |
36 | 65 | { |
37 | 66 | "cell_type": "code", |
38 | | - "execution_count": 33, |
| 67 | + "execution_count": 7, |
39 | 68 | "metadata": {}, |
40 | 69 | "outputs": [ |
41 | 70 | { |
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 | + ] |
50 | 76 | } |
51 | 77 | ], |
52 | 78 | "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" |
56 | 84 | ] |
57 | 85 | }, |
58 | 86 | { |
59 | 87 | "cell_type": "code", |
60 | | - "execution_count": 34, |
| 88 | + "execution_count": 8, |
61 | 89 | "metadata": {}, |
62 | 90 | "outputs": [], |
63 | 91 | "source": [ |
|
66 | 94 | " pass" |
67 | 95 | ] |
68 | 96 | }, |
| 97 | + { |
| 98 | + "cell_type": "markdown", |
| 99 | + "metadata": {}, |
| 100 | + "source": [ |
| 101 | + "**Standard loop**" |
| 102 | + ] |
| 103 | + }, |
69 | 104 | { |
70 | 105 | "cell_type": "code", |
71 | | - "execution_count": 74, |
| 106 | + "execution_count": 9, |
72 | 107 | "metadata": {}, |
73 | 108 | "outputs": [ |
74 | 109 | { |
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 | + ] |
83 | 116 | } |
84 | 117 | ], |
85 | 118 | "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())" |
91 | 157 | ] |
92 | 158 | }, |
93 | 159 | { |
94 | 160 | "cell_type": "code", |
95 | | - "execution_count": 75, |
| 161 | + "execution_count": 11, |
96 | 162 | "metadata": {}, |
97 | 163 | "outputs": [], |
98 | 164 | "source": [ |
|
103 | 169 | " pass" |
104 | 170 | ] |
105 | 171 | }, |
| 172 | + { |
| 173 | + "cell_type": "markdown", |
| 174 | + "metadata": {}, |
| 175 | + "source": [ |
| 176 | + "**Standard Solution**" |
| 177 | + ] |
| 178 | + }, |
106 | 179 | { |
107 | 180 | "cell_type": "code", |
108 | | - "execution_count": 76, |
| 181 | + "execution_count": 15, |
109 | 182 | "metadata": {}, |
110 | 183 | "outputs": [ |
111 | 184 | { |
112 | 185 | "name": "stdout", |
113 | 186 | "output_type": "stream", |
114 | 187 | "text": [ |
| 188 | + "8.89 µs ± 496 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n", |
115 | 189 | "['Trailblazer', 'Trailhawk']\n" |
116 | 190 | ] |
117 | 191 | } |
118 | 192 | ], |
119 | 193 | "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())" |
130 | 206 | ] |
131 | 207 | }, |
132 | 208 | { |
133 | | - "cell_type": "code", |
134 | | - "execution_count": 77, |
| 209 | + "cell_type": "markdown", |
135 | 210 | "metadata": {}, |
136 | | - "outputs": [], |
137 | 211 | "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**" |
141 | 213 | ] |
142 | 214 | }, |
143 | 215 | { |
144 | 216 | "cell_type": "code", |
145 | | - "execution_count": 69, |
| 217 | + "execution_count": 13, |
146 | 218 | "metadata": {}, |
147 | 219 | "outputs": [ |
148 | 220 | { |
149 | 221 | "name": "stdout", |
150 | 222 | "output_type": "stream", |
151 | 223 | "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" |
153 | 226 | ] |
154 | 227 | } |
155 | 228 | ], |
| 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": [], |
156 | 257 | "source": [ |
157 | 258 | "new_cars = {}\n", |
158 | 259 | "for maker in cars:\n", |
|
164 | 265 | }, |
165 | 266 | { |
166 | 267 | "cell_type": "code", |
167 | | - "execution_count": 95, |
| 268 | + "execution_count": null, |
168 | 269 | "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": [], |
181 | 271 | "source": [ |
182 | 272 | "def get_all_matching_models(grep='trail'):\n", |
183 | 273 | " search_result = []\n", |
|
196 | 286 | "execution_count": null, |
197 | 287 | "metadata": {}, |
198 | 288 | "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 | + ] |
200 | 298 | }, |
201 | 299 | { |
202 | 300 | "cell_type": "code", |
|
0 commit comments