Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Charon solution in Clear category for Simple Areas by Vulwsztyn
import math
def simple_areas(*args):
l=[]
for i in args:
l.append(i)
if len(l)==1:
return 1/4*l[0]**2*math.pi
elif len(l)==2:
return l[0]*l[1]
else:
p=l[0]+l[1]+l[2]
p/=2
return (p*(p-l[0])*(p-l[1])*(p-l[2]))**(1/2)
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. 22, 2016