Skip to content

Commit 2bba5c3

Browse files
Exercise cleanup.
1 parent 459ef36 commit 2bba5c3

File tree

1 file changed

+57
-83
lines changed

1 file changed

+57
-83
lines changed

chapter0-exercises/chapter_0_part1.ipynb

Lines changed: 57 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -943,17 +943,17 @@
943943
"source": [
944944
"def element_wise_sum_1(nums1, nums2):\n",
945945
" # A: init the output list\n",
946-
" summed_elems = [0 for _ in range(len(nums1))]\n",
946+
" summed_elems = [0 for _ in nums1]\n",
947947
" # B: iterate over both input lists\n",
948-
" for index in range(len(nums1)):\n",
948+
" for index in range(len(summed_elems)):\n",
949949
" # C: place the next sum needed in the output list\n",
950950
" elem_sum = nums1[index] + nums2[index]\n",
951951
" summed_elems[index] = elem_sum\n",
952952
" # D: return the output\n",
953953
" return summed_elems"
954954
],
955955
"id": "FLsum4Ac-EuZ",
956-
"execution_count": null,
956+
"execution_count": 32,
957957
"outputs": []
958958
},
959959
{
@@ -979,14 +979,14 @@
979979
"print(element_wise_sum_1(nums1, nums2))"
980980
],
981981
"id": "yrUOA73E_nCr",
982-
"execution_count": null,
982+
"execution_count": 33,
983983
"outputs": [
984984
{
985985
"output_type": "stream",
986+
"name": "stdout",
986987
"text": [
987988
"[11, 17, 23]\n"
988-
],
989-
"name": "stdout"
989+
]
990990
}
991991
]
992992
},
@@ -1013,14 +1013,14 @@
10131013
"This is where Python's built-in `zip()` function can become immensely useful. For our purposes, it will basically combine our arrays into a single matrix - forming an array that is the same length as `nums1` or `nums2` before; and which contains an array for all the elements found at that index in the aforementioned `nums1` and `nums2`. Observe how this works in the diagram below:\n",
10141014
"```\n",
10151015
"How zip(nums1, nums2) Works:\n",
1016-
"index | nums1 | num2 | out \n",
1017-
" 0 | 1 -------> 10 --------> (1, 10)\n",
1018-
" 1 | 2 -------> 15 --------> (2, 15)\n",
1019-
" 2 | 3 -------> 20 --------> (3, 20)\n",
1020-
" | | | \n",
1016+
"index | nums1 | num2 | out \n",
1017+
" 0 | 1 -------> 10 -------> (1, 10)\n",
1018+
" 1 | 2 -------> 15 -------> (2, 15)\n",
1019+
" 2 | 3 -------> 20 -------> (3, 20)\n",
1020+
" | | |\n",
10211021
"```\n",
10221022
"\n",
1023-
"Essentially, by using `zip()` we take out the need for tracking the `index` ourselves, and can thereby compute the element-wise sum using a more straightforward `for` loop, nested in a *list comprehension*."
1023+
"Essentially, by using `zip()` we take out the need for tracking the `index` ourselves, and can thereby compute the element-wise sum using a more straightforward `for` loop, nested in a *list comprehension*."
10241024
],
10251025
"id": "tcQe2UmO9VCS"
10261026
},
@@ -1039,7 +1039,7 @@
10391039
" return summed_elems"
10401040
],
10411041
"id": "prompt-rescue",
1042-
"execution_count": null,
1042+
"execution_count": 35,
10431043
"outputs": []
10441044
},
10451045
{
@@ -1075,14 +1075,14 @@
10751075
"print(element_wise_sum_2(nums1, nums2))"
10761076
],
10771077
"id": "civil-accordance",
1078-
"execution_count": null,
1078+
"execution_count": 36,
10791079
"outputs": [
10801080
{
10811081
"output_type": "stream",
1082+
"name": "stdout",
10821083
"text": [
10831084
"[11, 17, 23]\n"
1084-
],
1085-
"name": "stdout"
1085+
]
10861086
}
10871087
]
10881088
},
@@ -1117,14 +1117,14 @@
11171117
"id": "QC7SMlmn8huV"
11181118
},
11191119
"source": [
1120-
"# A: first, import the function we can use to add 2 elems\n",
1120+
"# A: first, import (or define) a function we can use to add 2 elems\n",
11211121
"from operator import add\n",
11221122
"\n",
11231123
"# B: iterate over the two arrays in parallel, to compute the element-wise sum\n",
11241124
"element_wise_sum_3 = list(map(add, nums1, nums2))"
11251125
],
11261126
"id": "QC7SMlmn8huV",
1127-
"execution_count": null,
1127+
"execution_count": 37,
11281128
"outputs": []
11291129
},
11301130
{
@@ -1150,14 +1150,14 @@
11501150
"print(element_wise_sum_3)"
11511151
],
11521152
"id": "PDvfPT1m9NDL",
1153-
"execution_count": null,
1153+
"execution_count": 38,
11541154
"outputs": [
11551155
{
11561156
"output_type": "stream",
1157+
"name": "stdout",
11571158
"text": [
11581159
"[11, 17, 23]\n"
1159-
],
1160-
"name": "stdout"
1160+
]
11611161
}
11621162
]
11631163
},
@@ -1171,7 +1171,7 @@
11711171
"\n",
11721172
"You are given an array of numbers, `nums`. How would implement a function in Python to compute the square of each number in `nums`?\n",
11731173
"\n",
1174-
"Please return the ouput in a new array."
1174+
"Please return the output in a new array."
11751175
],
11761176
"id": "military-magnet"
11771177
},
@@ -1194,7 +1194,7 @@
11941194
"nums = [1, 2, 3]"
11951195
],
11961196
"id": "dJQkJkR7BmE8",
1197-
"execution_count": null,
1197+
"execution_count": 39,
11981198
"outputs": []
11991199
},
12001200
{
@@ -1217,7 +1217,7 @@
12171217
" return [num**2 for num in nums]"
12181218
],
12191219
"id": "potential-fields",
1220-
"execution_count": null,
1220+
"execution_count": 40,
12211221
"outputs": []
12221222
},
12231223
{
@@ -1243,17 +1243,29 @@
12431243
"print(square_elements(nums))"
12441244
],
12451245
"id": "theoretical-houston",
1246-
"execution_count": null,
1246+
"execution_count": 41,
12471247
"outputs": [
12481248
{
12491249
"output_type": "stream",
1250+
"name": "stdout",
12501251
"text": [
12511252
"[1, 4, 9]\n"
1252-
],
1253-
"name": "stdout"
1253+
]
12541254
}
12551255
]
12561256
},
1257+
{
1258+
"source": [
1259+
"**Note**: Alternative to the `**` operator, Python also has a few other options for calculating the power of a number:\n",
1260+
"\n",
1261+
"1. `pow(a, b)`: this is a built-in function that return the result of `a` raised to the exponent `b`.\n",
1262+
"2. `math.pow(a, b)`: similar to the `pow()` function, except that it returns a floating-point value rather than an integer.\n",
1263+
"\n",
1264+
"Regardless of which option you choose to go with, we still advise using a list comprehension to solve this problem."
1265+
],
1266+
"cell_type": "markdown",
1267+
"metadata": {}
1268+
},
12571269
{
12581270
"cell_type": "markdown",
12591271
"metadata": {
@@ -1272,8 +1284,8 @@
12721284
"\n",
12731285
"$E(X) = \\sum_{i=1}^{6}i * P(dice = i)$\n",
12741286
"\n",
1275-
"Where:\n",
1276-
"- $X$ = our random event (aka the *random variable*)\n",
1287+
"Here, the expected value is a *probability-weighted average*, which accounts for all the different values the die can land on. Here's what the variables mean:\n",
1288+
"- $X$ = our random event (aka the *random variable*), which is the dice roll\n",
12771289
"- $i$ = the value of the dice on a given roll\n",
12781290
"- $P(dice = i)$ = the probability of the die landing with that value on top\n",
12791291
"\n",
@@ -1285,7 +1297,7 @@
12851297
"2. Compute the probability that when the die is rolled, its face value is 2 $(P(dice = 2))$?\n",
12861298
"3. And so and so forth, for all the faces of the die?\n",
12871299
"\n",
1288-
"7.2: Now, how would you compute $E(X)$ of the die, using the answer from the questions above?"
1300+
"7.2: Now, how would you compute $E(X)$ of the die, using your answers from 7.1?"
12891301
],
12901302
"id": "descending-orbit"
12911303
},
@@ -1319,53 +1331,16 @@
13191331
"source": [
13201332
"# We can show that E(X) is the mean of the following dice random variable\n",
13211333
"import numpy as np\n",
1322-
"# lets roll the dice 1000 times\n",
1334+
"# lets roll the dice 1,000 times\n",
13231335
"dice = np.random.randint(low=1.0, high=7.0, size=1000)\n",
1324-
"print(dice)\n",
1325-
"# Compute the mean of dice list\n",
1336+
"print(dice) # prints a list of 1,000 randomly generated integers\n",
1337+
"# Compute the mean of the dice values\n",
13261338
"print(np.mean(dice))\n",
13271339
"print(sum(dice)/len(dice))"
13281340
],
13291341
"id": "headed-michael",
13301342
"execution_count": null,
1331-
"outputs": [
1332-
{
1333-
"output_type": "stream",
1334-
"text": [
1335-
"[4 6 1 4 5 2 3 2 4 4 5 3 6 4 2 6 2 3 3 4 6 1 5 2 5 4 5 1 3 4 1 5 5 4 6 4 2\n",
1336-
" 3 1 3 3 2 6 3 1 5 4 1 2 1 6 1 6 1 5 3 4 5 4 5 5 2 1 5 2 6 4 5 6 4 1 2 4 1\n",
1337-
" 2 2 6 5 5 2 3 5 5 4 1 5 6 4 4 5 5 6 2 6 5 4 4 3 6 6 3 1 6 5 4 3 5 3 3 2 1\n",
1338-
" 3 6 5 3 1 1 5 5 1 3 4 4 5 2 3 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346
"cell_type": "markdown",
@@ -1456,17 +1431,17 @@
14561431
"If everything is working correctly, then the `histogram` should show the following key-value pairs:\n",
14571432
"```\n",
14581433
"{\n",
1459-
" 1: 2, \n",
1460-
" 3: 5, # <--- 3 is the mode!\n",
1461-
" 5: 2, \n",
1462-
" 2: 1, \n",
1463-
" 7: 3, \n",
1464-
" 8: 1, \n",
1465-
" 4: 2, \n",
1466-
" 10: 2, \n",
1467-
" 0: 3, \n",
1468-
" 6: 1, \n",
1469-
" 9: 1\n",
1434+
" 1 : 2, \n",
1435+
" 3 : 5, # <--- 3 is the mode!\n",
1436+
" 5 : 2, \n",
1437+
" 2 : 1, \n",
1438+
" 7 : 3, \n",
1439+
" 8 : 1, \n",
1440+
" 4 : 2, \n",
1441+
" 10 : 2, \n",
1442+
" 0 : 3, \n",
1443+
" 6 : 1, \n",
1444+
" 9 : 1\n",
14701445
"}\n",
14711446
"```"
14721447
],
@@ -1529,7 +1504,6 @@
15291504
"source": [
15301505
"from collections import Counter\n",
15311506
"\n",
1532-
"\n",
15331507
"def compute_mode_2(dataset):\n",
15341508
" # A: compute the histogram\n",
15351509
" histogram = Counter(dataset)\n",

0 commit comments

Comments
 (0)