From 12474cca3fd0bb24baf63841c8834e02c1c76719 Mon Sep 17 00:00:00 2001 From: shunan zhao Date: Wed, 7 Jun 2023 17:58:22 -0500 Subject: [PATCH] Shunan.z_HW --- max submission/6_7_HW.py | 131 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 max submission/6_7_HW.py diff --git a/max submission/6_7_HW.py b/max submission/6_7_HW.py new file mode 100644 index 0000000..94c9e0b --- /dev/null +++ b/max submission/6_7_HW.py @@ -0,0 +1,131 @@ +# question 1: Read this: https://docs.python.org/3/reference/datamodel.html?highlight=__int__#object.__int__ + +# question 2: +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): + 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): + return self.i + + def __sub__(self, other): + if isinstance(other, BoundedInt): + result = self.i - other.i + else: + result = self.i - other + if result < self.min: + raise ValueError("Result exceeds min value") + 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): + if isinstance(other, BoundedInt): + 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: + raise TypeError("Unsupported operand type.") + + if result > self.max: + raise ValueError("Sum exceeds maximum value.")