Update page 'Day 3 Homework - Sloetjes'
@@ -0,0 +1,206 @@
|
|||||||
|
**2. 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):
|
||||||
|
"""
|
||||||
|
Create a bounded integer.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
i (int): The initial value of the integer.
|
||||||
|
min (int): The minimum value of the integer.
|
||||||
|
max (int): The maximum value of the integer.
|
||||||
|
"""
|
||||||
|
self.i = i
|
||||||
|
self.min = min
|
||||||
|
self.max = max
|
||||||
|
|
||||||
|
def __int__(self):
|
||||||
|
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.**
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
**6. 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.
|
||||||
|
|
||||||
|
```
|
||||||
|
class BoundedInt:
|
||||||
|
def __init__(self, i, min=None, max=None):
|
||||||
|
self.i = i
|
||||||
|
self.min = min
|
||||||
|
self.max = max
|
||||||
|
|
||||||
|
def __int__(self):
|
||||||
|
return self.i
|
||||||
|
|
||||||
|
def __add__(self, other):
|
||||||
|
result = self.i + other
|
||||||
|
if self.max is not None and result > self.max:
|
||||||
|
raise ValueError("Value exceeds maximum bound.")
|
||||||
|
return BoundedInt(result, self.min, self.max)
|
||||||
|
|
||||||
|
def __sub__(self, other):
|
||||||
|
result = self.i - other
|
||||||
|
if self.min is not None and result < self.min:
|
||||||
|
raise ValueError("Value exceeds minimum bound.")
|
||||||
|
return BoundedInt(result, self.min, self.max)
|
||||||
|
|
||||||
|
def __radd__(self, other):
|
||||||
|
return self.__add__(other)
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return str(self.i)
|
||||||
|
```
|
||||||
|
|
||||||
|
**7. Do the same as 6, but for subtraction, to pass test 3.**
|
||||||
|
```
|
||||||
|
class BoundedInt:
|
||||||
|
def __init__(self, i, min=None, max=None):
|
||||||
|
self.i = i
|
||||||
|
self.min = min
|
||||||
|
self.max = max
|
||||||
|
|
||||||
|
def __int__(self):
|
||||||
|
return self.i
|
||||||
|
|
||||||
|
def __sub__(self, other):
|
||||||
|
result = self.i - other
|
||||||
|
if self.min is not None and result < self.min:
|
||||||
|
raise ValueError("Value exceeds minimum bound.")
|
||||||
|
return BoundedInt(result, self.min, self.max)
|
||||||
|
|
||||||
|
def __rsub__(self, other):
|
||||||
|
return self.__sub__(other)
|
||||||
|
|
||||||
|
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():
|
||||||
|
def __init__(self, i, min, max):
|
||||||
|
|
||||||
|
self.i = i
|
||||||
|
self.min = min
|
||||||
|
self.max = max
|
||||||
|
|
||||||
|
def __int__(self):
|
||||||
|
return self.i
|
||||||
|
|
||||||
|
def __add__(self, other):
|
||||||
|
if isinstance(other, BoundedInt):
|
||||||
|
result = self.i + other.i
|
||||||
|
else:
|
||||||
|
result = self.i + other
|
||||||
|
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():
|
||||||
|
def __init__(self, i, min, max):
|
||||||
|
self.i = i
|
||||||
|
self.min = min
|
||||||
|
self.max = max
|
||||||
|
|
||||||
|
def __int__(self):
|
||||||
|
return self.i
|
||||||
|
|
||||||
|
def __sub__(self, other):
|
||||||
|
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)
|
||||||
|
```
|
||||||
|
**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`.**
|
||||||
|
|
||||||
|
```
|
||||||
|
class BoundedInt():
|
||||||
|
def __init__(self, i, min, max):
|
||||||
|
self.i = i
|
||||||
|
self.min = min
|
||||||
|
self.max = max
|
||||||
|
|
||||||
|
def __int__(self):
|
||||||
|
return self.i
|
||||||
|
|
||||||
|
def __add__(self, other):
|
||||||
|
if isinstance(other, BoundedInt):
|
||||||
|
result = self.i + other.i
|
||||||
|
elif isinstance(other, str):
|
||||||
|
score = sentiment(other)
|
||||||
|
result = self.i + score
|
||||||
|
else:
|
||||||
|
result = self.i + other
|
||||||
|
if result > self.max:
|
||||||
|
raise ValueError("Result exceeds max value")
|
||||||
|
return BoundedInt(result, self.min, self.max)
|
||||||
|
|
||||||
|
def __sub__(self, other):
|
||||||
|
if isinstance(other, BoundedInt):
|
||||||
|
result = self.i - other.i
|
||||||
|
elif isinstance(other, str):
|
||||||
|
score = sentiment(other)
|
||||||
|
result = self.i - score
|
||||||
|
else:
|
||||||
|
result = self.i - other
|
||||||
|
if result < self.min:
|
||||||
|
raise ValueError("Result exceeds min value")
|
||||||
|
return BoundedInt(result, self.min, self.max)
|
||||||
|
|
||||||
|
```
|
||||||
|
#####An example of `sentiment(emoji)`:
|
||||||
|
```
|
||||||
|
def sentiment(emoji):
|
||||||
|
sentiment_scores = {
|
||||||
|
"😀": 8.9,
|
||||||
|
"😢": -6.2,
|
||||||
|
"😡": -9.5,
|
||||||
|
"😍": 9.8,
|
||||||
|
# and so on...
|
||||||
|
}
|
||||||
|
|
||||||
|
# look up the sentiment score for the given emoji, or return a neutral score if the emoji is not in the dictionary
|
||||||
|
return sentiment_scores.get(emoji, 0.0)
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user