|
1 | 1 | { |
2 | 2 | "cells": [ |
3 | 3 | { |
4 | | - "cell_type": "code", |
5 | | - "execution_count": null, |
6 | | - "id": "initial_id", |
7 | | - "metadata": { |
8 | | - "collapsed": true |
9 | | - }, |
10 | | - "outputs": [], |
11 | | - "source": [ |
12 | | - "" |
13 | | - ] |
| 4 | + "metadata": {}, |
| 5 | + "cell_type": "markdown", |
| 6 | + "source": "# solutions to all non-trivial practice exercises", |
| 7 | + "id": "5bf2778979e28449" |
14 | 8 | }, |
15 | 9 | { |
16 | 10 | "metadata": {}, |
17 | 11 | "cell_type": "markdown", |
18 | 12 | "source": [ |
19 | | - "## solution to data structures bonus challenge for tuples\n", |
| 13 | + "## solution to data structures bonus challenge for tuples - Data Structures\n", |
20 | 14 | "colinear line segments!!" |
21 | 15 | ], |
22 | 16 | "id": "890d8ecdf5c15887" |
|
41 | 35 | " return False" |
42 | 36 | ], |
43 | 37 | "id": "cc0208171d3eaacd" |
| 38 | + }, |
| 39 | + { |
| 40 | + "metadata": {}, |
| 41 | + "cell_type": "markdown", |
| 42 | + "source": [ |
| 43 | + "So, what we first did was unpack the tuples into their respective coordinates. Then, we could calculate the slope of each line segment using point slope. The if clause at the end checks for division by zero, and prevents it by issuing an \"infinity\" case.\n", |
| 44 | + "\n", |
| 45 | + "If the slopes of the two line segments are equal, that means that the lines would be parrallel and thus colinear. We then return True and False respectively." |
| 46 | + ], |
| 47 | + "id": "d3accfe0816836c3" |
| 48 | + }, |
| 49 | + { |
| 50 | + "metadata": {}, |
| 51 | + "cell_type": "markdown", |
| 52 | + "source": "## Greatest common divisor - functions and classes", |
| 53 | + "id": "b9285b1d168d7d01" |
| 54 | + }, |
| 55 | + { |
| 56 | + "metadata": { |
| 57 | + "ExecuteTime": { |
| 58 | + "end_time": "2025-08-21T16:52:24.365112Z", |
| 59 | + "start_time": "2025-08-21T16:52:24.355177Z" |
| 60 | + } |
| 61 | + }, |
| 62 | + "cell_type": "code", |
| 63 | + "source": [ |
| 64 | + "def gcd(a, b):\n", |
| 65 | + " while b:\n", |
| 66 | + " a, b = b, a % b\n", |
| 67 | + " print(a, b)\n", |
| 68 | + " return a\n", |
| 69 | + "gcd(12, 15) # should return 3" |
| 70 | + ], |
| 71 | + "id": "63826e5536435806", |
| 72 | + "outputs": [ |
| 73 | + { |
| 74 | + "name": "stdout", |
| 75 | + "output_type": "stream", |
| 76 | + "text": [ |
| 77 | + "15 12\n", |
| 78 | + "12 3\n", |
| 79 | + "3 0\n" |
| 80 | + ] |
| 81 | + }, |
| 82 | + { |
| 83 | + "data": { |
| 84 | + "text/plain": [ |
| 85 | + "3" |
| 86 | + ] |
| 87 | + }, |
| 88 | + "execution_count": 1, |
| 89 | + "metadata": {}, |
| 90 | + "output_type": "execute_result" |
| 91 | + } |
| 92 | + ], |
| 93 | + "execution_count": 1 |
| 94 | + }, |
| 95 | + { |
| 96 | + "metadata": {}, |
| 97 | + "cell_type": "markdown", |
| 98 | + "source": [ |
| 99 | + "While this is not the only way to find the greatest common divisor, it is a very efficient way to do so. This is known as the Euclidean algorithm, and it works by repeatedly replacing the larger number with the remainder of the division of the two numbers until one of them becomes zero. The other number at that point will be the GCD. For the proof to why this works, Reuben will explain, or you can consult [wikipedia](https://en.wikipedia.org/wiki/Euclidean_algorithm)\n", |
| 100 | + "\n", |
| 101 | + "Breaking down the code line by line, we first have a while loop that continues as long as b is not zero.\n", |
| 102 | + "In each iteration, we update the values of a and b. We set a to b, and b to the remainder of the division of a by b.\n", |
| 103 | + "If you remember tuple unpacking from the data structures section, this is a similar concept where we are unpacking the values of a and b into new values in a single line.\n", |
| 104 | + "\n", |
| 105 | + "When b becomes zero, we exit the loop and return a, which is now the GCD of the original two numbers." |
| 106 | + ], |
| 107 | + "id": "9e91636272b13eef" |
| 108 | + }, |
| 109 | + { |
| 110 | + "metadata": {}, |
| 111 | + "cell_type": "code", |
| 112 | + "outputs": [], |
| 113 | + "execution_count": null, |
| 114 | + "source": [ |
| 115 | + "def gcd(a,b):\n", |
| 116 | + " if b == 0:\n", |
| 117 | + " return a\n", |
| 118 | + " else:\n", |
| 119 | + " return gcd(b, a % b)\n", |
| 120 | + "print(gcd(25,5))\n", |
| 121 | + "print(gcd(72,86))" |
| 122 | + ], |
| 123 | + "id": "52c6955058a436a3" |
| 124 | + }, |
| 125 | + { |
| 126 | + "metadata": {}, |
| 127 | + "cell_type": "markdown", |
| 128 | + "source": [ |
| 129 | + "This is just a recursive version of the same algorithm. The base case here is when b is zero, so we return a. Euclid said something about how this works, when you have two numbers, a and b, you just keep dividing the larger number by the smaller number and replacing it with the remainder until one of them is zero, and the other number is the GCD. some other french guy did some other proof on this with sticks and funny looking figures but hey im not the math girl here.\n", |
| 130 | + "\n", |
| 131 | + "\n", |
| 132 | + "\n", |
| 133 | + "Euclid's method for finding the greatest common divisor (GCD) of two starting lengths BA and DC, both defined to be multiples of a common \"unit\" length. The length DC being shorter, it is used to \"measure\" BA, but only once because the remainder EA is less than DC. EA now measures (twice) the shorter length DC, with remainder FC shorter than EA. Then FC measures (three times) length EA. Because there is no remainder, the process ends with FC being the GCD. On the right Nicomachus's example with numbers 49 and 21 resulting in their GCD of 7 (derived from Heath 1908:300).\n", |
| 134 | + "- wikipedia" |
| 135 | + ], |
| 136 | + "id": "d76d86b3a3a9271f" |
| 137 | + }, |
| 138 | + { |
| 139 | + "metadata": {}, |
| 140 | + "cell_type": "code", |
| 141 | + "outputs": [], |
| 142 | + "execution_count": null, |
| 143 | + "source": "", |
| 144 | + "id": "134824d4f7570878" |
44 | 145 | } |
45 | 146 | ], |
46 | 147 | "metadata": { |
|
0 commit comments