diff --git a/Day 3 Homework - Karen.md b/Day 3 Homework - Karen.md new file mode 100644 index 0000000..a9168f2 --- /dev/null +++ b/Day 3 Homework - Karen.md @@ -0,0 +1,37 @@ +We're going to implement a class, `BoundedInt`, that only allows arithmetic within a given range of integers. For simplicity, we're only going to support addition and subtraction for now. +​ +Here's the standard class template, which we'll go over in more detail later on. The test program is here: +​ +https://gogs.gtsplab.com:5432/j/python-test/src/main/test13.py +​ +You can either append it to the template below, or save the template as BoundedInt.py in the same directory and run test13.py to execute the tests. +​ +``` +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 +``` +​ +1. Read this: https://docs.python.org/3/reference/datamodel.html?highlight=__int__#object.__int__ +2. Modify the template to pass test 1, which checks that `int()` called on your class will return the value of `i`. +3. Read this, and the paragraph below it: https://docs.python.org/3/reference/datamodel.html?highlight=__int__#object.__add__ +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. +5. Don't forget that you can access the value of a BoundedInt by using either `.i` on it, or `int()`, now that you've passed test 1. +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`. +7. Do the same as 6, but for subtraction, to pass test 3. +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. +9. Same as 8, but for subtraction. +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`. +​ \ No newline at end of file