diff --git a/ThinkPythonNotebooks.zip b/ThinkPythonNotebooks.zip index 4a991b9..f5c7866 100644 Binary files a/ThinkPythonNotebooks.zip and b/ThinkPythonNotebooks.zip differ diff --git a/chapters/chap00.ipynb b/chapters/chap00.ipynb index 55b62c2..e7c283c 100644 --- a/chapters/chap00.ipynb +++ b/chapters/chap00.ipynb @@ -50,7 +50,7 @@ "\n", "Writing this book, I tried to be careful with the vocabulary.\n", "I define each term when it first appears.\n", - "And there is a glossary that the end of each chapter that reviews the terms that were introduced.\n", + "And there is a glossary at the end of each chapter that reviews the terms that were introduced.\n", "\n", "I also tried to be concise.\n", "The less mental effort it takes to read the book, the more capacity you will have for programming.\n", @@ -171,9 +171,9 @@ "\n", "If you are teaching with this book, here are some resources you might find useful.\n", "\n", - "* You can find notebooks with solutions to the exercises from , along with links to the additional resources below.\n", + "* You can find notebooks with solutions to the exercises at , along with links to the additional resources below.\n", "\n", - "* Quizzes for each chapter, and a summative quiz for the whole book, are available from [COMING SOON]\n", + "* Quizzes for each chapter, and a summative quiz for the whole book, are available on request.\n", "\n", "* *Teaching and Learning with Jupyter* is an online book with suggestions for using Jupyter effectively in the classroom. You can read the book at \n", "\n", diff --git a/chapters/chap03.ipynb b/chapters/chap03.ipynb index 7eae21b..5f2cb1d 100644 --- a/chapters/chap03.ipynb +++ b/chapters/chap03.ipynb @@ -606,7 +606,10 @@ "width, height, x, y = [3.77, 2.9, 1.1, 2.65]\n", "ax = diagram(width, height)\n", "bbox = stack.draw(ax, x, y)\n", - "# adjust(x, y, bbox)" + "# adjust(x, y, bbox)\n", + "\n", + "import matplotlib.pyplot as plt\n", + "plt.savefig('chap03_stack_diagram.png', dpi=300)" ] }, { diff --git a/chapters/chap06.ipynb b/chapters/chap06.ipynb index 1d30683..a8d03c3 100644 --- a/chapters/chap06.ipynb +++ b/chapters/chap06.ipynb @@ -12,7 +12,7 @@ }, { "cell_type": "code", - "execution_count": 87, + "execution_count": 5, "id": "56b1c184", "metadata": { "tags": [] @@ -66,7 +66,7 @@ }, { "cell_type": "code", - "execution_count": 88, + "execution_count": 6, "id": "e0e1dd91", "metadata": {}, "outputs": [], @@ -86,7 +86,7 @@ }, { "cell_type": "code", - "execution_count": 89, + "execution_count": 7, "id": "5aaf62d2", "metadata": {}, "outputs": [], @@ -104,7 +104,7 @@ }, { "cell_type": "code", - "execution_count": 90, + "execution_count": 8, "id": "741f7386", "metadata": {}, "outputs": [], @@ -122,7 +122,7 @@ }, { "cell_type": "code", - "execution_count": 91, + "execution_count": 9, "id": "e56d39c4", "metadata": {}, "outputs": [], @@ -140,7 +140,7 @@ }, { "cell_type": "code", - "execution_count": 92, + "execution_count": 10, "id": "50a9a9be", "metadata": {}, "outputs": [], @@ -164,7 +164,7 @@ }, { "cell_type": "code", - "execution_count": 93, + "execution_count": 11, "id": "d70fd9b5", "metadata": {}, "outputs": [], @@ -182,7 +182,7 @@ }, { "cell_type": "code", - "execution_count": 94, + "execution_count": 12, "id": "ef20ba8c", "metadata": {}, "outputs": [], @@ -200,7 +200,7 @@ }, { "cell_type": "code", - "execution_count": 95, + "execution_count": 13, "id": "0a4670f4", "metadata": {}, "outputs": [], @@ -218,7 +218,7 @@ }, { "cell_type": "code", - "execution_count": 96, + "execution_count": 14, "id": "6e6460b9", "metadata": {}, "outputs": [], @@ -236,7 +236,7 @@ }, { "cell_type": "code", - "execution_count": 97, + "execution_count": 15, "id": "77613df9", "metadata": { "tags": [] @@ -269,7 +269,7 @@ }, { "cell_type": "code", - "execution_count": 98, + "execution_count": 16, "id": "89c083f8", "metadata": {}, "outputs": [], @@ -288,7 +288,7 @@ }, { "cell_type": "code", - "execution_count": 99, + "execution_count": 17, "id": "737b67ca", "metadata": {}, "outputs": [], @@ -307,7 +307,7 @@ }, { "cell_type": "code", - "execution_count": 100, + "execution_count": 18, "id": "9b4fa14f", "metadata": {}, "outputs": [], @@ -325,7 +325,7 @@ }, { "cell_type": "code", - "execution_count": 101, + "execution_count": 19, "id": "50f96bcb", "metadata": {}, "outputs": [], @@ -344,7 +344,7 @@ }, { "cell_type": "code", - "execution_count": 102, + "execution_count": 20, "id": "6712f2df", "metadata": {}, "outputs": [], @@ -364,7 +364,7 @@ }, { "cell_type": "code", - "execution_count": 103, + "execution_count": 21, "id": "0ec1afd3", "metadata": {}, "outputs": [], @@ -386,7 +386,7 @@ }, { "cell_type": "code", - "execution_count": 104, + "execution_count": 22, "id": "c82334b6", "metadata": {}, "outputs": [], @@ -404,7 +404,7 @@ }, { "cell_type": "code", - "execution_count": 105, + "execution_count": 23, "id": "595ec598", "metadata": {}, "outputs": [], @@ -432,7 +432,7 @@ }, { "cell_type": "code", - "execution_count": 106, + "execution_count": 24, "id": "236c59e6", "metadata": {}, "outputs": [], @@ -459,7 +459,7 @@ }, { "cell_type": "code", - "execution_count": 107, + "execution_count": 25, "id": "2f60639c", "metadata": {}, "outputs": [], @@ -481,7 +481,7 @@ }, { "cell_type": "code", - "execution_count": 108, + "execution_count": 26, "id": "c9dae6c8", "metadata": {}, "outputs": [], @@ -501,7 +501,7 @@ }, { "cell_type": "code", - "execution_count": 109, + "execution_count": 27, "id": "c8c4edee", "metadata": {}, "outputs": [], @@ -555,7 +555,7 @@ }, { "cell_type": "code", - "execution_count": 110, + "execution_count": 28, "id": "bbcab1ed", "metadata": {}, "outputs": [], @@ -577,7 +577,7 @@ }, { "cell_type": "code", - "execution_count": 111, + "execution_count": 29, "id": "923d96db", "metadata": {}, "outputs": [], @@ -601,7 +601,7 @@ }, { "cell_type": "code", - "execution_count": 112, + "execution_count": 30, "id": "9374cfe3", "metadata": {}, "outputs": [], @@ -627,7 +627,7 @@ }, { "cell_type": "code", - "execution_count": 113, + "execution_count": 31, "id": "405af839", "metadata": {}, "outputs": [], @@ -645,7 +645,7 @@ }, { "cell_type": "code", - "execution_count": 114, + "execution_count": 32, "id": "e52b3b04", "metadata": {}, "outputs": [], @@ -668,7 +668,7 @@ }, { "cell_type": "code", - "execution_count": 115, + "execution_count": 33, "id": "38eebbf3", "metadata": {}, "outputs": [], @@ -686,7 +686,7 @@ }, { "cell_type": "code", - "execution_count": 116, + "execution_count": 34, "id": "b4536ea0", "metadata": {}, "outputs": [], @@ -709,7 +709,7 @@ }, { "cell_type": "code", - "execution_count": 117, + "execution_count": 35, "id": "325efb93", "metadata": {}, "outputs": [], @@ -729,7 +729,7 @@ }, { "cell_type": "code", - "execution_count": 118, + "execution_count": 36, "id": "3cd982ce", "metadata": {}, "outputs": [], @@ -753,7 +753,7 @@ }, { "cell_type": "code", - "execution_count": 119, + "execution_count": 37, "id": "c734f5b2", "metadata": {}, "outputs": [], @@ -771,7 +771,7 @@ }, { "cell_type": "code", - "execution_count": 120, + "execution_count": 38, "id": "094a242f", "metadata": {}, "outputs": [], @@ -813,7 +813,7 @@ }, { "cell_type": "code", - "execution_count": 121, + "execution_count": 39, "id": "64207948", "metadata": {}, "outputs": [], @@ -835,7 +835,7 @@ }, { "cell_type": "code", - "execution_count": 122, + "execution_count": 40, "id": "c367cdae", "metadata": {}, "outputs": [], @@ -845,7 +845,7 @@ }, { "cell_type": "code", - "execution_count": 123, + "execution_count": 41, "id": "837f4f95", "metadata": {}, "outputs": [], @@ -864,7 +864,7 @@ }, { "cell_type": "code", - "execution_count": 124, + "execution_count": 42, "id": "e411354f", "metadata": {}, "outputs": [], @@ -883,7 +883,7 @@ }, { "cell_type": "code", - "execution_count": 125, + "execution_count": 43, "id": "925e7d4f", "metadata": {}, "outputs": [], @@ -902,7 +902,7 @@ }, { "cell_type": "code", - "execution_count": 126, + "execution_count": 44, "id": "62178e75", "metadata": {}, "outputs": [], @@ -949,7 +949,7 @@ }, { "cell_type": "code", - "execution_count": 127, + "execution_count": 45, "id": "23e37c79", "metadata": {}, "outputs": [], @@ -968,7 +968,7 @@ }, { "cell_type": "code", - "execution_count": 128, + "execution_count": 46, "id": "5ea57d9f", "metadata": {}, "outputs": [], @@ -991,7 +991,7 @@ }, { "cell_type": "code", - "execution_count": 129, + "execution_count": 47, "id": "b66e670b", "metadata": {}, "outputs": [], @@ -1039,7 +1039,7 @@ }, { "cell_type": "code", - "execution_count": 130, + "execution_count": 48, "id": "455f0457", "metadata": { "tags": [] @@ -1063,7 +1063,7 @@ " loc='left', dx=1.2)\n", " frames.append(frame)\n", " \n", - "binding1 = make_binding('n', n)\n", + "binding1 = make_binding('n', 0)\n", "frame = Frame([binding1], name='factorial', value=1, \n", " shim=1.2, loc='left', dx=1.4)\n", "frames.append(frame)\n", @@ -1073,7 +1073,7 @@ }, { "cell_type": "code", - "execution_count": 131, + "execution_count": 49, "id": "a75ccd9b", "metadata": { "tags": [] @@ -1142,7 +1142,7 @@ }, { "cell_type": "code", - "execution_count": 132, + "execution_count": 50, "id": "cad75752", "metadata": {}, "outputs": [], @@ -1180,7 +1180,7 @@ }, { "cell_type": "code", - "execution_count": 133, + "execution_count": 51, "id": "5e4b5f1d", "metadata": { "tags": [] @@ -1197,8 +1197,7 @@ "id": "0bec7ba4", "metadata": {}, "source": [ - "It looks like an infinite recursion. How can that be? The function has a\n", - "base case -- when `n == 0`.\n", + "It looks like an infinite recursion. How can that be? The function has base cases when `n == 1` or `n == 0`.\n", "But if `n` is not an integer, we can *miss* the base case and recurse forever.\n", "\n", "In this example, the initial value of `n` is `1.5`.\n", @@ -1212,7 +1211,7 @@ }, { "cell_type": "code", - "execution_count": 134, + "execution_count": 52, "id": "3f607dff", "metadata": {}, "outputs": [], @@ -1222,7 +1221,7 @@ }, { "cell_type": "code", - "execution_count": 135, + "execution_count": 53, "id": "ab638bfe", "metadata": {}, "outputs": [], @@ -1240,7 +1239,7 @@ }, { "cell_type": "code", - "execution_count": 136, + "execution_count": 54, "id": "73aafac0", "metadata": {}, "outputs": [], @@ -1270,7 +1269,7 @@ }, { "cell_type": "code", - "execution_count": 137, + "execution_count": 55, "id": "be881cb7", "metadata": {}, "outputs": [], @@ -1289,7 +1288,7 @@ }, { "cell_type": "code", - "execution_count": 138, + "execution_count": 56, "id": "fa83014f", "metadata": {}, "outputs": [], @@ -1338,7 +1337,7 @@ }, { "cell_type": "code", - "execution_count": 139, + "execution_count": 57, "id": "1d50479e", "metadata": {}, "outputs": [], @@ -1367,7 +1366,7 @@ }, { "cell_type": "code", - "execution_count": 140, + "execution_count": 58, "id": "798db5c4", "metadata": {}, "outputs": [], @@ -1424,7 +1423,7 @@ }, { "cell_type": "code", - "execution_count": 142, + "execution_count": 59, "id": "e0f15ca4", "metadata": { "tags": [] @@ -1449,7 +1448,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 60, "id": "90b4979f", "metadata": {}, "outputs": [], @@ -1471,7 +1470,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 61, "id": "9217f038", "metadata": {}, "outputs": [], @@ -1495,7 +1494,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 62, "id": "3168489b", "metadata": {}, "outputs": [], @@ -1534,7 +1533,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 63, "id": "62267fa3", "metadata": {}, "outputs": [], @@ -1544,7 +1543,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 64, "id": "5f8fa829", "metadata": {}, "outputs": [], @@ -1554,7 +1553,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 65, "id": "3d129b03", "metadata": {}, "outputs": [], @@ -1564,7 +1563,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 66, "id": "030179b6", "metadata": {}, "outputs": [], @@ -1574,7 +1573,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 67, "id": "d737b468", "metadata": {}, "outputs": [], @@ -1584,7 +1583,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 68, "id": "77a74879", "metadata": {}, "outputs": [], @@ -1594,7 +1593,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 69, "id": "0521d267", "metadata": {}, "outputs": [], @@ -1604,7 +1603,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 70, "id": "468a31e9", "metadata": {}, "outputs": [], @@ -1614,7 +1613,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 71, "id": "abbe3ebf", "metadata": {}, "outputs": [], @@ -1624,7 +1623,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 72, "id": "651295e4", "metadata": {}, "outputs": [], @@ -1645,7 +1644,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 73, "id": "0a4ee482", "metadata": {}, "outputs": [], @@ -1665,7 +1664,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 74, "id": "956ed6d7", "metadata": { "tags": [] @@ -1677,7 +1676,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 75, "id": "a994eaa6", "metadata": { "tags": [] @@ -1689,7 +1688,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 76, "id": "4318028d", "metadata": { "tags": [] @@ -1701,7 +1700,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 77, "id": "05208c8b", "metadata": { "tags": [] @@ -1734,7 +1733,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 78, "id": "7eb85c5c", "metadata": {}, "outputs": [], @@ -1754,7 +1753,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 79, "id": "687a3e5a", "metadata": { "tags": [] @@ -1766,7 +1765,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 80, "id": "c49e9749", "metadata": { "tags": [] @@ -1778,7 +1777,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 81, "id": "8497dec4", "metadata": { "tags": [] @@ -1800,7 +1799,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 82, "id": "76be4d15", "metadata": { "tags": [] @@ -1839,7 +1838,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 83, "id": "0bcba5fe", "metadata": {}, "outputs": [], @@ -1859,7 +1858,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 84, "id": "4b6656e6", "metadata": { "tags": [] @@ -1871,7 +1870,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 85, "id": "36d9e92a", "metadata": { "tags": [] @@ -1883,7 +1882,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 86, "id": "1d944b42", "metadata": { "tags": [] @@ -1895,7 +1894,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 87, "id": "63ec57c9", "metadata": { "tags": [] @@ -1925,7 +1924,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 88, "id": "4e067bfb", "metadata": {}, "outputs": [], @@ -1945,7 +1944,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 89, "id": "2a7c1c21", "metadata": { "tags": [] @@ -1957,7 +1956,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 90, "id": "5df00229", "metadata": { "tags": [] diff --git a/chapters/chap07.ipynb b/chapters/chap07.ipynb index 6370e83..9d9c634 100644 --- a/chapters/chap07.ipynb +++ b/chapters/chap07.ipynb @@ -815,7 +815,7 @@ "## Search\n", "\n", "Based on this simpler version of `has_e`, let's write a more general function called `uses_any` that takes a second parameter that is a string of letters.\n", - "If returns `True` if the word uses any of the letters and `False` otherwise." + "It returns `True` if the word uses any of the letters and `False` otherwise." ] }, { @@ -1076,7 +1076,7 @@ " A variable used to count something, usually initialized to zero and then incremented.\n", "\n", "**linear search:**\n", - "A computational pattern that searches through a sequence of elements and stops what it finds what it is looking for.\n", + "A computational pattern that searches through a sequence of elements and stops when it finds what it is looking for.\n", "\n", "**pass:**\n", "If a test runs and the result is as expected, the test passes.\n", diff --git a/chapters/chap08.ipynb b/chapters/chap08.ipynb index 1323762..0108312 100644 --- a/chapters/chap08.ipynb +++ b/chapters/chap08.ipynb @@ -862,7 +862,7 @@ "id": "34c93df3", "metadata": {}, "source": [ - "The `endswidth` method checks whether a string ends with a given sequence of characters." + "The `endswith` method checks whether a string ends with a given sequence of characters." ] }, { diff --git a/chapters/chap09.ipynb b/chapters/chap09.ipynb index 93f8fd6..755c656 100644 --- a/chapters/chap09.ipynb +++ b/chapters/chap09.ipynb @@ -895,7 +895,7 @@ "id": "f90bd9ea", "metadata": {}, "source": [ - "The result it a list.\n", + "The result is a list.\n", "To convert the list to a string, we can use `join`." ] }, @@ -1746,7 +1746,7 @@ "id": "8fc79a2f", "metadata": {}, "source": [ - "Of if you want them in a string, you can use the `join` method." + "Or if you want them in a string, you can use the `join` method." ] }, { diff --git a/chapters/chap10.ipynb b/chapters/chap10.ipynb index 51dac26..742da86 100644 --- a/chapters/chap10.ipynb +++ b/chapters/chap10.ipynb @@ -477,7 +477,7 @@ "metadata": {}, "source": [ "The following function loops through the words in the list.\n", - "For each one, it reverses the letters and then checks whether the reversed word in the word list." + "For each one, it reverses the letters and then checks whether the reversed word is in the word list." ] }, { @@ -505,6 +505,17 @@ "If it doesn't find what it's looking for -- which happens most of the time -- it has to search all the way to the end." ] }, + { + "cell_type": "markdown", + "id": "fac41347", + "metadata": { + "tags": [] + }, + "source": [ + "To measure how long a function takes, we can use `%time` which is one of Jupyter's \"built-in magic commands\".\n", + "These commands are not part of the Python language, so they might not work in other development environments." + ] + }, { "cell_type": "code", "execution_count": 58, @@ -590,18 +601,6 @@ "This function takes less than one hundredth of a second, so it's about 10,000 times faster than the previous version." ] }, - { - "cell_type": "code", - "execution_count": 62, - "id": "82b36568", - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ - "%time much_faster()" - ] - }, { "cell_type": "markdown", "id": "4cd91c99", @@ -613,14 +612,14 @@ }, { "cell_type": "code", - "execution_count": 63, - "id": "aa079ed3", - "metadata": {}, + "execution_count": 62, + "id": "82b36568", + "metadata": { + "tags": [] + }, "outputs": [], "source": [ - "d = {'a': 1, 'b': 2}\n", - "d['a'] = 3\n", - "d" + "%time much_faster()" ] }, { @@ -1213,17 +1212,6 @@ "In the notebook for this chapter, you'll see where these measurements come from." ] }, - { - "cell_type": "markdown", - "id": "c6f39f84", - "metadata": { - "tags": [] - }, - "source": [ - "To measure how long a function takes, we can use `%time` which is one of Jupyter's \"built-in magic commands\".\n", - "These commands are not part of the Python language, so they might not work in other development environments." - ] - }, { "cell_type": "code", "execution_count": 50, @@ -1376,7 +1364,7 @@ "id": "170f1deb", "metadata": {}, "source": [ - "### Ask an assistant\n", + "### Ask a virtual assistant\n", "\n", "In this chapter, I said the keys in a dictionary have to be hashable and I gave a short explanation. If you would like more details, ask a virtual assistant, \"Why do keys in Python dictionaries have to be hashable?\"\n", "\n", @@ -1571,7 +1559,7 @@ "source": [ "### Exercise\n", "\n", - "Write function called `find_repeats` that takes a dictionary that maps from each key to a counter, like the result from `value_counts`.\n", + "Write a function called `find_repeats` that takes a dictionary that maps from each key to a counter, like the result from `value_counts`.\n", "It should loop through the dictionary and return a list of keys that have counts greater than `1`.\n", "You can use the following outline to get started." ] @@ -1809,10 +1797,16 @@ "cell_type": "code", "execution_count": 70, "id": "e04a5c73", - "metadata": {}, + "metadata": { + "tags": [] + }, "outputs": [], "source": [ - "# Solution goes here" + "for word in word_list:\n", + " if len(word) >= 8 and is_interlocking(word):\n", + " first = word[0::2]\n", + " second = word[1::2]\n", + " print(word, first, second)" ] }, { diff --git a/chapters/chap12.ipynb b/chapters/chap12.ipynb index f3f7b19..f2d5d23 100644 --- a/chapters/chap12.ipynb +++ b/chapters/chap12.ipynb @@ -80,7 +80,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 2, "id": "4cd1c980", "metadata": { "tags": [] @@ -103,7 +103,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 3, "id": "52ebfe94", "metadata": { "tags": [] @@ -116,7 +116,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 4, "id": "49cfc352", "metadata": { "tags": [] @@ -142,7 +142,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 5, "id": "44e53ce6", "metadata": {}, "outputs": [], @@ -152,7 +152,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 6, "id": "50d1fafa", "metadata": { "tags": [] @@ -173,7 +173,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 7, "id": "16d24028", "metadata": {}, "outputs": [], @@ -201,7 +201,7 @@ }, { "cell_type": "code", - "execution_count": 88, + "execution_count": 8, "id": "1668e6bd", "metadata": {}, "outputs": [], @@ -241,7 +241,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 9, "id": "ed5f0a43", "metadata": {}, "outputs": [], @@ -261,7 +261,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 10, "id": "a9df2aeb", "metadata": {}, "outputs": [], @@ -283,7 +283,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 11, "id": "b138b123", "metadata": {}, "outputs": [], @@ -305,7 +305,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 12, "id": "fe65df44", "metadata": {}, "outputs": [], @@ -324,7 +324,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 13, "id": "b47a87cf", "metadata": {}, "outputs": [], @@ -347,7 +347,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 14, "id": "348949be", "metadata": {}, "outputs": [], @@ -366,7 +366,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 15, "id": "06121901", "metadata": {}, "outputs": [], @@ -385,7 +385,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 16, "id": "881ed9f8", "metadata": {}, "outputs": [], @@ -403,7 +403,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 17, "id": "ab5d2fed", "metadata": {}, "outputs": [], @@ -421,7 +421,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 18, "id": "2fdfb936", "metadata": {}, "outputs": [], @@ -446,7 +446,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 19, "id": "3104d191", "metadata": {}, "outputs": [], @@ -474,7 +474,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 20, "id": "4fba7d1c", "metadata": {}, "outputs": [], @@ -502,7 +502,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 21, "id": "4be34c95", "metadata": {}, "outputs": [], @@ -520,12 +520,12 @@ "\n", "* `key=second_element` means the items will be sorted according to the frequencies of the words.\n", "\n", - "* `reverse=True` means they items will be sorted in reverse order, with the most frequent words first." + "* `reverse=True` means the items will be sorted in reverse order, with the most frequent words first." ] }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 22, "id": "8efe7c4c", "metadata": {}, "outputs": [], @@ -543,7 +543,7 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 23, "id": "79c17341", "metadata": {}, "outputs": [], @@ -574,7 +574,7 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 24, "id": "838bcb4f", "metadata": {}, "outputs": [], @@ -593,7 +593,7 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 25, "id": "90c45e7e", "metadata": {}, "outputs": [], @@ -617,7 +617,7 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 26, "id": "e106be95", "metadata": {}, "outputs": [], @@ -635,7 +635,7 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": 27, "id": "8101a510", "metadata": {}, "outputs": [], @@ -655,7 +655,7 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": 28, "id": "c046117b", "metadata": { "tags": [] @@ -697,7 +697,7 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 29, "id": "edd8ff1c", "metadata": { "tags": [] @@ -717,7 +717,7 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": 30, "id": "67ef3e08", "metadata": {}, "outputs": [], @@ -730,12 +730,12 @@ "id": "22becbab", "metadata": {}, "source": [ - "The we'll store the words as keys in a dictionary so we can use the `in` operator to check quickly whether a word is valid." + "Then we'll store the words as keys in a dictionary so we can use the `in` operator to check quickly whether a word is valid." ] }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 31, "id": "471d58e9", "metadata": {}, "outputs": [], @@ -755,7 +755,7 @@ }, { "cell_type": "code", - "execution_count": 34, + "execution_count": 32, "id": "4d4c3538", "metadata": {}, "outputs": [], @@ -778,7 +778,7 @@ }, { "cell_type": "code", - "execution_count": 35, + "execution_count": 33, "id": "8b42e014", "metadata": {}, "outputs": [], @@ -796,7 +796,7 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": 34, "id": "f48be152", "metadata": {}, "outputs": [], @@ -817,7 +817,7 @@ }, { "cell_type": "code", - "execution_count": 37, + "execution_count": 35, "id": "5716f967", "metadata": {}, "outputs": [], @@ -838,7 +838,7 @@ }, { "cell_type": "code", - "execution_count": 38, + "execution_count": 36, "id": "b37219f5", "metadata": {}, "outputs": [], @@ -880,7 +880,7 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": 37, "id": "75b548a9", "metadata": {}, "outputs": [], @@ -890,7 +890,7 @@ }, { "cell_type": "code", - "execution_count": 40, + "execution_count": 38, "id": "2bfa31ae", "metadata": { "tags": [] @@ -913,7 +913,7 @@ }, { "cell_type": "code", - "execution_count": 41, + "execution_count": 39, "id": "6f5d5c1c", "metadata": {}, "outputs": [], @@ -932,7 +932,7 @@ }, { "cell_type": "code", - "execution_count": 42, + "execution_count": 40, "id": "1445068b", "metadata": {}, "outputs": [], @@ -952,7 +952,7 @@ }, { "cell_type": "code", - "execution_count": 43, + "execution_count": 41, "id": "4fc47ecd", "metadata": { "tags": [] @@ -974,7 +974,7 @@ }, { "cell_type": "code", - "execution_count": 44, + "execution_count": 42, "id": "91ae9d4c", "metadata": {}, "outputs": [], @@ -993,7 +993,7 @@ }, { "cell_type": "code", - "execution_count": 45, + "execution_count": 43, "id": "8bf595c1", "metadata": {}, "outputs": [], @@ -1016,7 +1016,7 @@ }, { "cell_type": "code", - "execution_count": 46, + "execution_count": 44, "id": "22953b65", "metadata": {}, "outputs": [], @@ -1034,7 +1034,7 @@ }, { "cell_type": "code", - "execution_count": 47, + "execution_count": 45, "id": "1c7cdf4d", "metadata": {}, "outputs": [], @@ -1052,7 +1052,7 @@ }, { "cell_type": "code", - "execution_count": 48, + "execution_count": 46, "id": "a7a3aa42", "metadata": {}, "outputs": [], @@ -1071,7 +1071,7 @@ }, { "cell_type": "code", - "execution_count": 49, + "execution_count": 47, "id": "c4286fb3", "metadata": {}, "outputs": [], @@ -1111,7 +1111,7 @@ }, { "cell_type": "code", - "execution_count": 50, + "execution_count": 48, "id": "d8ee02f6", "metadata": {}, "outputs": [], @@ -1131,7 +1131,7 @@ }, { "cell_type": "code", - "execution_count": 51, + "execution_count": 49, "id": "bfdb1de1", "metadata": {}, "outputs": [], @@ -1158,7 +1158,7 @@ }, { "cell_type": "code", - "execution_count": 52, + "execution_count": 50, "id": "2e73df79", "metadata": {}, "outputs": [], @@ -1176,7 +1176,7 @@ }, { "cell_type": "code", - "execution_count": 53, + "execution_count": 51, "id": "495ad429", "metadata": {}, "outputs": [], @@ -1206,7 +1206,7 @@ }, { "cell_type": "code", - "execution_count": 54, + "execution_count": 52, "id": "c1224061", "metadata": {}, "outputs": [], @@ -1228,7 +1228,7 @@ }, { "cell_type": "code", - "execution_count": 55, + "execution_count": 53, "id": "4296485a", "metadata": {}, "outputs": [], @@ -1247,7 +1247,7 @@ }, { "cell_type": "code", - "execution_count": 56, + "execution_count": 54, "id": "e03fd803", "metadata": { "tags": [] @@ -1259,7 +1259,7 @@ }, { "cell_type": "code", - "execution_count": 57, + "execution_count": 55, "id": "f6ee1840", "metadata": {}, "outputs": [], @@ -1282,7 +1282,7 @@ }, { "cell_type": "code", - "execution_count": 58, + "execution_count": 56, "id": "d6c65d79", "metadata": {}, "outputs": [], @@ -1312,7 +1312,7 @@ }, { "cell_type": "code", - "execution_count": 59, + "execution_count": 57, "id": "3171d592", "metadata": {}, "outputs": [], @@ -1335,7 +1335,7 @@ }, { "cell_type": "code", - "execution_count": 60, + "execution_count": 58, "id": "3321e6a4", "metadata": {}, "outputs": [], @@ -1353,7 +1353,7 @@ }, { "cell_type": "code", - "execution_count": 61, + "execution_count": 59, "id": "e4e55c71", "metadata": {}, "outputs": [], @@ -1372,7 +1372,7 @@ }, { "cell_type": "code", - "execution_count": 62, + "execution_count": 60, "id": "f25dcb5e", "metadata": {}, "outputs": [], @@ -1391,7 +1391,7 @@ }, { "cell_type": "code", - "execution_count": 63, + "execution_count": 61, "id": "990354a0", "metadata": {}, "outputs": [], @@ -1413,7 +1413,7 @@ }, { "cell_type": "code", - "execution_count": 64, + "execution_count": 62, "id": "b9371452", "metadata": {}, "outputs": [], @@ -1441,7 +1441,7 @@ }, { "cell_type": "code", - "execution_count": 65, + "execution_count": 63, "id": "8c3f45c2", "metadata": {}, "outputs": [], @@ -1464,7 +1464,7 @@ }, { "cell_type": "code", - "execution_count": 66, + "execution_count": 64, "id": "641990a3", "metadata": {}, "outputs": [], @@ -1487,7 +1487,7 @@ }, { "cell_type": "code", - "execution_count": 67, + "execution_count": 65, "id": "9322a49a", "metadata": {}, "outputs": [], @@ -1509,7 +1509,7 @@ }, { "cell_type": "code", - "execution_count": 68, + "execution_count": 66, "id": "45a60c52", "metadata": {}, "outputs": [], @@ -1533,7 +1533,7 @@ }, { "cell_type": "code", - "execution_count": 69, + "execution_count": 67, "id": "3e86102c", "metadata": { "tags": [] @@ -1553,7 +1553,7 @@ }, { "cell_type": "code", - "execution_count": 70, + "execution_count": 68, "id": "e49d52f7", "metadata": {}, "outputs": [], @@ -1590,7 +1590,7 @@ }, { "cell_type": "code", - "execution_count": 71, + "execution_count": 69, "id": "15108884", "metadata": {}, "outputs": [], @@ -1602,7 +1602,7 @@ }, { "cell_type": "code", - "execution_count": 72, + "execution_count": 70, "id": "747a41be", "metadata": { "tags": [] @@ -1626,7 +1626,7 @@ }, { "cell_type": "code", - "execution_count": 73, + "execution_count": 71, "id": "5a4682dc", "metadata": {}, "outputs": [], @@ -1647,7 +1647,7 @@ }, { "cell_type": "code", - "execution_count": 74, + "execution_count": 72, "id": "36ee0f76", "metadata": {}, "outputs": [], @@ -1782,7 +1782,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 73, "id": "05752b6d", "metadata": { "tags": [] @@ -1807,7 +1807,7 @@ }, { "cell_type": "code", - "execution_count": 75, + "execution_count": 74, "id": "a4365ac0", "metadata": {}, "outputs": [], @@ -1827,7 +1827,7 @@ "metadata": {}, "source": [ "Dictionaries provide a method called `setdefault` that we can use to do the same thing more concisely.\n", - "Ask a virtual assistant how it works, or copy `add_word` into a virtual assistant and ask \"Can you rewrite this using `setdefault`?\"\n", + "Ask a virtual assistant how it works, or copy `add_bigram` into a virtual assistant and ask \"Can you rewrite this using `setdefault`?\"\n", "\n", "In this chapter we implemented Markov chain text analysis and generation.\n", "If you are curious, you can ask a virtual assistant for more information on the topic.\n", @@ -1850,7 +1850,7 @@ }, { "cell_type": "code", - "execution_count": 76, + "execution_count": 75, "id": "f38a61ff", "metadata": {}, "outputs": [], @@ -1860,7 +1860,7 @@ }, { "cell_type": "code", - "execution_count": 77, + "execution_count": 76, "id": "d047e546", "metadata": {}, "outputs": [], @@ -1880,7 +1880,7 @@ }, { "cell_type": "code", - "execution_count": 78, + "execution_count": 77, "id": "6b8932ee", "metadata": { "tags": [] @@ -1908,7 +1908,7 @@ }, { "cell_type": "code", - "execution_count": 79, + "execution_count": 78, "id": "44c3f0d8", "metadata": { "tags": [] @@ -1932,7 +1932,7 @@ }, { "cell_type": "code", - "execution_count": 80, + "execution_count": 79, "id": "3fcf85f4", "metadata": {}, "outputs": [], @@ -1950,7 +1950,7 @@ }, { "cell_type": "code", - "execution_count": 81, + "execution_count": 80, "id": "d9e554e3", "metadata": {}, "outputs": [], @@ -1975,7 +1975,7 @@ }, { "cell_type": "code", - "execution_count": 82, + "execution_count": 81, "id": "8c2ee21c", "metadata": { "tags": [] @@ -2003,7 +2003,7 @@ }, { "cell_type": "code", - "execution_count": 83, + "execution_count": 82, "id": "b13384e3", "metadata": { "tags": [] @@ -2023,7 +2023,7 @@ }, { "cell_type": "code", - "execution_count": 84, + "execution_count": 83, "id": "62c2177f", "metadata": {}, "outputs": [], @@ -2057,7 +2057,7 @@ }, { "cell_type": "code", - "execution_count": 85, + "execution_count": 84, "id": "64e11f26", "metadata": { "tags": [] @@ -2081,7 +2081,7 @@ }, { "cell_type": "code", - "execution_count": 86, + "execution_count": 85, "id": "fe2d93fa", "metadata": {}, "outputs": [], @@ -2109,7 +2109,7 @@ }, { "cell_type": "code", - "execution_count": 89, + "execution_count": 86, "id": "22210a5c", "metadata": {}, "outputs": [], diff --git a/chapters/chap13.ipynb b/chapters/chap13.ipynb index b826989..ba90420 100644 --- a/chapters/chap13.ipynb +++ b/chapters/chap13.ipynb @@ -1587,9 +1587,7 @@ { "cell_type": "markdown", "id": "85844afb", - "metadata": { - "tags": [] - }, + "metadata": {}, "source": [ "Here's an outline of the function to get you started." ] @@ -1601,7 +1599,14 @@ "metadata": {}, "outputs": [], "source": [ - "# Solution goes here" + "def replace_all(old, new, source_path, dest_path):\n", + " # read the contents of the source file\n", + " reader = open(source_path)\n", + "\n", + " # replace the old string with the new\n", + " \n", + " # write the result into the destination file\n", + " " ] }, { diff --git a/chapters/chap14.ipynb b/chapters/chap14.ipynb index b71f706..f0f9998 100644 --- a/chapters/chap14.ipynb +++ b/chapters/chap14.ipynb @@ -359,7 +359,7 @@ "metadata": {}, "source": [ "It might be surprising that the parameters have the same names as the attributes, but that's a common way to write a function like this.\n", - "Here's how we use `make_time` to create a `Time` object.`" + "Here's how we use `make_time` to create a `Time` object." ] }, { @@ -907,7 +907,7 @@ " \n", " carry, time.second = divmod(time.second, 60)\n", " carry, time.minute = divmod(time.minute + carry, 60)\n", - " carry, time.hour = divmod(time.hour + carry, 60)" + " carry, time.hour = divmod(time.hour + carry, 24)" ] }, { @@ -981,7 +981,7 @@ "metadata": {}, "source": [ "The result is the number of seconds since the beginning of the day.\n", - "For example, `01:01:01` is `1` hour, `1` minute and `1` second from the beginning of the day, with is the sum of `3600` seconds, `60` seconds, and `1` second." + "For example, `01:01:01` is `1` hour, `1` minute and `1` second from the beginning of the day, which is the sum of `3600` seconds, `60` seconds, and `1` second." ] }, { @@ -1307,14 +1307,6 @@ "Also, in this chapter we saw one example of a format specifier. For more information, ask \"What format specifiers can be used in a Python f-string?\"" ] }, - { - "cell_type": "markdown", - "id": "c85eab62", - "metadata": {}, - "source": [ - "## Exercises\n" - ] - }, { "cell_type": "markdown", "id": "bcdab7d6", diff --git a/chapters/chap15.ipynb b/chapters/chap15.ipynb index 91d1271..f7dfa60 100644 --- a/chapters/chap15.ipynb +++ b/chapters/chap15.ipynb @@ -373,7 +373,7 @@ "id": "e6a18c76", "metadata": {}, "source": [ - "Now that we have `Time.from_seconds`, we can use it to write `add_time` as a method.\n", + "Now that we have `Time.int_to_time`, we can use it to write `add_time` as a method.\n", "Here's the function from the previous chapter." ] }, @@ -409,7 +409,7 @@ "\n", " def add_time(self, hours, minutes, seconds):\n", " duration = make_time(hours, minutes, seconds)\n", - " seconds = time_to_int(self) + time_to_int(duration)\n", + " seconds = Time.time_to_int(self) + Time.time_to_int(duration)\n", " return Time.int_to_time(seconds)" ] }, @@ -431,7 +431,7 @@ "outputs": [], "source": [ "end = start.add_time(1, 32, 0)\n", - "print_time(end)" + "end.print_time()" ] }, { @@ -604,7 +604,7 @@ "id": "e01e9673", "metadata": {}, "source": [ - "## The __init__ method\n", + "## The init method\n", "\n", "The most special of the special methods is `__init__`, so-called because it initializes the attributes of a new object.\n", "An `__init__` method for the `Time` class might look like this:" @@ -836,7 +836,7 @@ "\n", " def is_after(self, other):\n", " assert self.is_valid(), 'self is not a valid Time'\n", - " assert other.is_valid(), 'self is not a valid Time'\n", + " assert other.is_valid(), 'other is not a valid Time'\n", " return self.time_to_int() > other.time_to_int()" ] }, @@ -924,7 +924,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 36, "id": "3115ea33", "metadata": { "tags": [] @@ -960,7 +960,7 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": 37, "id": "133d7679", "metadata": {}, "outputs": [], @@ -983,16 +983,16 @@ "\n", "2. Write an `__init__` method that takes `year`, `month`, and `day` as parameters and assigns the parameters to attributes. Create an object that represents June 22, 1933.\n", "\n", - "2. Write `__str__` method that uses an f-string to format the attributes and returns the result. If you test it with the `Date` you created, the result should be `1933-06-22`.\n", + "3. Write `__str__` method that uses an f-string to format the attributes and returns the result. If you test it with the `Date` you created, the result should be `1933-06-22`.\n", "\n", - "3. Write a method called `is_after` that takes two `Date` objects and returns `True` if the first comes after the second. Create a second object that represents September 17, 1933, and check whether it comes after the first object.\n", + "4. Write a method called `is_after` that takes two `Date` objects and returns `True` if the first comes after the second. Create a second object that represents September 17, 1933, and check whether it comes after the first object.\n", "\n", "Hint: You might find it useful write a method called `to_tuple` that returns a tuple that contains the attributes of a `Date` object in year-month-day order." ] }, { "cell_type": "code", - "execution_count": 37, + "execution_count": 38, "id": "3c9f3777-4869-481e-9f4e-4223d6028913", "metadata": {}, "outputs": [], @@ -1012,7 +1012,7 @@ }, { "cell_type": "code", - "execution_count": 38, + "execution_count": 39, "id": "fd4b2521-aa71-45da-97eb-ce62ce2714ad", "metadata": { "tags": [] @@ -1025,7 +1025,7 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": 40, "id": "ee3f1294-cad1-406b-a574-045ad2b84294", "metadata": { "tags": [] @@ -1038,7 +1038,7 @@ }, { "cell_type": "code", - "execution_count": 40, + "execution_count": 41, "id": "ac093f7b-83cf-4488-8842-5c71bcfa35ec", "metadata": { "tags": [] @@ -1050,7 +1050,7 @@ }, { "cell_type": "code", - "execution_count": 41, + "execution_count": 42, "id": "7e7cb5e1-631f-4b1e-874f-eb16d4792625", "metadata": { "tags": [] diff --git a/chapters/chap16.ipynb b/chapters/chap16.ipynb index 01fd85b..eb84e1c 100644 --- a/chapters/chap16.ipynb +++ b/chapters/chap16.ipynb @@ -234,7 +234,7 @@ "id": "a7a635ee", "metadata": {}, "source": [ - "In the same way that the built in function `sort` modifies a list, and the `sorted` function creates a new list, now we have a `translate` method that modifies a `Point` and a `translated` method that creates a new one.\n", + "In the same way that the `sort` method modifies a list and the `sorted` function creates a new list, now we have a `translate` method that modifies a `Point` and a `translated` method that creates a new one.\n", "\n", "Here's an example:" ] @@ -1279,9 +1279,7 @@ { "cell_type": "markdown", "id": "2e488e0f", - "metadata": { - "tags": [] - }, + "metadata": {}, "source": [ "You can use the following outline to get started." ] @@ -1290,9 +1288,7 @@ "cell_type": "code", "execution_count": 49, "id": "92c07380", - "metadata": { - "tags": [] - }, + "metadata": {}, "outputs": [], "source": [ "%%add_method_to Line\n", @@ -1310,22 +1306,13 @@ }, "outputs": [], "source": [ - "%%add_method_to Line\n", - "\n", - "def __eq__(self, other):\n", - " if (self.p1 == other.p1) and (self.p2 == other.p2):\n", - " return True\n", - " if (self.p1 == other.p2) and (self.p2 == other.p1):\n", - " return True\n", - " return False" + "# Solution goes here" ] }, { "cell_type": "markdown", "id": "3a44e45a", - "metadata": { - "tags": [] - }, + "metadata": {}, "source": [ "You can use these examples to test your code." ] @@ -1334,9 +1321,7 @@ "cell_type": "code", "execution_count": 51, "id": "aa086dd1", - "metadata": { - "tags": [] - }, + "metadata": {}, "outputs": [], "source": [ "start1 = Point(0, 0)\n", @@ -1347,9 +1332,7 @@ { "cell_type": "markdown", "id": "e825f049", - "metadata": { - "tags": [] - }, + "metadata": {}, "source": [ "This example should be `True` because the `Line` objects refer to `Point` objects that are equivalent, in the same order." ] @@ -1358,9 +1341,7 @@ "cell_type": "code", "execution_count": 52, "id": "857cba26", - "metadata": { - "tags": [] - }, + "metadata": {}, "outputs": [], "source": [ "line_a = Line(start1, end)\n", @@ -1382,9 +1363,7 @@ "cell_type": "code", "execution_count": 53, "id": "b45def0a", - "metadata": { - "tags": [] - }, + "metadata": {}, "outputs": [], "source": [ "line_c = Line(end, start1)\n", @@ -1394,9 +1373,7 @@ { "cell_type": "markdown", "id": "8c9c787b", - "metadata": { - "tags": [] - }, + "metadata": {}, "source": [ "Equivalence should always be transitive -- that is, if `line_a` and `line_b` are equivalent, and `line_a` and `line_c` are equivalent, then `line_b` and `line_c` should also be equivalent." ] @@ -1405,9 +1382,7 @@ "cell_type": "code", "execution_count": 54, "id": "9784300c", - "metadata": { - "tags": [] - }, + "metadata": {}, "outputs": [], "source": [ "line_b == line_c # should be True" @@ -1416,9 +1391,7 @@ { "cell_type": "markdown", "id": "d4f385fa", - "metadata": { - "tags": [] - }, + "metadata": {}, "source": [ "This example should be `False` because the `Line` objects refer to `Point` objects that are not equivalent." ] @@ -1427,9 +1400,7 @@ "cell_type": "code", "execution_count": 55, "id": "5435c8e4", - "metadata": { - "tags": [] - }, + "metadata": {}, "outputs": [], "source": [ "line_d = Line(start1, start2)\n", @@ -1449,9 +1420,7 @@ { "cell_type": "markdown", "id": "b8c52d19", - "metadata": { - "tags": [] - }, + "metadata": {}, "source": [ "You can use the following outline to get started." ] @@ -1460,9 +1429,7 @@ "cell_type": "code", "execution_count": 56, "id": "f377afbb", - "metadata": { - "tags": [] - }, + "metadata": {}, "outputs": [], "source": [ "%%add_method_to Line\n", @@ -1480,20 +1447,13 @@ }, "outputs": [], "source": [ - "%%add_method_to Line\n", - "\n", - " def midpoint(self):\n", - " mid_x = (self.p1.x + self.p2.x) / 2\n", - " mid_y = (self.p1.y + self.p2.y) / 2\n", - " return Point(mid_x, mid_y)" + "# Solution goes here" ] }, { "cell_type": "markdown", "id": "4df69a9f", - "metadata": { - "tags": [] - }, + "metadata": {}, "source": [ "You can use the following examples to test your code and draw the result." ] @@ -1502,9 +1462,7 @@ "cell_type": "code", "execution_count": 58, "id": "0d603aa3", - "metadata": { - "tags": [] - }, + "metadata": {}, "outputs": [], "source": [ "start = Point(0, 0)\n", @@ -1518,9 +1476,7 @@ "cell_type": "code", "execution_count": 59, "id": "647d0982", - "metadata": { - "tags": [] - }, + "metadata": {}, "outputs": [], "source": [ "mid1 = line1.midpoint()\n", @@ -1531,9 +1487,7 @@ "cell_type": "code", "execution_count": 60, "id": "e351bea3", - "metadata": { - "tags": [] - }, + "metadata": {}, "outputs": [], "source": [ "mid2 = line2.midpoint()\n", @@ -1544,9 +1498,7 @@ "cell_type": "code", "execution_count": 61, "id": "5ad5a076", - "metadata": { - "tags": [] - }, + "metadata": {}, "outputs": [], "source": [ "line3 = Line(mid1, mid2)" @@ -1556,9 +1508,7 @@ "cell_type": "code", "execution_count": 62, "id": "8effaff0", - "metadata": { - "tags": [] - }, + "metadata": {}, "outputs": [], "source": [ "make_turtle()\n", @@ -1580,9 +1530,7 @@ { "cell_type": "markdown", "id": "c586a3ed", - "metadata": { - "tags": [] - }, + "metadata": {}, "source": [ "You can use the following outline to get started." ] @@ -1591,9 +1539,7 @@ "cell_type": "code", "execution_count": 63, "id": "d94a6350", - "metadata": { - "tags": [] - }, + "metadata": {}, "outputs": [], "source": [ "%%add_method_to Rectangle\n", @@ -1611,20 +1557,13 @@ }, "outputs": [], "source": [ - "%%add_method_to Rectangle\n", - "\n", - " def midpoint(self):\n", - " mid_x = self.corner.x + self.width / 2\n", - " mid_y = self.corner.y + self.height / 2\n", - " return Point(mid_x, mid_y)" + "# Solution goes here" ] }, { "cell_type": "markdown", "id": "d186c84b", - "metadata": { - "tags": [] - }, + "metadata": {}, "source": [ "You can use the following example to test your code." ] @@ -1633,9 +1572,7 @@ "cell_type": "code", "execution_count": 65, "id": "4aec759c", - "metadata": { - "tags": [] - }, + "metadata": {}, "outputs": [], "source": [ "corner = Point(30, 20)\n", @@ -1646,9 +1583,7 @@ "cell_type": "code", "execution_count": 66, "id": "7ec3339d", - "metadata": { - "tags": [] - }, + "metadata": {}, "outputs": [], "source": [ "mid = rectangle.midpoint()\n", @@ -1659,9 +1594,7 @@ "cell_type": "code", "execution_count": 67, "id": "326dbf24", - "metadata": { - "tags": [] - }, + "metadata": {}, "outputs": [], "source": [ "diagonal = Line(corner, mid)" @@ -1671,9 +1604,7 @@ "cell_type": "code", "execution_count": 68, "id": "4da710d4", - "metadata": { - "tags": [] - }, + "metadata": {}, "outputs": [], "source": [ "make_turtle()\n", @@ -1701,9 +1632,7 @@ { "cell_type": "markdown", "id": "29e994c6", - "metadata": { - "tags": [] - }, + "metadata": {}, "source": [ "You can use this outline to get started." ] @@ -1712,9 +1641,7 @@ "cell_type": "code", "execution_count": 69, "id": "30cc0726", - "metadata": { - "tags": [] - }, + "metadata": {}, "outputs": [], "source": [ "%%add_method_to Rectangle\n", @@ -1732,24 +1659,13 @@ }, "outputs": [], "source": [ - "%%add_method_to Rectangle\n", - "\n", - " def make_cross(self):\n", - " midpoints = []\n", - "\n", - " for line in self.make_lines():\n", - " midpoints.append(line.midpoint())\n", - "\n", - " p1, p2, p3, p4 = midpoints\n", - " return Line(p1, p3), Line(p2, p4)" + "# Solution goes here" ] }, { "cell_type": "markdown", "id": "970fcbca", - "metadata": { - "tags": [] - }, + "metadata": {}, "source": [ "You can use the following example to test your code." ] @@ -1758,9 +1674,7 @@ "cell_type": "code", "execution_count": 71, "id": "2afd718c", - "metadata": { - "tags": [] - }, + "metadata": {}, "outputs": [], "source": [ "corner = Point(30, 20)\n", @@ -1771,9 +1685,7 @@ "cell_type": "code", "execution_count": 72, "id": "b7bdb467", - "metadata": { - "tags": [] - }, + "metadata": {}, "outputs": [], "source": [ "lines = rectangle.make_cross()" @@ -1783,9 +1695,7 @@ "cell_type": "code", "execution_count": 73, "id": "9d09b2c3", - "metadata": { - "tags": [] - }, + "metadata": {}, "outputs": [], "source": [ "make_turtle()\n", @@ -1809,9 +1719,7 @@ { "cell_type": "markdown", "id": "cb1b24a3", - "metadata": { - "tags": [] - }, + "metadata": {}, "source": [ "You can use the following function, which is a version of the `circle` function we wrote in Chapter 4." ] @@ -1820,9 +1728,7 @@ "cell_type": "code", "execution_count": 74, "id": "b3d2328f", - "metadata": { - "tags": [] - }, + "metadata": {}, "outputs": [], "source": [ "from jupyturtle import make_turtle, forward, left, right\n", @@ -1852,9 +1758,7 @@ { "cell_type": "markdown", "id": "b4325143", - "metadata": { - "tags": [] - }, + "metadata": {}, "source": [ "You can use the following example to test your code.\n", "We'll start with a square `Rectangle` with width and height `100`." @@ -1864,9 +1768,7 @@ "cell_type": "code", "execution_count": 76, "id": "49074ed5", - "metadata": { - "tags": [] - }, + "metadata": {}, "outputs": [], "source": [ "corner = Point(20, 20)\n", @@ -1876,9 +1778,7 @@ { "cell_type": "markdown", "id": "2cdecfa9", - "metadata": { - "tags": [] - }, + "metadata": {}, "source": [ "The following code should create a `Circle` that fits inside the square." ] @@ -1887,9 +1787,7 @@ "cell_type": "code", "execution_count": 77, "id": "d65a9163", - "metadata": { - "tags": [] - }, + "metadata": {}, "outputs": [], "source": [ "center = rectangle.midpoint()\n", @@ -1902,9 +1800,7 @@ { "cell_type": "markdown", "id": "37e94d98", - "metadata": { - "tags": [] - }, + "metadata": {}, "source": [ "If everything worked correctly, the following code should draw the circle inside the square (touching on all four sides)." ] @@ -1913,9 +1809,7 @@ "cell_type": "code", "execution_count": 78, "id": "e3b23b4d", - "metadata": { - "tags": [] - }, + "metadata": {}, "outputs": [], "source": [ "make_turtle(delay=0.01)\n", @@ -1924,14 +1818,6 @@ "circle.draw()" ] }, - { - "cell_type": "code", - "execution_count": null, - "id": "48abaaae", - "metadata": {}, - "outputs": [], - "source": [] - }, { "cell_type": "markdown", "id": "a7f4edf8", diff --git a/chapters/chap17.ipynb b/chapters/chap17.ipynb index 7dadbc2..c26b8b7 100644 --- a/chapters/chap17.ipynb +++ b/chapters/chap17.ipynb @@ -1740,7 +1740,7 @@ "* `get_rank_counts` does the same thing with the ranks of the cards, returning a dictionary that maps from each rank code to the number of times it appears.\n", "\n", "All of the exercises that follow can be done using only the Python features we have learned so far, but some of them are more difficult than most of the previous exercises.\n", - "I encourage you to ask an AI for help.\n", + "I encourage you to ask a virtual assistant for help.\n", "\n", "For problems like this, it often works well to ask for general advice about strategies and algorithms.\n", "Then you can either write the code yourself or ask for code.\n", @@ -1934,7 +1934,7 @@ "Write a method called `has_straight` that checks whether a hand contains a straight, which is a set of five cards with consecutive ranks.\n", "For example, if a hand contains ranks `5`, `6`, `7`, `8`, and `9`, it contains a straight.\n", "\n", - "An Ace can come before a two or after a King, so `Ace`, `2`, `3`, `4`, `5` is a straight and so it `10`, `Jack`, `Queen`, `King`, `Ace`.\n", + "An Ace can come before a two or after a King, so `Ace`, `2`, `3`, `4`, `5` is a straight and so is `10`, `Jack`, `Queen`, `King`, `Ace`.\n", "But a straight cannot \"wrap around\", so `King`, `Ace`, `2`, `3`, `4` is not a straight." ] }, diff --git a/chapters/chap18.ipynb b/chapters/chap18.ipynb index 2cc91a5..3027822 100644 --- a/chapters/chap18.ipynb +++ b/chapters/chap18.ipynb @@ -1661,7 +1661,7 @@ "id": "07be77f3", "metadata": {}, "source": [ - "In this example, the value of `kwargs` is printed, but otherwise is has no effect.\n", + "In this example, the value of `kwargs` is printed, but otherwise it has no effect.\n", "\n", "But the `**` operator can also be used in an argument list to unpack a dictionary.\n", "For example, here's a version of `mean` that packs any keyword arguments it gets and then unpacks them as keyword arguments for `sum`." @@ -2013,7 +2013,6 @@ "### Ask a virtual assistant\n", "\n", "There are a few topics in this chapter you might want to learn about.\n", - "Here are some question to ask an AI.\n", "\n", "* \"What are the methods and operators of Python's set class?\"\n", "\n", @@ -2027,11 +2026,11 @@ "\n", "* \"How does `unittest` do test discovery?\"\n", "\n", - "\"Along with `assertEqual`, what are the most commonly used methods in `unittest.TestCase`?\"\n", + "* \"Along with `assertEqual`, what are the most commonly used methods in `unittest.TestCase`?\"\n", "\n", - "\"What are the pros and cons of `doctest` and `unittest`?\"\n", + "* \"What are the pros and cons of `doctest` and `unittest`?\"\n", "\n", - "For the following exercises, consider asking an AI for help, but as always, remember to test the results." + "For the following exercises, consider asking a virtual assistant for help, but as always, remember to test the results." ] }, { @@ -2064,7 +2063,7 @@ "metadata": {}, "source": [ "Write a version of this function that uses `set` operations instead of a `for` loop.\n", - "Hint: ask an AI \"How do I compute the intersection of Python sets?\"" + "Hint: ask a VA, \"How do I compute the intersection of Python sets?\"" ] }, { diff --git a/jb/_config.yml b/jb/_config.yml index 245575a..f0b261f 100644 --- a/jb/_config.yml +++ b/jb/_config.yml @@ -1,5 +1,5 @@ # Book settings -title: Think Python, 3rd edition +title: Think Python author: Allen B. Downey latex: @@ -14,6 +14,19 @@ repository: html: use_repository_button: true + extra_footer: | +
+ + Creative Commons License + +
+ This work is licensed under a + + Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License + . +
+ parse: # myst_extended_syntax: true # instead enable individual features below myst_enable_extensions: # https://myst-parser.readthedocs.io/en/latest/using/syntax-optional.html diff --git a/jb/_toc.yml b/jb/_toc.yml index e5e3fd4..6a5c362 100644 --- a/jb/_toc.yml +++ b/jb/_toc.yml @@ -1,23 +1,31 @@ format: jb-book root: index -chapters: -- file: chap00 -- file: chap01 -- file: chap02 -- file: chap03 -- file: chap04 -- file: chap05 -- file: chap06 -- file: chap07 -- file: chap08 -- file: chap09 -- file: chap10 -- file: chap11 -- file: chap12 -- file: chap13 -- file: chap14 -- file: chap15 -- file: chap16 -- file: chap17 -- file: chap18 -- file: chap19 +parts: +- caption: Front Matter + chapters: + - file: chap00 +- caption: Chapters + numbered: True + chapters: + - file: chap01 + - file: chap02 + - file: chap03 + - file: chap04 + - file: chap05 + - file: chap06 + - file: chap07 + - file: chap08 + - file: chap09 + - file: chap10 + - file: chap11 + - file: chap12 + - file: chap13 + - file: chap14 + - file: chap15 + - file: chap16 + - file: chap17 + - file: chap18 + - file: chap19 +- caption: End Matter + chapters: + - file: blank