-
Notifications
You must be signed in to change notification settings - Fork 4
/
ranges.py
38 lines (29 loc) · 1.1 KB
/
ranges.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Range:
def __init__(self, lo, hi):
assert hi >= lo
self.lo = lo
self.hi = hi
def __add__(self, other):
if isinstance(other, Range):
return Range(self.lo + other.lo, self.hi + other.hi)
else:
return Range(self.lo + other, self.hi + other)
def __radd__(self, other):
return Range(other + self.lo, other + self.hi)
def __mul__(self, other):
if isinstance(other, Range):
return Range(self.lo * other.lo, self.hi * other.hi)
else:
return Range(self.lo * other, self.hi * other)
def __rmul__(self, other):
return Range(self.lo * other, self.hi * other)
def __neg__(self):
return Range(-self.hi, -self.lo)
def __sub__(self, other):
return Range(self.lo - other.hi, self.hi - other.lo)
def __rsub__(self, other):
return Range(other - self.hi, other - self.lo)
def __truediv__(self, other):
return Range(self.lo / other.hi, self.hi / other.lo)
def __rtruediv__(self, other):
return Range(other / self.hi, other / self.lo)