You are given three integers a
, b
, and c
, which represent the lengths of the sides of a triangle. You need to determine and print the type of triangle based on the lengths of its sides.
The possible outputs are:
a
, b
, and c
are equal, then print “Equilateral”.Since the problem guarantees the input forms a valid triangle, we don’t need to perform additional validity checks for the side lengths.
def triangle_type(a: int, b: int, c: int) -> str:
if a == b == c:
return "Equilateral"
elif a == b or b == c or a == c:
return "Isosceles"
else:
return "Scalene"
# Example usage:
print(triangle_type(3, 3, 3)) # Should print "Equilateral"
print(triangle_type(2, 2, 3)) # Should print "Isosceles"
print(triangle_type(3, 4, 5)) # Should print "Scalene"
The function performs a constant number of comparisons and operations, hence the time complexity is O(1)
(constant time).
This solution is efficient and straightforward given the constraints of the problem.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?