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):
"""
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
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.
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.
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 __radd__(self, other):
return self.__add__(other)
def __repr__(self):
return str(self.i)
4. 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)
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):
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)
6. 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)
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():
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,
}
return sentiment_scores.get(emoji, 0.0)