Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Second solution in Clear category for The Angles of a Triangle by colinmcnicholl
from math import degrees, acos
def calc_angle(x,y,z):
"""Input: The lengths of the sides of the triangle as integers.
Output: The included angle in degrees rounded to the nearest integer.
"""
return int(round(degrees(acos((x**2 + y**2 - z**2) / (2 * x * y))), 0))
def checkio(a, b, c):
"""Input: The lengths of the sides of a triangle as integers.
This function finds all three angles for the triangle if a triangle can
be formed from the three side lengths.
Output: Angles of a triangle in degrees as sorted list of integers.
Return all angles as 0 (zero) if a triangle cannot be formed from the
three side lengths.
"""
if (a + b > c) and (b + c > a) and (a + c > b):
angle_C = calc_angle(a, b, c)
angle_A = calc_angle(b, c, a)
angle_B = 180 - (angle_A + angle_C)
return sorted([angle_A, angle_B, angle_C])
else:
return [0, 0, 0]
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert checkio(4, 4, 4) == [60, 60, 60], "All sides are equal"
assert checkio(3, 4, 5) == [37, 53, 90], "Egyptian triangle"
assert checkio(2, 2, 5) == [0, 0, 0], "It's can not be a triangle"
Feb. 12, 2019