Skip to content

Commit 8678fa2

Browse files
Edit Activity 21 in Chapter 0
1 parent 187d92b commit 8678fa2

File tree

1 file changed

+92
-33
lines changed

1 file changed

+92
-33
lines changed

chapter0-exercises/chapter_0_part2.ipynb

Lines changed: 92 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@
141141
" print(choices(event, weights))"
142142
],
143143
"id": "english-federation",
144-
"execution_count": 46,
144+
"execution_count": null,
145145
"outputs": [
146146
{
147147
"output_type": "stream",
@@ -209,7 +209,7 @@
209209
" return (occurences / trials)\n"
210210
],
211211
"id": "academic-bankruptcy",
212-
"execution_count": 48,
212+
"execution_count": null,
213213
"outputs": []
214214
},
215215
{
@@ -238,7 +238,7 @@
238238
"print(3*(a**2)*(1-a)) # expected probability - does it match with the above?"
239239
],
240240
"id": "QY52_-8-kXo7",
241-
"execution_count": 49,
241+
"execution_count": null,
242242
"outputs": [
243243
{
244244
"output_type": "stream",
@@ -288,7 +288,7 @@
288288
"nums = [2, 3, 2, 4, 1, 2]"
289289
],
290290
"id": "answering-smooth",
291-
"execution_count": 15,
291+
"execution_count": null,
292292
"outputs": []
293293
},
294294
{
@@ -311,7 +311,7 @@
311311
" return set(nums)"
312312
],
313313
"id": "brilliant-exploration",
314-
"execution_count": 50,
314+
"execution_count": null,
315315
"outputs": []
316316
},
317317
{
@@ -337,7 +337,7 @@
337337
"print(remove_duplicates_1(nums))"
338338
],
339339
"id": "y9rWcRY8mEFg",
340-
"execution_count": 51,
340+
"execution_count": null,
341341
"outputs": [
342342
{
343343
"output_type": "stream",
@@ -372,7 +372,7 @@
372372
" return no_duplicates"
373373
],
374374
"id": "nutritional-pixel",
375-
"execution_count": 52,
375+
"execution_count": null,
376376
"outputs": []
377377
},
378378
{
@@ -398,7 +398,7 @@
398398
"print(remove_duplicates_2(nums))"
399399
],
400400
"id": "hyWuTXFMmQjj",
401-
"execution_count": 53,
401+
"execution_count": null,
402402
"outputs": [
403403
{
404404
"output_type": "stream",
@@ -442,7 +442,7 @@
442442
" return [num for num in numbers if num != 0]"
443443
],
444444
"id": "textile-invitation",
445-
"execution_count": 54,
445+
"execution_count": null,
446446
"outputs": []
447447
},
448448
{
@@ -468,7 +468,7 @@
468468
"print(remove_duplicates_3(nums))"
469469
],
470470
"id": "MYWECU0xnDU4",
471-
"execution_count": 55,
471+
"execution_count": null,
472472
"outputs": [
473473
{
474474
"output_type": "stream",
@@ -517,7 +517,7 @@
517517
" return numbers"
518518
],
519519
"id": "tribal-choir",
520-
"execution_count": 56,
520+
"execution_count": null,
521521
"outputs": []
522522
},
523523
{
@@ -543,7 +543,7 @@
543543
"print(remove_duplicates_4(nums))"
544544
],
545545
"id": "cV9idpAWnTsk",
546-
"execution_count": 57,
546+
"execution_count": null,
547547
"outputs": [
548548
{
549549
"output_type": "stream",
@@ -603,7 +603,7 @@
603603
" return reduce(collect_all_unique, iterable_sets)"
604604
],
605605
"id": "dangerous-wallpaper",
606-
"execution_count": 59,
606+
"execution_count": null,
607607
"outputs": []
608608
},
609609
{
@@ -629,7 +629,7 @@
629629
"print(remove_duplicates_map_reduce(nums))"
630630
],
631631
"id": "dpu8FmIP3iyC",
632-
"execution_count": 61,
632+
"execution_count": null,
633633
"outputs": [
634634
{
635635
"output_type": "stream",
@@ -688,7 +688,7 @@
688688
"d2 = {'b': 40, 'd':50}"
689689
],
690690
"id": "Lz8yzKoR4PoY",
691-
"execution_count": 62,
691+
"execution_count": null,
692692
"outputs": []
693693
},
694694
{
@@ -723,7 +723,7 @@
723723
" return combined"
724724
],
725725
"id": "sized-congo",
726-
"execution_count": 63,
726+
"execution_count": null,
727727
"outputs": []
728728
},
729729
{
@@ -749,7 +749,7 @@
749749
"print(combine_dicts(d1, d2))"
750750
],
751751
"id": "EyoM8YSe4y9U",
752-
"execution_count": 64,
752+
"execution_count": null,
753753
"outputs": [
754754
{
755755
"output_type": "stream",
@@ -806,7 +806,7 @@
806806
"matrix = [[8, 2, 3], [9, 1, 9], [5, 4, 1]]"
807807
],
808808
"id": "Omktfanw6YOd",
809-
"execution_count": 66,
809+
"execution_count": null,
810810
"outputs": []
811811
},
812812
{
@@ -836,7 +836,7 @@
836836
" return ls"
837837
],
838838
"id": "soviet-accused",
839-
"execution_count": 67,
839+
"execution_count": null,
840840
"outputs": []
841841
},
842842
{
@@ -862,7 +862,7 @@
862862
"print(flatten_1(matrix))"
863863
],
864864
"id": "FIk4mS7W6kfm",
865-
"execution_count": 68,
865+
"execution_count": null,
866866
"outputs": [
867867
{
868868
"output_type": "stream",
@@ -903,7 +903,7 @@
903903
" return row_vector"
904904
],
905905
"id": "UE1OKdqT5w1Z",
906-
"execution_count": 65,
906+
"execution_count": null,
907907
"outputs": []
908908
},
909909
{
@@ -919,17 +919,17 @@
919919
{
920920
"cell_type": "code",
921921
"metadata": {
922+
"id": "AOGb0HZj7Slm",
922923
"colab": {
923924
"base_uri": "https://localhost:8080/"
924925
},
925-
"id": "AOGb0HZj7Slm",
926926
"outputId": "b6417184-69f7-4a5a-85f7-4e8cefd1b3fb"
927927
},
928928
"source": [
929929
"print(flatten_2(matrix))"
930930
],
931931
"id": "AOGb0HZj7Slm",
932-
"execution_count": 69,
932+
"execution_count": null,
933933
"outputs": [
934934
{
935935
"output_type": "stream",
@@ -946,27 +946,86 @@
946946
"id": "radio-pencil"
947947
},
948948
"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? "
950954
],
951955
"id": "radio-pencil"
952956
},
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+
},
953989
{
954990
"cell_type": "code",
955991
"metadata": {
956-
"id": "violent-tactics",
957-
"outputId": "a8957164-aefd-4b90-b0c2-76f1639c9368"
992+
"id": "violent-tactics"
958993
},
959994
"source": [
960-
"def trace(matx):\n",
961-
" c = 0\n",
995+
"def trace(matrix):\n",
996+
" sum_diagonal = 0\n",
962997
" 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"
9671000
],
9681001
"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,
9701029
"outputs": [
9711030
{
9721031
"output_type": "stream",

0 commit comments

Comments
 (0)