| n | from math import isclose, sqrt | n | from math import sqrt, isclose |
| | | |
| class Triangle: | | class Triangle: |
| | | |
| def __init__(self, a, b, c): | | def __init__(self, a, b, c): |
| self.a, self.b, self.c = map(float, (a, b, c)) | | self.a, self.b, self.c = map(float, (a, b, c)) |
| self.valid = self.a > 0 and self.b > 0 and (self.c > 0) and (sel | | self.valid = self.a > 0 and self.b > 0 and (self.c > 0) and (sel |
| f.a + self.b > self.c) and (self.a + self.c > self.b) and (self.b + self | | f.a + self.b > self.c) and (self.a + self.c > self.b) and (self.b + self |
| .c > self.a) | | .c > self.a) |
| | | |
| def __bool__(self): | | def __bool__(self): |
| return self.valid | | return self.valid |
| | | |
| def __abs__(self): | | def __abs__(self): |
| n | if not self.valid: | n | if not self: |
| return 0.0 | | return 0.0 |
| t | s = (self.a + self.b + self.c) / 2 | t | p = (self.a + self.b + self.c) / 2 |
| return sqrt(s * (s - self.a) * (s - self.b) * (s - 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 |
| return all((isclose(x, y) for x, y in zip(sorted((self.a, self.b | | return all((isclose(x, y) for x, y in zip(sorted((self.a, self.b |
| , self.c)), sorted((other.a, other.b, other.c))))) | | , self.c)), sorted((other.a, other.b, other.c))))) |
| | | |
| 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}' |