| f | from math import isclose, sqrt | f | from math import isclose, sqrt |
| | | |
| class Triangle: | | class Triangle: |
| | | |
| def __init__(self, a, b, c): | | def __init__(self, a, b, c): |
| self.a, self.b, self.c = (float(a), float(b), float(c)) | | self.a, self.b, self.c = (float(a), float(b), float(c)) |
| if self.a <= 0 or self.b <= 0 or self.c <= 0 or (self.a + self.b | | if self.a <= 0 or self.b <= 0 or self.c <= 0 or (self.a + self.b |
| <= self.c) or (self.a + self.c <= self.b) or (self.b + self.c <= self.a | | <= self.c) or (self.a + self.c <= self.b) or (self.b + self.c <= self.a |
| ): | | ): |
| n | self.valid = False | n | self.empty = True |
| else: | | else: |
| n | self.valid = True | n | self.empty = False |
| | | |
| def __bool__(self): | | def __bool__(self): |
| n | return self.valid | n | return not self.empty |
| | | |
| def __abs__(self): | | def __abs__(self): |
| n | if not self: | n | if self.empty: |
| return 0.0 | | return 0.0 |
| p = (self.a + self.b + self.c) / 2 | | p = (self.a + self.b + self.c) / 2 |
| return sqrt(p * (p - self.a) * (p - self.b) * (p - self.c)) | | return sqrt(p * (p - self.a) * (p - self.b) * (p - self.c)) |
| | | |
| def __eq__(self, other): | | def __eq__(self, other): |
| if not isinstance(other, Triangle): | | if not isinstance(other, Triangle): |
| return NotImplemented | | return NotImplemented |
| sides1 = sorted([self.a, self.b, self.c]) | | sides1 = sorted([self.a, self.b, self.c]) |
| sides2 = sorted([other.a, other.b, other.c]) | | sides2 = sorted([other.a, other.b, other.c]) |
| return all((isclose(x, y) for x, y in zip(sides1, sides2))) | | return all((isclose(x, y) for x, y in zip(sides1, sides2))) |
| | | |
| def __lt__(self, other): | | def __lt__(self, other): |
| return abs(self) < abs(other) | | return abs(self) < abs(other) |
| | | |
| def __le__(self, other): | | def __le__(self, other): |
| return abs(self) <= abs(other) | | return abs(self) <= abs(other) |
| | | |
| def __gt__(self, other): | | def __gt__(self, other): |
| return abs(self) > abs(other) | | return abs(self) > abs(other) |
| | | |
| def __ge__(self, other): | | def __ge__(self, other): |
| return abs(self) >= abs(other) | | return abs(self) >= abs(other) |
| | | |
| def __str__(self): | | def __str__(self): |
| return f'{self.a}:{self.b}:{self.c}' | | return f'{self.a}:{self.b}:{self.c}' |
| t | 'tri = Triangle(3, 4, 5), Triangle(5, 4, 3), Triangle(7, 1, 1), Triangle | t | |
| (5, 5, 5), Triangle(7, 4, 4)\nfor a, b in zip(tri[:-1], tri[1:]):\n p | | |
| rint(a if a else b)\n print(f"{a}={abs(a):.2f} {b}={abs(b):.2f}")\n | | |
| print(a == b)\n print(a >= b)\n print(a < b)' | | |