Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Law of Cosines solution in Clear category for The Angles of a Triangle by kkkkk
from math import acos
from math import cos
from math import degrees
from typing import List
def checkio(a: int, b: int, c: int) -> List[int]:
"""Find the angles of a triangle in degrees given lengths of sides."""
# If the smaller two sides are not greater than the third side,
# these sides can't form a triangle.
sorted_sides = sorted([a, b, c])
if sum(sorted_sides[0:2]) <= sorted_sides[2]:
return [0, 0, 0]
# Law of Cosines: cos(C) = (a**2 + b**2 - c**2) / (2 * a * b)
cos_c = (a**2 + b** 2 - c**2) / (2 * a * b)
degree_c = round(degrees(acos(cos_c)))
cos_a = (b**2 + c** 2 - a**2) / (2 * b * c)
degree_a = round(degrees(acos(cos_a)))
degree_b = 180 - (degree_c + degree_a)
return sorted([degree_a, degree_b, degree_c])
March 25, 2020
Comments: