Skip to content

Commit 76a1cf0

Browse files
authored
Merge pull request #33 from ikostan/exercism-sync/2d8e06b48072e81b
[Sync Iteration] python/triangle/1
2 parents e979072 + d08c23f commit 76a1cf0

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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

0 commit comments

Comments
 (0)