File tree Expand file tree Collapse file tree 1 file changed +49
-0
lines changed
solutions/python/triangle/1 Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change 1+ """Determine if a triangle is equilateral, isosceles, or scalene."""
2+
3+
4+ def equilateral (sides : list ) -> bool :
5+ """
6+ An equilateral triangle has all three sides the same length.
7+ """
8+ if check_for_zeroes (sides ) and inequality_violation (sides ):
9+ return sides [0 ] == sides [1 ] == sides [2 ]
10+ return False
11+
12+
13+ def isosceles (sides : list ) -> bool :
14+ """
15+ An isosceles triangle has at least two sides the same length.
16+ (It is sometimes specified as having exactly two sides the same length,
17+ but for the purposes of this exercise we'll say at least two.)
18+ """
19+ if check_for_zeroes (sides ) and inequality_violation (sides ):
20+ return sides [0 ] == sides [1 ] or sides [1 ] == sides [2 ] or sides [0 ] == sides [2 ]
21+ return False
22+
23+
24+ def scalene (sides : list ) -> bool :
25+ """
26+ A scalene triangle has all sides of different lengths.
27+ """
28+ if check_for_zeroes (sides ) and inequality_violation (sides ):
29+ return sides [0 ] != sides [1 ] and sides [1 ] != sides [2 ] and sides [0 ] != sides [2 ]
30+ return False
31+
32+
33+ def inequality_violation (sides : list ) -> bool :
34+ """
35+ Let a, b, and c be sides of the triangle.
36+ Then all three of the following expressions must be true:
37+
38+ a + b ≥ c
39+ b + c ≥ a
40+ a + c ≥ b
41+ """
42+ return sides [0 ] + sides [1 ] >= sides [2 ] and sides [0 ] + sides [2 ] >= sides [1 ] and sides [2 ] + sides [1 ] >= sides [0 ]
43+
44+
45+ def check_for_zeroes (sides : list ) -> bool :
46+ """No zeroes allowed."""
47+ if 0 in sides :
48+ return False
49+ return True
You can’t perform that action at this time.
0 commit comments