From 34e39ba1d95cb33d75c948de49e852f30d73c1d6 Mon Sep 17 00:00:00 2001 From: "scott.liu" Date: Wed, 7 Jun 2023 17:05:01 -0700 Subject: [PATCH 1/2] This is the hm6 --- Scott_Submissions/BoundedInt.py | 46 ++++++++++++++++ Scott_Submissions/BoundedIntTester.py | 67 +++++++++++++++++++++++ Scott_Submissions/HW6.py | 76 +++++++++++++++++++++++++++ 3 files changed, 189 insertions(+) create mode 100644 Scott_Submissions/BoundedInt.py create mode 100644 Scott_Submissions/BoundedIntTester.py create mode 100644 Scott_Submissions/HW6.py 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") + ''' From 21661d71ee55a3bce4f5b9f7d8c424497972cb4a Mon Sep 17 00:00:00 2001 From: "scott.liu" Date: Wed, 7 Jun 2023 17:05:24 -0700 Subject: [PATCH 2/2] . --- .../__pycache__/BoundedInt.cpython-311.pyc | Bin 0 -> 2769 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 Scott_Submissions/__pycache__/BoundedInt.cpython-311.pyc diff --git a/Scott_Submissions/__pycache__/BoundedInt.cpython-311.pyc b/Scott_Submissions/__pycache__/BoundedInt.cpython-311.pyc new file mode 100644 index 0000000000000000000000000000000000000000..398423514836d02df04ba5bc0e33270557d80c66 GIT binary patch literal 2769 zcmcf@O=}xRbao|KYkgF%lq#l8(hV_13dHKE4xtoe$AnzmP&=jgvaB`Zcx!7{of)~d z5XCK_xZp!?cEJH3OxoZcdMGr9{0J%N5Egt2y{otZNkXBLdm>Jm4{?LL{-3H8Uh@lVZcK)0v3kc*rZdnDWi97S?YO5L+C36zoYj7 zZIilECk9X*sH?#B{E>PHI6pS3fL*orGNyN7iZfF(-P9L4o^;EW>;dEBG5<)BKM=Qk z4)rWO2gmZa^tmMl3YN6Y6@ArQsWRPJ)TJlvTIu=0wQSjgiojasYWXaR=GqBGbL|X@ zGx?CKSZ)a105aF|VK*$;$|8?InmE*!@F*aGdBy0C?-uS0#zjGt94QO8nOK%irEq6% z_Fkc~E|(np#)9L>l3A`4X7PwITU{tyLRgM13jIQjSJqw4Ffc8KQOgW$^n_RqaT(AC zX{Sj#vl-qRdXdS(qIH)|ZBsjf1El2F5g4Sss*eBxRVSar+5xDlQmgqznsV$_#-&$^ zKw)+1V2uar`w5d(ZU;+j%T9Sulr>;$+;^B)ytKliK|lf{)%h@BH;P}4nMemR0kRaH zjRS&2yg^1TY^`o4Ir1gHVS&MVd7r%tX19qPNJ(9h11C?8A^#W>Mv?w`Py2P2F=7t}y*vi}1K-AM@H-Mc9YO-%F@5qo&O7?9 zy|cxP73%_3gBi+)W@=Z8l)`=B*g9J)F-CloSx?HcY zyynI%Vc9~Oc8PIJRA*9gNZr_{c)LtUtw} z{V=OHt`?iw$yRo-fLt2$6>h&FNox3>Eyf& zK8qY}!|C8@L>WXE00p~WR2NQlzb6~R!~dfDZ#T2kt?YDTWcu8? z{}m?B*!?s4C_cBgS!Rae#tftE(CP}(3B&lJYOZv4I24ZXB%&-Loct%mn;-9;>LeF$ z9lY|zJwO}&Ut5hwF1Cq}-i}7HZQ`SJEON6=d^GA)eHl_6XXs)E-bwF*y97)>hGXr+ jPVgUb{O`aEWCDnGNKurwdP4y(r_moM`|}kWp5ea$bF@h< literal 0 HcmV?d00001