Skip to content

Commit e979072

Browse files
authored
Merge pull request #34 from ikostan/exercism-sync/a5f63c45875196e6
[Sync Iteration] python/triangle/2
2 parents 279dfde + a4d3582 commit e979072

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 sides[0] + sides[2] >= sides[1] and sides[2] + sides[1] >= sides[0]
43+
44+
45+
def all_sides_positive(sides: list) -> bool:
46+
"""No zeroes or negative numbers allowed."""
47+
return all(side > 0 for side in sides) and len(sides) == 3

0 commit comments

Comments
 (0)