|
4 | 4 | "cell_type": "markdown",
|
5 | 5 | "metadata": {},
|
6 | 6 | "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!*" |
8 | 10 | ]
|
9 | 11 | },
|
10 | 12 | {
|
|
24 | 26 | " def __repr__(self) -> str:\n",
|
25 | 27 | " return \"0/0\"\n",
|
26 | 28 | "\n",
|
| 29 | + " # Task 2\n", |
27 | 30 | " def flip(self) -> \"Fraction\":\n",
|
28 | 31 | " return Fraction(0, 0)\n",
|
| 32 | + " \n", |
| 33 | + " def double(self) -> None:\n", |
| 34 | + " pass\n", |
29 | 35 | "\n",
|
30 |
| - " # Task 2\n", |
| 36 | + " def square(self) -> None:\n", |
| 37 | + " pass\n", |
| 38 | + "\n", |
| 39 | + " # Task 3\n", |
31 | 40 | " def __eq__(self, other) -> bool:\n",
|
32 | 41 | " return False\n",
|
33 | 42 | " \n",
|
|
37 | 46 | " def __mul__(self, other) -> \"Fraction\":\n",
|
38 | 47 | " return Fraction(0, 0)\n",
|
39 | 48 | "\n",
|
40 |
| - " # Task 3\n", |
| 49 | + " # Bonus Task\n", |
41 | 50 | " def __radd__(self, other) -> \"Fraction\":\n",
|
42 | 51 | " return Fraction(0, 0)\n",
|
43 | 52 | " \n",
|
|
49 | 58 | "cell_type": "markdown",
|
50 | 59 | "metadata": {},
|
51 | 60 | "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." |
54 | 65 | ]
|
55 | 66 | },
|
56 | 67 | {
|
57 | 68 | "cell_type": "markdown",
|
58 | 69 | "metadata": {},
|
59 | 70 | "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.*" |
61 | 72 | ]
|
62 | 73 | },
|
63 | 74 | {
|
|
67 | 78 | "outputs": [],
|
68 | 79 | "source": [
|
69 | 80 | "# --- 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", |
74 | 85 | "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" |
79 | 90 | ]
|
80 | 91 | },
|
81 | 92 | {
|
82 | 93 | "cell_type": "markdown",
|
83 | 94 | "metadata": {},
|
84 | 95 | "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." |
86 | 100 | ]
|
87 | 101 | },
|
88 | 102 | {
|
|
91 | 105 | "metadata": {},
|
92 | 106 | "outputs": [],
|
93 | 107 | "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" |
95 | 118 | ]
|
96 | 119 | },
|
97 | 120 | {
|
98 | 121 | "cell_type": "markdown",
|
99 | 122 | "metadata": {},
|
100 | 123 | "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", |
102 | 153 | "\n",
|
103 | 154 | "*Hint: `type(variable_name)` will return the type of a `variable_name`, and you can check equality to another type.*"
|
104 | 155 | ]
|
|
109 | 160 | "metadata": {},
|
110 | 161 | "outputs": [],
|
111 | 162 | "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)*" |
113 | 191 | ]
|
114 | 192 | }
|
115 | 193 | ],
|
|
0 commit comments