Skip to content

Commit 7fa45c7

Browse files
rename data structures to correct type 😭 also add more exercises and solution
1 parent 16f9fca commit 7fa45c7

File tree

4 files changed

+154
-12
lines changed

4 files changed

+154
-12
lines changed
File renamed without changes.

Functions_and_Classes.ipynb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,47 @@
4141
],
4242
"id": "initial_id"
4343
},
44+
{
45+
"metadata": {},
46+
"cell_type": "markdown",
47+
"source": [
48+
"### practice exercise!! - greatest commmon divisor\n",
49+
"write a function that takes in two numbers and returns their greatest common divisor (gcd). the gcd is the largest number that divides both numbers without leaving a remainder. for example, the gcd of 12 and 15 is 3, because 3 is the largest number that will cleanly factor into 12 and 15.\n"
50+
],
51+
"id": "863527bde78d346d"
52+
},
53+
{
54+
"metadata": {
55+
"ExecuteTime": {
56+
"end_time": "2025-08-21T16:36:49.522342Z",
57+
"start_time": "2025-08-21T16:36:49.517259Z"
58+
}
59+
},
60+
"cell_type": "code",
61+
"source": [
62+
"## write your code for the gcd function here\n",
63+
"\n",
64+
"# your code here!!\n",
65+
"\n",
66+
"assert gcd(12, 15) == 3\n",
67+
"assert gcd(54, 24) == 6\n",
68+
"assert gcd(101, 10) == 1\n",
69+
"assert gcd(100, 25) == 25\n",
70+
"print(\"all unit tests are passing\")"
71+
],
72+
"id": "ff295ae2bf57e0a3",
73+
"outputs": [
74+
{
75+
"name": "stdout",
76+
"output_type": "stream",
77+
"text": [
78+
"3\n",
79+
"6\n"
80+
]
81+
}
82+
],
83+
"execution_count": 2
84+
},
4485
{
4586
"metadata": {},
4687
"cell_type": "markdown",

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ These notebooks are designed as lessons, and are split into ~1 hour chunks
66
# Table of Contents
77
- [Intro To Python](Intro_To_Python.ipynb)
88
- [Loops And Logic](Loops_And_Logic.ipynb)
9-
- [Arrays and Data Structures](Data_Sturctures.ipynb)
9+
- [Arrays and Data Structures](Data_Structures.ipynb)
1010
- [Functions and Classes](Functions_And_Classes.ipynb)

solutions.ipynb

Lines changed: 112 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
11
{
22
"cells": [
33
{
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"
148
},
159
{
1610
"metadata": {},
1711
"cell_type": "markdown",
1812
"source": [
19-
"## solution to data structures bonus challenge for tuples\n",
13+
"## solution to data structures bonus challenge for tuples - Data Structures\n",
2014
"colinear line segments!!"
2115
],
2216
"id": "890d8ecdf5c15887"
@@ -41,6 +35,113 @@
4135
" return False"
4236
],
4337
"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+
"![euclid proof](https://upload.wikimedia.org/wikipedia/commons/thumb/9/96/Euclid%27s_algorithm_Book_VII_Proposition_2_3.svg/500px-Euclid%27s_algorithm_Book_VII_Proposition_2_3.svg.png)\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"
44145
}
45146
],
46147
"metadata": {

0 commit comments

Comments
 (0)