triangle.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
39
40
class TriangleError(Exception):
    pass


class Triangle:
    EQUILATERAL = 'equilateral'
    ISOSCELES = 'isosceles'
    SCALENE = 'scalene'

    def __init__(self, *sides):
        self._sides = sides
        if not self.is_legal_triangle:
            raise TriangleError('Does not satisfy the Triangle inequality')

    @property
    def is_equilateral(self):
        return len(set(self._sides)) == 1

    @property
    def is_isosceles(self):
        return len(set(self._sides)) == 2

    @property
    def is_legal_triangle(self):
        return (
            all(side > 0 for side in self._sides) and
            self.satisfies_triangle_inequality
        )

    @property
    def satisfies_triangle_inequality(self):
        return sum(self._sides) > max(self._sides) * 2

    def kind(self):
        if self.is_equilateral:
            return self.EQUILATERAL
        elif self.is_isosceles:
            return self.ISOSCELES
        else:
            return self.SCALENE

@cds-amal thinks this looks great

Comments


You're not logged in right now. Please login via GitHub to comment