Skip to content

Commit 381e6a0

Browse files
committed
Switch to proposed new headings
1 parent fbc6290 commit 381e6a0

File tree

4 files changed

+651
-678
lines changed

4 files changed

+651
-678
lines changed

week5/README.md

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,15 @@
11
In our fifth week, we will cover:
22

3-
Current:
4-
5-
- Programming practices
6-
- Expect everything
7-
- A comprehensive description
8-
- Bonus Tasks
9-
10-
Suggestion sketch:
11-
123
- A very brief introduction to objects
134
- What is an object?
145
- Properties and methods
15-
- Exceptions as an example of objects?
16-
- There's more?!?!???!
6+
- What next?
177
- Using libraries
18-
- The `import` keyword
8+
- Where do I start?
199
- Finding libraries
2010
- Doing useful(-ish) things
2111
- Example one
2212
- Example two
23-
- Readability counts
13+
- Readability counts!
2414
- Why bother?
2515
- Tips for readability

week5/exercises.ipynb

Lines changed: 9 additions & 170 deletions
Original file line numberDiff line numberDiff line change
@@ -1,193 +1,32 @@
11
{
22
"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-
},
743
{
754
"cell_type": "code",
765
"execution_count": null,
77-
"metadata": {},
6+
"metadata": {
7+
"vscode": {
8+
"languageId": "plaintext"
9+
}
10+
},
7811
"outputs": [],
7912
"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()"
17714
]
17815
},
17916
{
17+
"attachments": {},
18018
"cell_type": "markdown",
18119
"metadata": {},
18220
"source": [
18321
"---"
18422
]
18523
},
18624
{
25+
"attachments": {},
18726
"cell_type": "markdown",
18827
"metadata": {},
18928
"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)*"
19130
]
19231
}
19332
],

0 commit comments

Comments
 (0)