|
| 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 all_sides_positive(sides) and no_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 all_sides_positive(sides) and no_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 all_sides_positive(sides) and no_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 no_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 |
| 43 | + sides[0] + sides[2] >= sides[1] and |
| 44 | + sides[2] + sides[1] >= sides[0]) |
| 45 | + |
| 46 | + |
| 47 | +def all_sides_positive(sides: list) -> bool: |
| 48 | + """ |
| 49 | + No zeroes or negative numbers allowed. |
| 50 | + All triangles should have 3 sides. |
| 51 | + """ |
| 52 | + return all(side > 0 for side in sides) and len(sides) == 3 |
0 commit comments