Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Simple Areas by Kacper_Kapela
"""
W tym zadaniu musimy obliczyc pole kola, trojkata, lub prostokata
jezeli mam dana jedna zmienna to pole kola, jezeli dwie zmienne to prostokata, jezeli trzy trojkata
"""
"""
Pobieramy biblioteke math aby skorzystac z liczby pi(math.pi oraz z pierwiastka math.sqrt()
1. Jesli dlugosc pierwszego argumentu jest 1 to
Nasz argument(srednica) przez 2 do kwadratu razy pi i mamy pole kola
2. Jesli dlugosc drugiego jest 2 to
Watosc args 1 razy wartosc args 2 daje pole prostokata
3. Jesli dlugosc 3 to pole trojkata (pole Herona)
czyli najpierw robimy polowe obwodu a pozniej wedlug wzorku
"""
import math
def simple_areas(*args):
if len(args) == 1:
return (args[0] / 2.0)**2 * math.pi
elif len(args) == 2:
return args[0]*args[1]
elif len(args) == 3:
p = (args[0]+args[1]+args[2])/2
return math.sqrt(p*((p-args[0])*(p-args[1])*(p-args[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"
Dec. 17, 2015