Added more test files from lectures.

This commit is contained in:
cheezedoodle2
2023-04-06 17:03:21 -07:00
parent c610762522
commit 0dd1348601
13 changed files with 311 additions and 7 deletions
+52
View File
@@ -0,0 +1,52 @@
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
if i < min or i > max:
raise ValueError(f"{i} is outside the range of [{min}, {max}].")
self.min = min
self.max = max
def __int__(self):
return self.i
def __index__(self):
return self.i
def __getattr__(self, name):
print(f"DEBUG: {type(name)=}, {name=}")
return lambda *args, **kwargs: None
def __add__(self, y):
if isinstance(y, BoundedInt):
i = y.i
else:
i = y
if self.i + i > self.max:
raise ValueError()
else:
return BoundedInt(self.i + i, self.min, self.max)
def __sub__(self, y):
if isinstance(y, BoundedInt):
i = y.i
else:
i = y
if self.i - i < self.min:
raise ValueError()
else:
return BoundedInt(self.i - i, self.min, self.max)
if __name__ == "__main__":
b1 = BoundedInt(1,0,2)
b2 = BoundedInt(1,0,2)
print(f"{b1.doo}")
b3 = b1+b2