This is the hm6
This commit is contained in:
@@ -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")
|
||||
Reference in New Issue
Block a user