Update page 'Day 3 homework - Sloetjes'

xuanyi.wang
2023-04-03 20:58:49 +02:00
parent c54d42a6ee
commit 56dee7d0b1
+7 -7
@@ -1,4 +1,4 @@
**2. Modify the template to pass test 1, which checks that `int()` called on your class will return the value of `i`.**
**1. Modify the template to pass test 1, which checks that `int()` called on your class will return the value of `i`.**
```
class BoundedInt():
def __init__(self, i, min, max):
@@ -18,11 +18,11 @@ class BoundedInt():
return self.i
```
**4. What do you think the types of x and y are? Guess, without checking first. What are they, actually? You can use the debugger's local window to find out.**
**2. What do you think the types of x and y are? Guess, without checking first. What are they, actually? You can use the debugger's local window to find out.**
###### Answer: Based on the documentation, I guess that x and y are likely to be any type of objects that define the corresponding binary arithmetic methods such as add(), sub(), mul(), etc.
**6. Modify the template to pass test 2, which checks that:**
**3. Modify the template to pass test 2, which checks that:**
* Your class implements the + operator.
* It throws ValueError when attempting to add another BoundedInt that would exceed self.max.
@@ -50,7 +50,7 @@ class BoundedInt:
return str(self.i)
```
**7. Do the same as 6, but for subtraction, to pass test 3.**
**4. Do the same as 6, but for subtraction, to pass test 3.**
```
class BoundedInt:
def __init__(self, i, min=None, max=None):
@@ -74,7 +74,7 @@ class BoundedInt:
return str(self.i)
```
**8. Modify the template so it also supports adding a regular integer to a BoundedInt, to pass test 4. Note that you can use `isinstance()` to determine the class of a variable.**
**5. Modify the template so it also supports adding a regular integer to a BoundedInt, to pass test 4. Note that you can use `isinstance()` to determine the class of a variable.**
```
class BoundedInt():
def __init__(self, i, min, max):
@@ -96,7 +96,7 @@ class BoundedInt():
return BoundedInt(result, self.min, self.max)
```
**9. Same as 8, but for subtraction.**
**6. Same as 8, but for subtraction.**
```
class BoundedInt():
def __init__(self, i, min, max):
@@ -117,7 +117,7 @@ class BoundedInt():
return BoundedInt(result, self.min, self.max)
```
**10. You don't have to implement the code, but think about how you would change the template so it could allow adding a BoundedInt to an emoji, if there was a hypothetical function sentiment(emoji) which returned a floating point number from -10.0 to 10.0 which indicated whether the emoji was happy or sad. e.g. BoundedInt(1, min = 1, max = 10) + 😁 would raise `ValueError`.**
**7. You don't have to implement the code, but think about how you would change the template so it could allow adding a BoundedInt to an emoji, if there was a hypothetical function sentiment(emoji) which returned a floating point number from -10.0 to 10.0 which indicated whether the emoji was happy or sad. e.g. BoundedInt(1, min = 1, max = 10) + 😁 would raise `ValueError`.**
```
class BoundedInt():