Skip to content

Commit 2b3d763

Browse files
committed
Lab | Map, Filter, Reduce
1 parent b968e9c commit 2b3d763

File tree

1 file changed

+153
-48
lines changed

1 file changed

+153
-48
lines changed

your-code/main.ipynb

Lines changed: 153 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
},
1313
{
1414
"cell_type": "code",
15-
"execution_count": null,
15+
"execution_count": 2,
1616
"metadata": {},
1717
"outputs": [],
1818
"source": [
@@ -35,7 +35,7 @@
3535
},
3636
{
3737
"cell_type": "code",
38-
"execution_count": null,
38+
"execution_count": 3,
3939
"metadata": {},
4040
"outputs": [],
4141
"source": [
@@ -48,9 +48,20 @@
4848
},
4949
{
5050
"cell_type": "code",
51-
"execution_count": null,
51+
"execution_count": 4,
5252
"metadata": {},
53-
"outputs": [],
53+
"outputs": [
54+
{
55+
"data": {
56+
"text/plain": [
57+
"13637"
58+
]
59+
},
60+
"execution_count": 4,
61+
"metadata": {},
62+
"output_type": "execute_result"
63+
}
64+
],
5465
"source": [
5566
"len(prophet)"
5667
]
@@ -66,11 +77,12 @@
6677
},
6778
{
6879
"cell_type": "code",
69-
"execution_count": null,
80+
"execution_count": 5,
7081
"metadata": {},
7182
"outputs": [],
7283
"source": [
73-
"# your code here"
84+
"# your code here\n",
85+
"prophet = prophet[568:len(prophet)]\n"
7486
]
7587
},
7688
{
@@ -82,11 +94,34 @@
8294
},
8395
{
8496
"cell_type": "code",
85-
"execution_count": null,
97+
"execution_count": 6,
8698
"metadata": {},
87-
"outputs": [],
88-
"source": [
89-
"# your code here"
99+
"outputs": [
100+
{
101+
"name": "stdout",
102+
"output_type": "stream",
103+
"text": [
104+
"PROPHET\n",
105+
"\n",
106+
"|Almustafa,\n",
107+
"the{7}\n",
108+
"chosen\n",
109+
"and\n",
110+
"the\n",
111+
"beloved,\n",
112+
"who\n",
113+
"was\n",
114+
"a\n",
115+
"dawn\n",
116+
"unto\n",
117+
"his\n"
118+
]
119+
}
120+
],
121+
"source": [
122+
"# your code here\n",
123+
"for i in range(0,11):\n",
124+
" print(prophet[i])"
90125
]
91126
},
92127
{
@@ -100,7 +135,7 @@
100135
},
101136
{
102137
"cell_type": "code",
103-
"execution_count": null,
138+
"execution_count": 7,
104139
"metadata": {},
105140
"outputs": [],
106141
"source": [
@@ -114,16 +149,29 @@
114149
" Output: 'the'\n",
115150
" '''\n",
116151
" \n",
117-
" # your code here"
152+
" # your code here\n",
153+
" return x.split('{')[0]"
118154
]
119155
},
120156
{
121157
"cell_type": "code",
122-
"execution_count": null,
158+
"execution_count": 8,
123159
"metadata": {},
124-
"outputs": [],
160+
"outputs": [
161+
{
162+
"data": {
163+
"text/plain": [
164+
"'the'"
165+
]
166+
},
167+
"execution_count": 8,
168+
"metadata": {},
169+
"output_type": "execute_result"
170+
}
171+
],
125172
"source": [
126-
"# your code here"
173+
"# your code here\n",
174+
"reference('the{7}')"
127175
]
128176
},
129177
{
@@ -135,11 +183,12 @@
135183
},
136184
{
137185
"cell_type": "code",
138-
"execution_count": null,
186+
"execution_count": 9,
139187
"metadata": {},
140188
"outputs": [],
141189
"source": [
142-
"# your code here"
190+
"# your code here\n",
191+
"prophet_reference = map(reference, prophet)"
143192
]
144193
},
145194
{
@@ -151,7 +200,7 @@
151200
},
152201
{
153202
"cell_type": "code",
154-
"execution_count": null,
203+
"execution_count": 10,
155204
"metadata": {},
156205
"outputs": [],
157206
"source": [
@@ -164,6 +213,7 @@
164213
" Input: 'the\\nbeloved'\n",
165214
" Output: ['the', 'beloved']\n",
166215
" '''\n",
216+
" return x.split('\\n')\n",
167217
" \n",
168218
" # your code here"
169219
]
@@ -177,13 +227,14 @@
177227
},
178228
{
179229
"cell_type": "code",
180-
"execution_count": null,
230+
"execution_count": 11,
181231
"metadata": {
182232
"scrolled": true
183233
},
184234
"outputs": [],
185235
"source": [
186-
"# your code here"
236+
"# your code here\n",
237+
"prophet_line = map(line_break, prophet_reference)"
187238
]
188239
},
189240
{
@@ -195,21 +246,16 @@
195246
},
196247
{
197248
"cell_type": "code",
198-
"execution_count": null,
199-
"metadata": {},
200-
"outputs": [],
201-
"source": [
202-
"prophet_flat = [i for sub in prophet_line for i in sub]\n",
203-
"prophet_flat"
204-
]
205-
},
206-
{
207-
"cell_type": "code",
208-
"execution_count": null,
249+
"execution_count": 12,
209250
"metadata": {},
210251
"outputs": [],
211252
"source": [
212-
"# your code here"
253+
"# your code here\n",
254+
"prophet_flat = [\n",
255+
" el\n",
256+
" for row in prophet_line\n",
257+
" for el in row\n",
258+
"]"
213259
]
214260
},
215261
{
@@ -223,7 +269,7 @@
223269
},
224270
{
225271
"cell_type": "code",
226-
"execution_count": null,
272+
"execution_count": 13,
227273
"metadata": {},
228274
"outputs": [],
229275
"source": [
@@ -244,7 +290,11 @@
244290
" \n",
245291
" word_list = ['and', 'the', 'a', 'an']\n",
246292
" \n",
247-
" # your code here"
293+
" # your code here\n",
294+
" if x not in word_list:\n",
295+
" return True\n",
296+
" else:\n",
297+
" return False"
248298
]
249299
},
250300
{
@@ -256,13 +306,14 @@
256306
},
257307
{
258308
"cell_type": "code",
259-
"execution_count": null,
309+
"execution_count": 14,
260310
"metadata": {
261311
"scrolled": true
262312
},
263313
"outputs": [],
264314
"source": [
265-
"# your code here"
315+
"# your code here\n",
316+
"prophet_filter = filter(word_filter, prophet_flat)"
266317
]
267318
},
268319
{
@@ -276,15 +327,42 @@
276327
},
277328
{
278329
"cell_type": "code",
279-
"execution_count": null,
330+
"execution_count": 15,
280331
"metadata": {},
281332
"outputs": [],
282333
"source": [
283334
"def word_filter_case(x):\n",
284-
" \n",
335+
" '''\n",
336+
" Input: A string\n",
337+
" Output: True if the word is not in the specified list \n",
338+
" and False if the word is in the list.\n",
339+
" \n",
340+
" Example:\n",
341+
" word list = ['And', 'the']\n",
342+
" Input: 'and'\n",
343+
" Output: False\n",
344+
" \n",
345+
" Input: 'John'\n",
346+
" Output: True\n",
347+
" '''\n",
348+
" \n",
285349
" word_list = ['and', 'the', 'a', 'an']\n",
350+
" x = x.lower()\n",
286351
" \n",
287-
" # your code here"
352+
" # your code here\n",
353+
" if x not in word_list:\n",
354+
" return True\n",
355+
" else:\n",
356+
" return False"
357+
]
358+
},
359+
{
360+
"cell_type": "code",
361+
"execution_count": 16,
362+
"metadata": {},
363+
"outputs": [],
364+
"source": [
365+
"prophet_flat_filter = filter(word_filter_case, prophet_flat)"
288366
]
289367
},
290368
{
@@ -300,7 +378,7 @@
300378
},
301379
{
302380
"cell_type": "code",
303-
"execution_count": null,
381+
"execution_count": 17,
304382
"metadata": {},
305383
"outputs": [],
306384
"source": [
@@ -314,18 +392,31 @@
314392
" Output: 'John Smith'\n",
315393
" '''\n",
316394
" \n",
317-
" # your code here"
395+
" # your code here\n",
396+
" return f\"{a} {b}\""
318397
]
319398
},
320399
{
321400
"cell_type": "code",
322-
"execution_count": null,
401+
"execution_count": 18,
323402
"metadata": {
324403
"scrolled": true
325404
},
326-
"outputs": [],
405+
"outputs": [
406+
{
407+
"data": {
408+
"text/plain": [
409+
"'test 123'"
410+
]
411+
},
412+
"execution_count": 18,
413+
"metadata": {},
414+
"output_type": "execute_result"
415+
}
416+
],
327417
"source": [
328-
"# your code here"
418+
"# your code here\n",
419+
"concat_space('test', '123')"
329420
]
330421
},
331422
{
@@ -337,17 +428,31 @@
337428
},
338429
{
339430
"cell_type": "code",
340-
"execution_count": null,
431+
"execution_count": 19,
341432
"metadata": {},
342433
"outputs": [],
343434
"source": [
344-
"# your code here"
435+
"# your code here\n",
436+
"from functools import reduce\n",
437+
"\n",
438+
"def concat_space(a, b):\n",
439+
" '''\n",
440+
" Input:Two strings\n",
441+
" Output: A single string separated by a space\n",
442+
" \n",
443+
" Example:\n",
444+
" Input: 'John', 'Smith'\n",
445+
" Output: 'John Smith'\n",
446+
" '''\n",
447+
" return f\"{a} {b}\"\n",
448+
"\n",
449+
"prophet_string = reduce(concat_space, prophet_filter)"
345450
]
346451
}
347452
],
348453
"metadata": {
349454
"kernelspec": {
350-
"display_name": "Python 3 (ipykernel)",
455+
"display_name": "Python 3",
351456
"language": "python",
352457
"name": "python3"
353458
},
@@ -361,7 +466,7 @@
361466
"name": "python",
362467
"nbconvert_exporter": "python",
363468
"pygments_lexer": "ipython3",
364-
"version": "3.9.13"
469+
"version": "3.13.7"
365470
}
366471
},
367472
"nbformat": 4,

0 commit comments

Comments
 (0)