Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Simple Areas by 9maksim
from math import pi, sqrt
def simple_areas(*args):
type = len(args)
if type == 1: # Circle
checked = (args[0]/2)**2 * pi
if type == 2: # Rectangle
checked = args[0] * args[1]
if type == 3: # Triangle
p = (args[0] + args[1] + args[2])/2 #half perimetr
checked = sqrt(p*(p-args[0])*(p-args[1])*(p -args[2]))
return checked
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"
June 13, 2020