Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Trigonometry Review! solution in Clear category for The Angles of a Triangle by maughan.john
from math import acos, degrees
def checkio(a, b, c):
"""Solve for the angles of any triangle given three sides.
Args
a, b, c -- side lengths of the triangle
Returns
The three angles of the triangle as a list sorted in ascending
order, with the angles, in degrees, rounded to integers.
If the triangle is degenerate, return [0, 0, 0].
Note that it is usually better to raise an exception than to return
zeroes like this from a function. Exceptions are easier to catch and tend
to minimize bugs.
"""
angles = []
try:
# Loop through three times to solve all three angles.
for _ in range(3):
# Use the Triangle Inequality Theorem to test for a true triangle.
if a + b <= c:
raise ValueError('Degenerate triangle!')
# math.acos returns its answer in radians.
rad_angle = acos((a**2 + b**2 - c**2) / (2 * a * b))
deg_angle = degrees(rad_angle)
angles.append(round(deg_angle))
# Rotate triangle to solve for different side.
a, b, c = b, c, a
return sorted(angles)
except ValueError:
return [0, 0, 0]
Dec. 15, 2016