Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Simple Areas by ssk8
def triange(a, b, c):
s = (a + b + c) / 2
return (s*(s - a)*(s - b)*(s - c))**(1/2)
AREA = {
1 : lambda D: 3.14159265359*(D[0]/2)**2,
2 : lambda L: L[0]*L[1],
3 : lambda sides: triange(sides[0], sides[1], sides[2]),
}
def simple_areas(*args):
return AREA[len(args)](args)
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
def almost_equal(checked, correct, significant_digits=2):
precision = 0.1 ** significant_digits
return correct - precision < checked < correct + precision
assert almost_equal(simple_areas(3), 7.07), "Circle"
assert almost_equal(simple_areas(2, 2), 4), "Square"
assert almost_equal(simple_areas(2, 3), 6), "Rectangle"
assert almost_equal(simple_areas(3, 5, 4), 6), "Triangle"
assert almost_equal(simple_areas(1.5, 2.5, 2), 1.5), "Small triangle"
Nov. 27, 2016