Skip to content

Commit

Permalink
Update maths/area.py (#2501)
Browse files Browse the repository at this point in the history
the parameters of geometric shapes should be non-negative values
  • Loading branch information
taseikyo authored Sep 28, 2020
1 parent 1b637ba commit 121dddc
Showing 1 changed file with 104 additions and 8 deletions.
112 changes: 104 additions & 8 deletions maths/area.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ def surface_area_cube(side_length: float) -> float:
6
>>> surface_area_cube(3)
54
>>> surface_area_cube(-1)
Traceback (most recent call last):
...
ValueError: surface_area_cube() only accepts non-negative values
"""
if side_length < 0:
raise ValueError("surface_area_cube() only accepts non-negative values")
return 6 * side_length ** 2


Expand All @@ -26,17 +32,37 @@ def surface_area_sphere(radius: float) -> float:
314.1592653589793
>>> surface_area_sphere(1)
12.566370614359172
>>> surface_area_sphere(-1)
Traceback (most recent call last):
...
ValueError: surface_area_sphere() only accepts non-negative values
"""
if radius < 0:
raise ValueError("surface_area_sphere() only accepts non-negative values")
return 4 * pi * radius ** 2


def area_rectangle(length: float, width: float) -> float:
"""
Calculate the area of a rectangle
>>> area_rectangle(10,20)
>>> area_rectangle(10, 20)
200
"""
>>> area_rectangle(-1, -2)
Traceback (most recent call last):
...
ValueError: area_rectangle() only accepts non-negative values
>>> area_rectangle(1, -2)
Traceback (most recent call last):
...
ValueError: area_rectangle() only accepts non-negative values
>>> area_rectangle(-1, 2)
Traceback (most recent call last):
...
ValueError: area_rectangle() only accepts non-negative values
"""
if length < 0 or width < 0:
raise ValueError("area_rectangle() only accepts non-negative values")
return length * width


Expand All @@ -46,37 +72,101 @@ def area_square(side_length: float) -> float:
>>> area_square(10)
100
>>> area_square(-1)
Traceback (most recent call last):
...
ValueError: area_square() only accepts non-negative values
"""
if side_length < 0:
raise ValueError("area_square() only accepts non-negative values")
return side_length ** 2


def area_triangle(base: float, height: float) -> float:
"""
Calculate the area of a triangle
>>> area_triangle(10,10)
>>> area_triangle(10, 10)
50.0
"""
>>> area_triangle(-1, -2)
Traceback (most recent call last):
...
ValueError: area_triangle() only accepts non-negative values
>>> area_triangle(1, -2)
Traceback (most recent call last):
...
ValueError: area_triangle() only accepts non-negative values
>>> area_triangle(-1, 2)
Traceback (most recent call last):
...
ValueError: area_triangle() only accepts non-negative values
"""
if base < 0 or height < 0:
raise ValueError("area_triangle() only accepts non-negative values")
return (base * height) / 2


def area_parallelogram(base: float, height: float) -> float:
"""
Calculate the area of a parallelogram
>>> area_parallelogram(10,20)
>>> area_parallelogram(10, 20)
200
"""
>>> area_parallelogram(-1, -2)
Traceback (most recent call last):
...
ValueError: area_parallelogram() only accepts non-negative values
>>> area_parallelogram(1, -2)
Traceback (most recent call last):
...
ValueError: area_parallelogram() only accepts non-negative values
>>> area_parallelogram(-1, 2)
Traceback (most recent call last):
...
ValueError: area_parallelogram() only accepts non-negative values
"""
if base < 0 or height < 0:
raise ValueError("area_parallelogram() only accepts non-negative values")
return base * height


def area_trapezium(base1: float, base2: float, height: float) -> float:
"""
Calculate the area of a trapezium
>>> area_trapezium(10,20,30)
>>> area_trapezium(10, 20, 30)
450.0
"""
>>> area_trapezium(-1, -2, -3)
Traceback (most recent call last):
...
ValueError: area_trapezium() only accepts non-negative values
>>> area_trapezium(-1, 2, 3)
Traceback (most recent call last):
...
ValueError: area_trapezium() only accepts non-negative values
>>> area_trapezium(1, -2, 3)
Traceback (most recent call last):
...
ValueError: area_trapezium() only accepts non-negative values
>>> area_trapezium(1, 2, -3)
Traceback (most recent call last):
...
ValueError: area_trapezium() only accepts non-negative values
>>> area_trapezium(-1, -2, 3)
Traceback (most recent call last):
...
ValueError: area_trapezium() only accepts non-negative values
>>> area_trapezium(1, -2, -3)
Traceback (most recent call last):
...
ValueError: area_trapezium() only accepts non-negative values
>>> area_trapezium(-1, 2, -3)
Traceback (most recent call last):
...
ValueError: area_trapezium() only accepts non-negative values
"""
if base1 < 0 or base2 < 0 or height < 0:
raise ValueError("area_trapezium() only accepts non-negative values")
return 1 / 2 * (base1 + base2) * height


Expand All @@ -86,7 +176,13 @@ def area_circle(radius: float) -> float:
>>> area_circle(20)
1256.6370614359173
>>> area_circle(-1)
Traceback (most recent call last):
...
ValueError: area_circle() only accepts non-negative values
"""
if radius < 0:
raise ValueError("area_circle() only accepts non-negative values")
return pi * radius ** 2


Expand Down

0 comments on commit 121dddc

Please sign in to comment.