|
256 | 256 | " numbers = nums.copy()\n", |
257 | 257 | " # B: mark the duplicates\n", |
258 | 258 | " 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", |
260 | 260 | " rest_of_list = numbers[index+1:]\n", |
261 | 261 | " if numbers[index] in rest_of_list:\n", |
262 | 262 | " numbers[index] = 0 \n", |
263 | | - " # C: return all non-duplicates\n", |
| 263 | + " # D: return all non-duplicates\n", |
264 | 264 | " return [num for num in numbers if num != 0]\n", |
265 | 265 | "\n", |
266 | 266 | "print(remove_duplicates_3(nums))" |
|
283 | 283 | "id": "DjhcUMXvw8sB" |
284 | 284 | }, |
285 | 285 | "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" |
288 | 288 | ], |
289 | 289 | "id": "DjhcUMXvw8sB" |
290 | 290 | }, |
|
309 | 309 | " while num_checks < original_length:\n", |
310 | 310 | " num_checks += 1\n", |
311 | 311 | " rest_of_list = numbers[index+1:]\n", |
312 | | - " # duplicate found\n", |
| 312 | + " # C: duplicate found\n", |
313 | 313 | " if numbers[index] in rest_of_list:\n", |
314 | 314 | " 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", |
316 | 316 | " else:\n", |
317 | 317 | " index += 1\n", |
318 | | - " # C: return only the non-duplicate numbers\n", |
| 318 | + " # E: return only the non-duplicate numbers\n", |
319 | 319 | " return numbers\n", |
320 | 320 | "\n", |
321 | 321 | "print(remove_duplicates_4(nums))" |
|
386 | 386 | }, |
387 | 387 | "source": [ |
388 | 388 | "## 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", |
389 | 394 | "- d1 = {'a':10, 'b':20, 'c':30}\n", |
390 | 395 | "- d2 = {'b': 40, 'd':50}\n", |
391 | 396 | "- return d = {'a': 10, 'b': [20, 40], 'c': 50, 'd': 50}" |
|
395 | 400 | { |
396 | 401 | "cell_type": "code", |
397 | 402 | "metadata": { |
| 403 | + "colab": { |
| 404 | + "base_uri": "https://localhost:8080/" |
| 405 | + }, |
398 | 406 | "id": "sized-congo", |
399 | | - "outputId": "6267b2fe-fd5b-4f7b-9cdf-1e2cc70ac0cd" |
| 407 | + "outputId": "a2a26d41-f993-4d08-8595-aceaa1a570b6" |
400 | 408 | }, |
401 | 409 | "source": [ |
402 | 410 | "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", |
408 | 420 | " 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", |
411 | 424 | "\n", |
412 | 425 | "d1 = {'a':10, 'b':20, 'c':30}\n", |
413 | 426 | "d2 = {'b': 40, 'd':50}\n", |
414 | 427 | "print(combine_dicts(d1, d2))" |
415 | 428 | ], |
416 | 429 | "id": "sized-congo", |
417 | | - "execution_count": null, |
| 430 | + "execution_count": 40, |
418 | 431 | "outputs": [ |
419 | 432 | { |
420 | 433 | "output_type": "stream", |
|
431 | 444 | "id": "equal-shock" |
432 | 445 | }, |
433 | 446 | "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" |
435 | 450 | ], |
436 | 451 | "id": "equal-shock" |
437 | 452 | }, |
438 | 453 | { |
439 | 454 | "cell_type": "code", |
440 | 455 | "metadata": { |
| 456 | + "colab": { |
| 457 | + "base_uri": "https://localhost:8080/" |
| 458 | + }, |
441 | 459 | "id": "soviet-accused", |
442 | | - "outputId": "b24ecf65-3c45-4752-9c3c-c2c7d65da036" |
| 460 | + "outputId": "8dcdf69b-d515-4973-c1ec-bd6eb1bcd8d9" |
443 | 461 | }, |
444 | 462 | "source": [ |
445 | 463 | "matrix = [[8, 2, 3], [9, 1, 9], [5, 4, 1]]\n", |
446 | 464 | "\n", |
447 | | - "def flatten(matx):\n", |
| 465 | + "def flatten(matrix):\n", |
| 466 | + " # A: init the 1-dimensional array\n", |
448 | 467 | " ls = []\n", |
| 468 | + " # B: add all the elements to it row-wise\n", |
449 | 469 | " for row in matrix:\n", |
450 | 470 | " for element in row:\n", |
451 | 471 | " ls.append(element)\n", |
| 472 | + " # C: return the populated list\n", |
452 | 473 | " return ls\n", |
453 | 474 | "\n", |
454 | 475 | "print(flatten(matrix))" |
455 | 476 | ], |
456 | 477 | "id": "soviet-accused", |
457 | | - "execution_count": null, |
| 478 | + "execution_count": 42, |
458 | 479 | "outputs": [ |
459 | 480 | { |
460 | 481 | "output_type": "stream", |
|
465 | 486 | } |
466 | 487 | ] |
467 | 488 | }, |
| 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 | + }, |
468 | 518 | { |
469 | 519 | "cell_type": "markdown", |
470 | 520 | "metadata": { |
471 | 521 | "id": "radio-pencil" |
472 | 522 | }, |
473 | 523 | "source": [ |
474 | | - "## Activity: Obtain the trace of a given squared matrix" |
| 524 | + "## Activity: Obtain the trace of a given square matrix" |
475 | 525 | ], |
476 | 526 | "id": "radio-pencil" |
477 | 527 | }, |
|
0 commit comments