diff --git a/Scott_Submissions/BoundedInt.py b/Scott_Submissions/BoundedInt.py new file mode 100644 index 0000000..f5a70e7 --- /dev/null +++ b/Scott_Submissions/BoundedInt.py @@ -0,0 +1,46 @@ +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): + """ + Convert the bounded integer to int. + + Returns: + int: The value of the bounded integer. + """ + return self.i + + def __add__(self, other): + if isinstance(other, BoundedInt): + if self.i + other.i > self.max: + raise ValueError("Addition exceeds maximum value") + return BoundedInt(self.i + other.i, self.min, self.max) + elif isinstance(other, int): + if self.i + other > self.max: + raise ValueError("Addition exceeds maximum value") + return BoundedInt(self.i + other, self.min, self.max) + else: + raise TypeError("Unsupported operand type") + + def __sub__(self, other): + if isinstance(other, BoundedInt): + if self.i - other.i < self.min: + raise ValueError("Subtraction exceeds minimum value") + return BoundedInt(self.i - other.i, self.min, self.max) + elif isinstance(other, int): + if self.i - other < self.min: + raise ValueError("Subtraction exceeds minimum value") + return BoundedInt(self.i - other, self.min, self.max) + else: + raise TypeError("Unsupported operand type") \ No newline at end of file diff --git a/Scott_Submissions/BoundedIntTester.py b/Scott_Submissions/BoundedIntTester.py new file mode 100644 index 0000000..3c52452 --- /dev/null +++ b/Scott_Submissions/BoundedIntTester.py @@ -0,0 +1,67 @@ +from BoundedInt import BoundedInt + +class BoundedIntTester: + def __init__(self): + pass + + def test1(self): + bi1 = BoundedInt(0xdeadbeef, min = (0xdeadbeef-1), max = (0xdeadbeef+1)) + if int(bi1) == 0xdeadbeef: + print(f"✅ Test 1 passed!") + else: + print(f"❌ Test 1 failed, int(bi1) = {int(bi1)}, expected 0xdeadbeef") + + def test2(self): + """ + Test that addition works for two BoundedInts, and throws an error if the bounds are exceeded. + """ + bi2 = BoundedInt(1, min = 0, max = 1) + bi3 = BoundedInt(1, min = 0, max = 1) + try: + bi4 = bi2 + bi3 # 1 + 1 = 2, which is more than max = 1 + print(f"❌ Test 2 failed!") + except ValueError as err: + print(f"✅ Test 2 passed!") + + def test3(self): + """ + Test that addition works for two BoundedInts, and throws an error if the bounds are exceeded. + """ + bi4 = BoundedInt(1, min = 0, max = 1) + bi5 = BoundedInt(2, min = 0, max = -1) + try: + bi6 = bi4 - bi5 # 1 - 2 = (-1), which is less than min = 0 + print(f"❌ Test 3 failed!") + except ValueError as err: + print(f"✅ Test 3 passed!") + + def test4(self): + """ + Test that addition works for a BoundedInt and a regular int, and throws an error if the bounds are exceeded. + """ + bi7 = BoundedInt(1, min = 0, max = 1) + bi8 = 2 + try: + bi9 = bi7 + bi8 + print(f"❌ Test 4 failed!") + except ValueError as err: + print(f"✅ Test 4 passed!") + + def test5(self): + """ + Test that subtraction works for a BoundedInt and a regular int, and throws an error if the bounds are exceeded. + """ + bi10 = BoundedInt(1, min = 0, max = 1) + bi11 = 2 + try: + bi12 = bi10 - 11 + print(f"❌ Test 5 failed!") + except ValueError as err: + print(f"✅ Test 5 passed!") + +tester = BoundedIntTester() +tester.test1() +tester.test2() +tester.test3() +tester.test4() +tester.test5() \ No newline at end of file diff --git a/Scott_Submissions/HW6.py b/Scott_Submissions/HW6.py new file mode 100644 index 0000000..dc7f675 --- /dev/null +++ b/Scott_Submissions/HW6.py @@ -0,0 +1,76 @@ +########## This file only contains the question 2, 4, and 10. The rest is in the BoundedInt.py. ########## + + + +# 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): +# """ +# Convert the bounded integer to int. + +# Returns: +# int: The value of the bounded integer. +# """ +# return self.i + + + +# 4. In that reading material, there's a sentence that starts with "For instance, to evaluate the expression x + y, "... +# 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. + + # x might be an instance of a class that has defined the __add__() method + # And y is the object that is compatible with the x instance for the addition operation. + +class MyClass: + def __init__(self, value): + self.value = value + + def __add__(self, other): + if isinstance(other, MyClass): + return MyClass(self.value + other.value) + elif isinstance(other, int): + return MyClass(self.value + other) + else: + raise TypeError("Unsupported operand type.") + +# Example usage +obj1 = MyClass(5) +obj2 = MyClass(10) +result1 = obj1 + obj2 # Calls obj1.__add__(obj2) +print(result1.value) # Output: 15 + +result2 = obj1 + 3 # Calls obj1.__add__(3) +print(result2.value) # Output: 8 + + + +# 10. 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. + + # By adding the conceptional function 'sentiment(emoji)' in the '__add__()' method + # The sentiment will analyze the happy level of the input emoji and return a floating number from a number range + # For example: + # 10 for a 😊 smiley face + # -10 for a frowny face + # 0 for a 🍕 + # The protential code can be like this: +''' + elif isinstance(other, str) and sentiment(other) < 0: + aise ValueError("Cannot add a BoundedInt to a sad emoji") + '''