Compare commits

2 Commits
Author SHA1 Message Date
shunan.z 7ab36cc4c5 Merge branch 'main' of https://gogs.gtsplab.com:5432/SVIG/python-bootcamp 2023-06-07 19:56:05 -05:00
shunan.z dd85aebb88 Resubmit for HW 6.7 2023-06-07 19:56:03 -05:00
+57 -111
View File
@@ -1,99 +1,8 @@
# question 1: Read this: https://docs.python.org/3/reference/datamodel.html?highlight=__int__#object.__int__
# question 2:
class BoundedInt(): class BoundedInt():
def __init__(self, i, min, max): def __init__(self, i, min_val, max_val):
"""
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.i = i
self.min = min self.min = min_val
self.max = max self.max = max_val
def __int__(self):
return self.i
# question 3:Read this, and the paragraph below it: https://docs.python.org/3/reference/datamodel.html?highlight=__int__#object.__add__
# question 4: Without checking the debugger's local window, I would guess that x and y are integer values. Actually,
# they can be objects of any type.
# question 6:
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 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)
# question 7:
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 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)
# question 8:
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)
# question 9:
class BoundedInt():
def __init__(self, i, min, max):
self.i = i
self.min = min
self.max = max
def __int__(self): def __int__(self):
return self.i return self.i
@@ -106,26 +15,63 @@ class BoundedInt():
if result < self.min: if result < self.min:
raise ValueError("Result exceeds min value") raise ValueError("Result exceeds min value")
return BoundedInt(result, self.min, self.max) return BoundedInt(result, self.min, self.max)
# question 10:
class BoundedInt():
def __init__(self, i, min_val, max_val):
self.i = i
self.min = min
self.max = max
def __int__(self):
return self.i
def __add__(self, other): def __add__(self, other):
if isinstance(other, BoundedInt): if isinstance(other, BoundedInt):
result = self.i + other.i result = self.i + other.i
elif isinstance(other, int):
result = self.i + other
elif isinstance(other, emoji):
raise ValueError("Cannot add a BoundedInt to an emoji.")
else: else:
raise TypeError("Unsupported operand type.") result = self.i + other
if result > self.max: if result > self.max:
raise ValueError("Sum exceeds maximum value.") raise ValueError("Result exceeds max value")
return BoundedInt(result, self.min, self.max)
def test1(self):
bi1 = BoundedInt(0xdeadbeef, min_val=(0xdeadbeef - 1), max_val=(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):
bi2 = BoundedInt(1, min_val=0, max_val=1)
bi3 = BoundedInt(1, min_val=0, max_val=1)
try:
bi4 = bi2 + bi3 # 1 + 1 = 2, which exceeds max_val = 1
print(f"❌ Test 2 failed!")
except ValueError as err:
print(f"✅ Test 2 passed!")
def test3(self):
bi4 = BoundedInt(1, min_val=0, max_val=1)
bi5 = BoundedInt(2, min_val=0, max_val=-1)
try:
bi6 = bi4 - bi5 # 1 - 2 = -1, which is less than min_val = 0
print(f"❌ Test 3 failed!")
except ValueError as err:
print(f"✅ Test 3 passed!")
def test4(self):
bi7 = BoundedInt(1, min_val=0, max_val=1)
bi8 = 2
try:
bi9 = bi7 + bi8
print(f"❌ Test 4 failed!")
except ValueError as err:
print(f"✅ Test 4 passed!")
def test5(self):
bi10 = BoundedInt(1, min_val=0, max_val=1)
bi11 = 2
try:
bi12 = bi10 - 11
print(f"❌ Test 5 failed!")
except ValueError as err:
print(f"✅ Test 5 passed!")
tester = BoundedInt(0, 0, 0)
tester.test1()
tester.test2()
tester.test3()
tester.test4()
tester.test5()