-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple-areas.py
25 lines (22 loc) · 1.09 KB
/
simple-areas.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
def simple_areas(*args):
import math
if len(args) == 1:
radius = args[0] / 2.0
return round(radius * radius * math.pi, 2)
elif len(args) == 2:
return round(args[0] * args[1], 2)
elif len(args) == 3:
s = (args[0] + args[1] + args[2]) / 2.0
return round(math.sqrt(s * (s - args[0]) * (s - args[1]) * (s - args[2])), 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"
assert almost_equal(simple_areas(1000), 785398.16), "Big Circle"
assert almost_equal(simple_areas(1,1,1), 0.43), "Simple Triangles"