|
141 | 141 | " print(choices(event, weights))" |
142 | 142 | ], |
143 | 143 | "id": "english-federation", |
144 | | - "execution_count": 46, |
| 144 | + "execution_count": null, |
145 | 145 | "outputs": [ |
146 | 146 | { |
147 | 147 | "output_type": "stream", |
|
209 | 209 | " return (occurences / trials)\n" |
210 | 210 | ], |
211 | 211 | "id": "academic-bankruptcy", |
212 | | - "execution_count": 48, |
| 212 | + "execution_count": null, |
213 | 213 | "outputs": [] |
214 | 214 | }, |
215 | 215 | { |
|
238 | 238 | "print(3*(a**2)*(1-a)) # expected probability - does it match with the above?" |
239 | 239 | ], |
240 | 240 | "id": "QY52_-8-kXo7", |
241 | | - "execution_count": 49, |
| 241 | + "execution_count": null, |
242 | 242 | "outputs": [ |
243 | 243 | { |
244 | 244 | "output_type": "stream", |
|
288 | 288 | "nums = [2, 3, 2, 4, 1, 2]" |
289 | 289 | ], |
290 | 290 | "id": "answering-smooth", |
291 | | - "execution_count": 15, |
| 291 | + "execution_count": null, |
292 | 292 | "outputs": [] |
293 | 293 | }, |
294 | 294 | { |
|
311 | 311 | " return set(nums)" |
312 | 312 | ], |
313 | 313 | "id": "brilliant-exploration", |
314 | | - "execution_count": 50, |
| 314 | + "execution_count": null, |
315 | 315 | "outputs": [] |
316 | 316 | }, |
317 | 317 | { |
|
337 | 337 | "print(remove_duplicates_1(nums))" |
338 | 338 | ], |
339 | 339 | "id": "y9rWcRY8mEFg", |
340 | | - "execution_count": 51, |
| 340 | + "execution_count": null, |
341 | 341 | "outputs": [ |
342 | 342 | { |
343 | 343 | "output_type": "stream", |
|
372 | 372 | " return no_duplicates" |
373 | 373 | ], |
374 | 374 | "id": "nutritional-pixel", |
375 | | - "execution_count": 52, |
| 375 | + "execution_count": null, |
376 | 376 | "outputs": [] |
377 | 377 | }, |
378 | 378 | { |
|
398 | 398 | "print(remove_duplicates_2(nums))" |
399 | 399 | ], |
400 | 400 | "id": "hyWuTXFMmQjj", |
401 | | - "execution_count": 53, |
| 401 | + "execution_count": null, |
402 | 402 | "outputs": [ |
403 | 403 | { |
404 | 404 | "output_type": "stream", |
|
442 | 442 | " return [num for num in numbers if num != 0]" |
443 | 443 | ], |
444 | 444 | "id": "textile-invitation", |
445 | | - "execution_count": 54, |
| 445 | + "execution_count": null, |
446 | 446 | "outputs": [] |
447 | 447 | }, |
448 | 448 | { |
|
468 | 468 | "print(remove_duplicates_3(nums))" |
469 | 469 | ], |
470 | 470 | "id": "MYWECU0xnDU4", |
471 | | - "execution_count": 55, |
| 471 | + "execution_count": null, |
472 | 472 | "outputs": [ |
473 | 473 | { |
474 | 474 | "output_type": "stream", |
|
517 | 517 | " return numbers" |
518 | 518 | ], |
519 | 519 | "id": "tribal-choir", |
520 | | - "execution_count": 56, |
| 520 | + "execution_count": null, |
521 | 521 | "outputs": [] |
522 | 522 | }, |
523 | 523 | { |
|
543 | 543 | "print(remove_duplicates_4(nums))" |
544 | 544 | ], |
545 | 545 | "id": "cV9idpAWnTsk", |
546 | | - "execution_count": 57, |
| 546 | + "execution_count": null, |
547 | 547 | "outputs": [ |
548 | 548 | { |
549 | 549 | "output_type": "stream", |
|
603 | 603 | " return reduce(collect_all_unique, iterable_sets)" |
604 | 604 | ], |
605 | 605 | "id": "dangerous-wallpaper", |
606 | | - "execution_count": 59, |
| 606 | + "execution_count": null, |
607 | 607 | "outputs": [] |
608 | 608 | }, |
609 | 609 | { |
|
629 | 629 | "print(remove_duplicates_map_reduce(nums))" |
630 | 630 | ], |
631 | 631 | "id": "dpu8FmIP3iyC", |
632 | | - "execution_count": 61, |
| 632 | + "execution_count": null, |
633 | 633 | "outputs": [ |
634 | 634 | { |
635 | 635 | "output_type": "stream", |
|
688 | 688 | "d2 = {'b': 40, 'd':50}" |
689 | 689 | ], |
690 | 690 | "id": "Lz8yzKoR4PoY", |
691 | | - "execution_count": 62, |
| 691 | + "execution_count": null, |
692 | 692 | "outputs": [] |
693 | 693 | }, |
694 | 694 | { |
|
723 | 723 | " return combined" |
724 | 724 | ], |
725 | 725 | "id": "sized-congo", |
726 | | - "execution_count": 63, |
| 726 | + "execution_count": null, |
727 | 727 | "outputs": [] |
728 | 728 | }, |
729 | 729 | { |
|
749 | 749 | "print(combine_dicts(d1, d2))" |
750 | 750 | ], |
751 | 751 | "id": "EyoM8YSe4y9U", |
752 | | - "execution_count": 64, |
| 752 | + "execution_count": null, |
753 | 753 | "outputs": [ |
754 | 754 | { |
755 | 755 | "output_type": "stream", |
|
806 | 806 | "matrix = [[8, 2, 3], [9, 1, 9], [5, 4, 1]]" |
807 | 807 | ], |
808 | 808 | "id": "Omktfanw6YOd", |
809 | | - "execution_count": 66, |
| 809 | + "execution_count": null, |
810 | 810 | "outputs": [] |
811 | 811 | }, |
812 | 812 | { |
|
836 | 836 | " return ls" |
837 | 837 | ], |
838 | 838 | "id": "soviet-accused", |
839 | | - "execution_count": 67, |
| 839 | + "execution_count": null, |
840 | 840 | "outputs": [] |
841 | 841 | }, |
842 | 842 | { |
|
862 | 862 | "print(flatten_1(matrix))" |
863 | 863 | ], |
864 | 864 | "id": "FIk4mS7W6kfm", |
865 | | - "execution_count": 68, |
| 865 | + "execution_count": null, |
866 | 866 | "outputs": [ |
867 | 867 | { |
868 | 868 | "output_type": "stream", |
|
903 | 903 | " return row_vector" |
904 | 904 | ], |
905 | 905 | "id": "UE1OKdqT5w1Z", |
906 | | - "execution_count": 65, |
| 906 | + "execution_count": null, |
907 | 907 | "outputs": [] |
908 | 908 | }, |
909 | 909 | { |
|
919 | 919 | { |
920 | 920 | "cell_type": "code", |
921 | 921 | "metadata": { |
| 922 | + "id": "AOGb0HZj7Slm", |
922 | 923 | "colab": { |
923 | 924 | "base_uri": "https://localhost:8080/" |
924 | 925 | }, |
925 | | - "id": "AOGb0HZj7Slm", |
926 | 926 | "outputId": "b6417184-69f7-4a5a-85f7-4e8cefd1b3fb" |
927 | 927 | }, |
928 | 928 | "source": [ |
929 | 929 | "print(flatten_2(matrix))" |
930 | 930 | ], |
931 | 931 | "id": "AOGb0HZj7Slm", |
932 | | - "execution_count": 69, |
| 932 | + "execution_count": null, |
933 | 933 | "outputs": [ |
934 | 934 | { |
935 | 935 | "output_type": "stream", |
|
946 | 946 | "id": "radio-pencil" |
947 | 947 | }, |
948 | 948 | "source": [ |
949 | | - "## Activity: Obtain the trace of a given square matrix" |
| 949 | + "## Activity 21: Compute the Trace\n", |
| 950 | + "\n", |
| 951 | + "The *trace of a square matrix* is a concept from linear algebra. According to [Wikipedia](https://en.wikipedia.org/wiki/Trace_%28linear_algebra%29), it is \"defined to be the sum of elements on the main diagonal (from the upper left to the lower right) of $A$.\" Here, the Wikipedia editors simply use $A$ as a variable to represent our square matrix.\n", |
| 952 | + "\n", |
| 953 | + "You are given a 2D list of numbers, `matrix`. You may assume it has the same number of rows as it has columns. How would you write Python code to compute its trace? " |
950 | 954 | ], |
951 | 955 | "id": "radio-pencil" |
952 | 956 | }, |
| 957 | + { |
| 958 | + "cell_type": "markdown", |
| 959 | + "metadata": { |
| 960 | + "id": "EdjPaZBxRoZa" |
| 961 | + }, |
| 962 | + "source": [ |
| 963 | + "### Example Input" |
| 964 | + ], |
| 965 | + "id": "EdjPaZBxRoZa" |
| 966 | + }, |
| 967 | + { |
| 968 | + "cell_type": "code", |
| 969 | + "metadata": { |
| 970 | + "id": "CgdIeZLVRqGA" |
| 971 | + }, |
| 972 | + "source": [ |
| 973 | + "matrix = [[8, 2, 3], [9, 1, 9], [5, 4, 1]]" |
| 974 | + ], |
| 975 | + "id": "CgdIeZLVRqGA", |
| 976 | + "execution_count": 2, |
| 977 | + "outputs": [] |
| 978 | + }, |
| 979 | + { |
| 980 | + "cell_type": "markdown", |
| 981 | + "metadata": { |
| 982 | + "id": "YsyWOrzbRen0" |
| 983 | + }, |
| 984 | + "source": [ |
| 985 | + "### Solution: Using `enumerate`" |
| 986 | + ], |
| 987 | + "id": "YsyWOrzbRen0" |
| 988 | + }, |
953 | 989 | { |
954 | 990 | "cell_type": "code", |
955 | 991 | "metadata": { |
956 | | - "id": "violent-tactics", |
957 | | - "outputId": "a8957164-aefd-4b90-b0c2-76f1639c9368" |
| 992 | + "id": "violent-tactics" |
958 | 993 | }, |
959 | 994 | "source": [ |
960 | | - "def trace(matx):\n", |
961 | | - " c = 0\n", |
| 995 | + "def trace(matrix):\n", |
| 996 | + " sum_diagonal = 0\n", |
962 | 997 | " for index_row, row in enumerate(matrix):\n", |
963 | | - " c += matrix[index_row][index_row]\n", |
964 | | - " return c\n", |
965 | | - "\n", |
966 | | - "print(trace(matrix)) " |
| 998 | + " sum_diagonal += matrix[index_row][index_row]\n", |
| 999 | + " return sum_diagonal" |
967 | 1000 | ], |
968 | 1001 | "id": "violent-tactics", |
969 | | - "execution_count": null, |
| 1002 | + "execution_count": 1, |
| 1003 | + "outputs": [] |
| 1004 | + }, |
| 1005 | + { |
| 1006 | + "cell_type": "markdown", |
| 1007 | + "metadata": { |
| 1008 | + "id": "LBBairKbRige" |
| 1009 | + }, |
| 1010 | + "source": [ |
| 1011 | + "#### Test Out Solution Code" |
| 1012 | + ], |
| 1013 | + "id": "LBBairKbRige" |
| 1014 | + }, |
| 1015 | + { |
| 1016 | + "cell_type": "code", |
| 1017 | + "metadata": { |
| 1018 | + "colab": { |
| 1019 | + "base_uri": "https://localhost:8080/" |
| 1020 | + }, |
| 1021 | + "id": "mT_Y7FSORnFm", |
| 1022 | + "outputId": "cd87e58b-1808-4633-9839-ee941d9d715a" |
| 1023 | + }, |
| 1024 | + "source": [ |
| 1025 | + "print(trace(matrix)) " |
| 1026 | + ], |
| 1027 | + "id": "mT_Y7FSORnFm", |
| 1028 | + "execution_count": 3, |
970 | 1029 | "outputs": [ |
971 | 1030 | { |
972 | 1031 | "output_type": "stream", |
|
0 commit comments