Skip to content

Commit fe2d66d

Browse files
Refactor solutions for Acitvities 3-4, of Part 2 of Chapter 0.
1 parent 9dfca32 commit fe2d66d

File tree

1 file changed

+71
-21
lines changed

1 file changed

+71
-21
lines changed

chapter0-exercises/chapter_0_part2.ipynb

Lines changed: 71 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -256,11 +256,11 @@
256256
" numbers = nums.copy()\n",
257257
" # B: mark the duplicates\n",
258258
" for index in range(len(nums)):\n",
259-
" # if this number appears again, then it's a duplicate\n",
259+
" # C: if this number appears again, then it's a duplicate\n",
260260
" rest_of_list = numbers[index+1:]\n",
261261
" if numbers[index] in rest_of_list:\n",
262262
" numbers[index] = 0 \n",
263-
" # C: return all non-duplicates\n",
263+
" # D: return all non-duplicates\n",
264264
" return [num for num in numbers if num != 0]\n",
265265
"\n",
266266
"print(remove_duplicates_3(nums))"
@@ -283,8 +283,8 @@
283283
"id": "DjhcUMXvw8sB"
284284
},
285285
"source": [
286-
"# TODO: explain above that this solution is just like above, except we instead\n",
287-
" # just delete the duplicate values from the list "
286+
"TODO: explain above that this solution is just like above, except we instead\n",
287+
" # just delete the duplicate values from the list"
288288
],
289289
"id": "DjhcUMXvw8sB"
290290
},
@@ -309,13 +309,13 @@
309309
" while num_checks < original_length:\n",
310310
" num_checks += 1\n",
311311
" rest_of_list = numbers[index+1:]\n",
312-
" # duplicate found\n",
312+
" # C: duplicate found\n",
313313
" if numbers[index] in rest_of_list:\n",
314314
" del numbers[index]\n",
315-
" # duplicate not found, so move on to the next indx\n",
315+
" # D: duplicate not found, so move on to the next indx\n",
316316
" else:\n",
317317
" index += 1\n",
318-
" # C: return only the non-duplicate numbers\n",
318+
" # E: return only the non-duplicate numbers\n",
319319
" return numbers\n",
320320
"\n",
321321
"print(remove_duplicates_4(nums))"
@@ -386,6 +386,11 @@
386386
},
387387
"source": [
388388
"## Activity: Combine dictionaries\n",
389+
"\n",
390+
"TODO: \n",
391+
" 1. mention that if there are any keys in the second that are already present in the first, then place the corresponding values together into a list, and make that the new value in the first\n",
392+
" 2. again, both the input dictionaries are immutable \n",
393+
"\n",
389394
"- d1 = {'a':10, 'b':20, 'c':30}\n",
390395
"- d2 = {'b': 40, 'd':50}\n",
391396
"- return d = {'a': 10, 'b': [20, 40], 'c': 50, 'd': 50}"
@@ -395,26 +400,34 @@
395400
{
396401
"cell_type": "code",
397402
"metadata": {
403+
"colab": {
404+
"base_uri": "https://localhost:8080/"
405+
},
398406
"id": "sized-congo",
399-
"outputId": "6267b2fe-fd5b-4f7b-9cdf-1e2cc70ac0cd"
407+
"outputId": "a2a26d41-f993-4d08-8595-aceaa1a570b6"
400408
},
401409
"source": [
402410
"def combine_dicts(d1, d2):\n",
403-
" d = d1.copy()\n",
404-
" for key_d2,value_d2 in d2.items():\n",
405-
" if key_d2 in d:\n",
406-
" ls = d[key_d2]\n",
407-
" d[key_d2] = [ls, value_d2]\n",
411+
" # A: init a third dict\n",
412+
" combined = d1.copy()\n",
413+
" # B: add the key-value pairs from the second \n",
414+
" for key_d2, value_d2 in d2.items():\n",
415+
" # C: if an value has already been mapped to this key, include both\n",
416+
" if key_d2 in combined:\n",
417+
" value_d1 = d1[key_d2]\n",
418+
" combined[key_d2] = [value_d1, value_d2]\n",
419+
" # D: otherwise, just add the key-value pair\n",
408420
" else:\n",
409-
" d[key_d2] = value_d2\n",
410-
" return d\n",
421+
" combined[key_d2] = value_d2\n",
422+
" # E: return the new dict\n",
423+
" return combined\n",
411424
"\n",
412425
"d1 = {'a':10, 'b':20, 'c':30}\n",
413426
"d2 = {'b': 40, 'd':50}\n",
414427
"print(combine_dicts(d1, d2))"
415428
],
416429
"id": "sized-congo",
417-
"execution_count": null,
430+
"execution_count": 40,
418431
"outputs": [
419432
{
420433
"output_type": "stream",
@@ -431,30 +444,38 @@
431444
"id": "equal-shock"
432445
},
433446
"source": [
434-
"## Activity: Flatten the given matrix"
447+
"## Activity: Flatten the given matrix\n",
448+
"\n",
449+
"TODO: explain that this means to collect all the elements into a 1D array - they should be in the same order as on would see if your were reading the matrix row-wise"
435450
],
436451
"id": "equal-shock"
437452
},
438453
{
439454
"cell_type": "code",
440455
"metadata": {
456+
"colab": {
457+
"base_uri": "https://localhost:8080/"
458+
},
441459
"id": "soviet-accused",
442-
"outputId": "b24ecf65-3c45-4752-9c3c-c2c7d65da036"
460+
"outputId": "8dcdf69b-d515-4973-c1ec-bd6eb1bcd8d9"
443461
},
444462
"source": [
445463
"matrix = [[8, 2, 3], [9, 1, 9], [5, 4, 1]]\n",
446464
"\n",
447-
"def flatten(matx):\n",
465+
"def flatten(matrix):\n",
466+
" # A: init the 1-dimensional array\n",
448467
" ls = []\n",
468+
" # B: add all the elements to it row-wise\n",
449469
" for row in matrix:\n",
450470
" for element in row:\n",
451471
" ls.append(element)\n",
472+
" # C: return the populated list\n",
452473
" return ls\n",
453474
"\n",
454475
"print(flatten(matrix))"
455476
],
456477
"id": "soviet-accused",
457-
"execution_count": null,
478+
"execution_count": 42,
458479
"outputs": [
459480
{
460481
"output_type": "stream",
@@ -465,13 +486,42 @@
465486
}
466487
]
467488
},
489+
{
490+
"cell_type": "markdown",
491+
"metadata": {
492+
"id": "o-kKYzZc7EBl"
493+
},
494+
"source": [
495+
"TODO: show how to do the same as above, using the list.extend method"
496+
],
497+
"id": "o-kKYzZc7EBl"
498+
},
499+
{
500+
"cell_type": "code",
501+
"metadata": {
502+
"id": "UE1OKdqT5w1Z"
503+
},
504+
"source": [
505+
"def flatten(matrix):\n",
506+
" # A: init the 1-dimensional array\n",
507+
" row_vector = []\n",
508+
" # B: like before, add all the elements\n",
509+
" for row in matrix:\n",
510+
" row_vector.extend(row)\n",
511+
" # C: return the populated list\n",
512+
" return row_vector"
513+
],
514+
"id": "UE1OKdqT5w1Z",
515+
"execution_count": 43,
516+
"outputs": []
517+
},
468518
{
469519
"cell_type": "markdown",
470520
"metadata": {
471521
"id": "radio-pencil"
472522
},
473523
"source": [
474-
"## Activity: Obtain the trace of a given squared matrix"
524+
"## Activity: Obtain the trace of a given square matrix"
475525
],
476526
"id": "radio-pencil"
477527
},

0 commit comments

Comments
 (0)