Skip to content

Commit 990529b

Browse files
committed
finish tests for exercises
1 parent 74ad47b commit 990529b

File tree

1 file changed

+96
-18
lines changed

1 file changed

+96
-18
lines changed

week5/exercises.ipynb

Lines changed: 96 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
"cell_type": "markdown",
55
"metadata": {},
66
"source": [
7-
"These exercises will guide you through making a class to represent fractions in Python. With each task, you want to keep adding more functionality to this class!"
7+
"This week, the tasks are linked! They'll guide you through making a class to represent fractions in Python. \n",
8+
"\n",
9+
"*Each task will build towards adding more functionality to this unfinished class below!*"
810
]
911
},
1012
{
@@ -24,10 +26,17 @@
2426
" def __repr__(self) -> str:\n",
2527
" return \"0/0\"\n",
2628
"\n",
29+
" # Task 2\n",
2730
" def flip(self) -> \"Fraction\":\n",
2831
" return Fraction(0, 0)\n",
32+
" \n",
33+
" def double(self) -> None:\n",
34+
" pass\n",
2935
"\n",
30-
" # Task 2\n",
36+
" def square(self) -> None:\n",
37+
" pass\n",
38+
"\n",
39+
" # Task 3\n",
3140
" def __eq__(self, other) -> bool:\n",
3241
" return False\n",
3342
" \n",
@@ -37,7 +46,7 @@
3746
" def __mul__(self, other) -> \"Fraction\":\n",
3847
" return Fraction(0, 0)\n",
3948
"\n",
40-
" # Task 3\n",
49+
" # Bonus Task\n",
4150
" def __radd__(self, other) -> \"Fraction\":\n",
4251
" return Fraction(0, 0)\n",
4352
" \n",
@@ -49,15 +58,17 @@
4958
"cell_type": "markdown",
5059
"metadata": {},
5160
"source": [
52-
"> Task 1: Create a **Fraction** class, which has two properties `upper` and `lower`. You should try to simplify the fraction where possible on creation, and raise an exception if `lower` is 0.\n",
53-
"> Also implement `__repr__`, which determines how the class will be output when printed, and the `flip()` method, which should return a new fraction where upper and lower are swapped."
61+
"> Task 1: Create a **Fraction** class, which has two properties `upper` and `lower` which are passed in the constructor.\n",
62+
"> - You should try to simplify the fraction where possible on creation, and raise an exception if `lower` is 0.\n",
63+
"> \n",
64+
"> Also implement `__repr__` which returns a string, representing how the class will be output when printed."
5465
]
5566
},
5667
{
5768
"cell_type": "markdown",
5869
"metadata": {},
5970
"source": [
60-
"*Make sure to run the cell containing your class to update it before running the tests!*"
71+
"*Make sure to run the cell containing your class to update it before running the tests! Hitting `Run All` at the top should work fine.*"
6172
]
6273
},
6374
{
@@ -67,22 +78,25 @@
6778
"outputs": [],
6879
"source": [
6980
"# --- Tests for Task 1 ---\n",
70-
"x = Fraction(5, 2) \n",
71-
"y = Fraction(4, 6) \n",
72-
"print(x) # should return 5/2\n",
73-
"print(y) # should return 2/3\n",
81+
"x = Fraction(5, 2)\n",
82+
"y = Fraction(4, 6)\n",
83+
"print(x) # should return 5/2\n",
84+
"print(y) # should return 2/3\n",
7485
"z = y.flip()\n",
75-
"print(y) # should return 3/2\n",
76-
"a = Fraction(0, 7) # what would you like this to return?\n",
77-
"b = a.flip() # should raise an exception\n",
78-
"print(b)"
86+
"print(y) # should return 3/2\n",
87+
"a = Fraction(0, 7)\n",
88+
"print(a) # what would you like this to return?\n",
89+
"b = Fraction(7, 0) # should raise an exception"
7990
]
8091
},
8192
{
8293
"cell_type": "markdown",
8394
"metadata": {},
8495
"source": [
85-
"> Task 2: Implement the `__eq__`, `__add__` and `__mul__` double-underscore methods for your **Fraction** class, where `other` is assumed to be another instance of **Fraction**."
96+
">Task 2: Implement the following three class methods:\n",
97+
"> - `flip()`, which returns a *new* fraction with the values of upper and lower swapped.\n",
98+
"> - `double()`, which doubles the value of the *existing* fraction.\n",
99+
"> - `square()`, which would multiply the *existing* fraction by itself."
86100
]
87101
},
88102
{
@@ -91,14 +105,51 @@
91105
"metadata": {},
92106
"outputs": [],
93107
"source": [
94-
"# --- Tests for Task 2 ---\n"
108+
"# --- Tests for Task 2 ---\n",
109+
"x = Fraction(4, 3)\n",
110+
"x = x.flip()\n",
111+
"print(x) # Should be 3/4\n",
112+
"x.double()\n",
113+
"print(x) # Should be 3/2 (simplify?)\n",
114+
"x.square()\n",
115+
"print(x) # Should be 9/4\n",
116+
"y = Fraction(0, 7)\n",
117+
"y.flip() # Should raise an exception"
95118
]
96119
},
97120
{
98121
"cell_type": "markdown",
99122
"metadata": {},
100123
"source": [
101-
"> Task 3: Implement the same methods as in Task 2, but this time assume that `other` could also be of type `int`. It should behave how you expect. Also implement `__radd__` and `__rmul__` to reflect this change.\n",
124+
"> Task 3: Implement these double-underscore methods for your **Fraction** class, where `other` is another instance of **Fraction**.\n",
125+
"> - `__eq__` should determine whether the two fractions are equal.\n",
126+
"> - `__add__` should return a new fraction that's the sum of the *existing* fraction and *other* fraction. \n",
127+
"> - `__mul__` should return a new fraction that's the product of the *existing* fraction and *other* fraction."
128+
]
129+
},
130+
{
131+
"cell_type": "code",
132+
"execution_count": null,
133+
"metadata": {},
134+
"outputs": [],
135+
"source": [
136+
"# --- Tests for Task 3 ---\n",
137+
"x = Fraction(2, 3)\n",
138+
"y = Fraction(2, 6)\n",
139+
"print(x == y) # Should be false\n",
140+
"y.double()\n",
141+
"print(x == y) # Should be true\n",
142+
"z = x + y\n",
143+
"print(z) # Should be 4/3\n",
144+
"z *= x\n",
145+
"print(z) # Should be 8/9"
146+
]
147+
},
148+
{
149+
"cell_type": "markdown",
150+
"metadata": {},
151+
"source": [
152+
"> Bonus Task: Extend the methods you wrote for Task 3, this time assuming that `other` could also be of type `int`. It should behave how you expect a fraction to. Also implement `__radd__` and `__rmul__` to reflect this change.\n",
102153
"\n",
103154
"*Hint: `type(variable_name)` will return the type of a `variable_name`, and you can check equality to another type.*"
104155
]
@@ -109,7 +160,34 @@
109160
"metadata": {},
110161
"outputs": [],
111162
"source": [
112-
"# --- Tests for Task 3 ---"
163+
"# Tests for __mul__ and __rmul__\n",
164+
"x = Fraction(3, 4)\n",
165+
"y = x * 2\n",
166+
"print(y)\n",
167+
"z = 2 * x\n",
168+
"print(z)\n",
169+
"# implement for __add__, __radd__, and __eq__?"
170+
]
171+
},
172+
{
173+
"cell_type": "markdown",
174+
"metadata": {},
175+
"source": [
176+
"*What other class methods do you think would be useful to add? Feel free to extend it as you want.*"
177+
]
178+
},
179+
{
180+
"cell_type": "markdown",
181+
"metadata": {},
182+
"source": [
183+
"---"
184+
]
185+
},
186+
{
187+
"cell_type": "markdown",
188+
"metadata": {},
189+
"source": [
190+
"🖋️ *Exercises written by Keegan from the [Computing Society](https://uwcs.co.uk)*"
113191
]
114192
}
115193
],

0 commit comments

Comments
 (0)