From f56652b07b2d6b121bdb043ffc1c77d0fabdcd9c Mon Sep 17 00:00:00 2001 From: "exercism-solutions-syncer[bot]" <211797793+exercism-solutions-syncer[bot]@users.noreply.github.com> Date: Tue, 26 Aug 2025 04:30:29 +0000 Subject: [PATCH] [Sync Iteration] python/triangle/2 --- solutions/python/triangle/2/triangle.py | 47 +++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 solutions/python/triangle/2/triangle.py diff --git a/solutions/python/triangle/2/triangle.py b/solutions/python/triangle/2/triangle.py new file mode 100644 index 0000000..bac8e96 --- /dev/null +++ b/solutions/python/triangle/2/triangle.py @@ -0,0 +1,47 @@ +"""Determine if a triangle is equilateral, isosceles, or scalene.""" + + +def equilateral(sides: list) -> bool: + """ + An equilateral triangle has all three sides the same length. + """ + if all_sides_positive(sides) and no_inequality_violation(sides): + return sides[0] == sides[1] == sides[2] + return False + + +def isosceles(sides: list) -> bool: + """ + An isosceles triangle has at least two sides the same length. + (It is sometimes specified as having exactly two sides the same length, + but for the purposes of this exercise we'll say at least two.) + """ + if all_sides_positive(sides) and no_inequality_violation(sides): + return sides[0] == sides[1] or sides[1] == sides[2] or sides[0] == sides[2] + return False + + +def scalene(sides: list) -> bool: + """ + A scalene triangle has all sides of different lengths. + """ + if all_sides_positive(sides) and no_inequality_violation(sides): + return sides[0] != sides[1] and sides[1] != sides[2] and sides[0] != sides[2] + return False + + +def no_inequality_violation(sides: list) -> bool: + """ + Let a, b, and c be sides of the triangle. + Then all three of the following expressions must be true: + + a + b ≥ c + b + c ≥ a + a + c ≥ b + """ + return sides[0] + sides[1] >= sides[2] and sides[0] + sides[2] >= sides[1] and sides[2] + sides[1] >= sides[0] + + +def all_sides_positive(sides: list) -> bool: + """No zeroes or negative numbers allowed.""" + return all(side > 0 for side in sides) and len(sides) == 3