|
1 | 1 | {
|
2 | 2 | "cells": [
|
3 |
| - { |
4 |
| - "cell_type": "markdown", |
5 |
| - "metadata": {}, |
6 |
| - "source": [ |
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!*" |
10 |
| - ] |
11 |
| - }, |
12 |
| - { |
13 |
| - "cell_type": "code", |
14 |
| - "execution_count": null, |
15 |
| - "metadata": {}, |
16 |
| - "outputs": [], |
17 |
| - "source": [ |
18 |
| - "# !!! Scroll down to the tasks before you start writing anything !!!\n", |
19 |
| - "\n", |
20 |
| - "class Fraction:\n", |
21 |
| - " # Task 1\n", |
22 |
| - " def __init__(self):\n", |
23 |
| - " self.upper = 0\n", |
24 |
| - " self.lower = 0\n", |
25 |
| - "\n", |
26 |
| - " def __repr__(self) -> str:\n", |
27 |
| - " return \"0/0\"\n", |
28 |
| - "\n", |
29 |
| - " # Task 2\n", |
30 |
| - " def flip(self) -> \"Fraction\":\n", |
31 |
| - " return Fraction(0, 0)\n", |
32 |
| - " \n", |
33 |
| - " def double(self) -> None:\n", |
34 |
| - " pass\n", |
35 |
| - "\n", |
36 |
| - " def square(self) -> None:\n", |
37 |
| - " pass\n", |
38 |
| - "\n", |
39 |
| - " # Task 3\n", |
40 |
| - " def __eq__(self, other) -> bool:\n", |
41 |
| - " return False\n", |
42 |
| - " \n", |
43 |
| - " def __add__(self, other) -> \"Fraction\":\n", |
44 |
| - " return Fraction(0, 0)\n", |
45 |
| - " \n", |
46 |
| - " def __mul__(self, other) -> \"Fraction\":\n", |
47 |
| - " return Fraction(0, 0)\n", |
48 |
| - "\n", |
49 |
| - " # Bonus Task\n", |
50 |
| - " def __radd__(self, other) -> \"Fraction\":\n", |
51 |
| - " return Fraction(0, 0)\n", |
52 |
| - " \n", |
53 |
| - " def __rmul__(self, other) -> \"Fraction\":\n", |
54 |
| - " return Fraction(0, 0)" |
55 |
| - ] |
56 |
| - }, |
57 |
| - { |
58 |
| - "cell_type": "markdown", |
59 |
| - "metadata": {}, |
60 |
| - "source": [ |
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." |
65 |
| - ] |
66 |
| - }, |
67 |
| - { |
68 |
| - "cell_type": "markdown", |
69 |
| - "metadata": {}, |
70 |
| - "source": [ |
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.*" |
72 |
| - ] |
73 |
| - }, |
74 | 3 | {
|
75 | 4 | "cell_type": "code",
|
76 | 5 | "execution_count": null,
|
77 |
| - "metadata": {}, |
| 6 | + "metadata": { |
| 7 | + "vscode": { |
| 8 | + "languageId": "plaintext" |
| 9 | + } |
| 10 | + }, |
78 | 11 | "outputs": [],
|
79 | 12 | "source": [
|
80 |
| - "# --- Tests for Task 1 ---\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", |
85 |
| - "z = y.flip()\n", |
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" |
90 |
| - ] |
91 |
| - }, |
92 |
| - { |
93 |
| - "cell_type": "markdown", |
94 |
| - "metadata": {}, |
95 |
| - "source": [ |
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." |
100 |
| - ] |
101 |
| - }, |
102 |
| - { |
103 |
| - "cell_type": "code", |
104 |
| - "execution_count": null, |
105 |
| - "metadata": {}, |
106 |
| - "outputs": [], |
107 |
| - "source": [ |
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" |
118 |
| - ] |
119 |
| - }, |
120 |
| - { |
121 |
| - "cell_type": "markdown", |
122 |
| - "metadata": {}, |
123 |
| - "source": [ |
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", |
153 |
| - "\n", |
154 |
| - "*Hint: `type(variable_name)` will return the type of a `variable_name`, and you can check equality to another type.*" |
155 |
| - ] |
156 |
| - }, |
157 |
| - { |
158 |
| - "cell_type": "code", |
159 |
| - "execution_count": null, |
160 |
| - "metadata": {}, |
161 |
| - "outputs": [], |
162 |
| - "source": [ |
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.*" |
| 13 | + "raise NotImplementedError()" |
177 | 14 | ]
|
178 | 15 | },
|
179 | 16 | {
|
| 17 | + "attachments": {}, |
180 | 18 | "cell_type": "markdown",
|
181 | 19 | "metadata": {},
|
182 | 20 | "source": [
|
183 | 21 | "---"
|
184 | 22 | ]
|
185 | 23 | },
|
186 | 24 | {
|
| 25 | + "attachments": {}, |
187 | 26 | "cell_type": "markdown",
|
188 | 27 | "metadata": {},
|
189 | 28 | "source": [
|
190 |
| - "🖋️ *Exercises written by Keegan from the [Computing Society](https://uwcs.co.uk)*" |
| 29 | + "🖋️ *Exercises written by _____ from the [Computing Society](https://uwcs.co.uk)*" |
191 | 30 | ]
|
192 | 31 | }
|
193 | 32 | ],
|
|
0 commit comments