Update page 'Day 3 homework - Sloetjes'
+3
-17
@@ -95,8 +95,8 @@ class BoundedInt:
|
||||
|
||||
def __repr__(self):
|
||||
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.**
|
||||
```
|
||||
class BoundedInt():
|
||||
@@ -117,22 +117,8 @@ class BoundedInt():
|
||||
if result > self.max:
|
||||
raise ValueError("Result exceeds max value")
|
||||
return BoundedInt(result, self.min, self.max)
|
||||
|
||||
def __sub__(self, other):
|
||||
"""
|
||||
Subtracts another BoundedInt or an int from this BoundedInt.
|
||||
|
||||
Raises:
|
||||
ValueError: If subtracting a BoundedInt would exceed self.min.
|
||||
"""
|
||||
if isinstance(other, BoundedInt):
|
||||
result = self.i - other.i
|
||||
else:
|
||||
result = self.i - other
|
||||
if result < self.min:
|
||||
raise ValueError("Result exceeds min value")
|
||||
return BoundedInt(result, self.min, self.max)
|
||||
```
|
||||
|
||||
**9. Same as 8, but for subtraction.**
|
||||
```
|
||||
class BoundedInt():
|
||||
@@ -153,6 +139,7 @@ class BoundedInt():
|
||||
raise ValueError("Result exceeds min value")
|
||||
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`.**
|
||||
|
||||
```
|
||||
@@ -188,7 +175,6 @@ class BoundedInt():
|
||||
if result < self.min:
|
||||
raise ValueError("Result exceeds min value")
|
||||
return BoundedInt(result, self.min, self.max)
|
||||
|
||||
```
|
||||
#####An example of `sentiment(emoji)`:
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user