2020-05-18 21:06:19 +00:00
|
|
|
"""
|
|
|
|
Find the area of various geometric shapes
|
|
|
|
"""
|
2020-07-06 22:21:59 +00:00
|
|
|
from math import pi
|
2020-05-18 21:06:19 +00:00
|
|
|
|
2020-07-06 22:21:59 +00:00
|
|
|
|
2020-07-27 13:29:31 +00:00
|
|
|
def surface_area_cube(side_length: float) -> float:
|
2020-07-06 22:21:59 +00:00
|
|
|
"""
|
|
|
|
Calculate the Surface Area of a Cube.
|
|
|
|
|
|
|
|
>>> surface_area_cube(1)
|
|
|
|
6
|
|
|
|
>>> surface_area_cube(3)
|
|
|
|
54
|
2020-09-28 14:40:47 +00:00
|
|
|
>>> surface_area_cube(-1)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
ValueError: surface_area_cube() only accepts non-negative values
|
2020-07-06 22:21:59 +00:00
|
|
|
"""
|
2020-09-28 14:40:47 +00:00
|
|
|
if side_length < 0:
|
|
|
|
raise ValueError("surface_area_cube() only accepts non-negative values")
|
2020-07-27 13:29:31 +00:00
|
|
|
return 6 * side_length ** 2
|
2020-07-06 22:21:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
def surface_area_sphere(radius: float) -> float:
|
|
|
|
"""
|
|
|
|
Calculate the Surface Area of a Sphere.
|
|
|
|
Wikipedia reference: https://en.wikipedia.org/wiki/Sphere
|
|
|
|
:return 4 * pi * r^2
|
|
|
|
|
|
|
|
>>> surface_area_sphere(5)
|
|
|
|
314.1592653589793
|
|
|
|
>>> surface_area_sphere(1)
|
|
|
|
12.566370614359172
|
2020-09-28 14:40:47 +00:00
|
|
|
>>> surface_area_sphere(-1)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
ValueError: surface_area_sphere() only accepts non-negative values
|
2020-07-06 22:21:59 +00:00
|
|
|
"""
|
2020-09-28 14:40:47 +00:00
|
|
|
if radius < 0:
|
|
|
|
raise ValueError("surface_area_sphere() only accepts non-negative values")
|
2020-07-27 13:29:31 +00:00
|
|
|
return 4 * pi * radius ** 2
|
2020-05-18 21:06:19 +00:00
|
|
|
|
|
|
|
|
2020-07-27 13:29:31 +00:00
|
|
|
def area_rectangle(length: float, width: float) -> float:
|
2020-05-18 21:06:19 +00:00
|
|
|
"""
|
|
|
|
Calculate the area of a rectangle
|
|
|
|
|
2020-09-28 14:40:47 +00:00
|
|
|
>>> area_rectangle(10, 20)
|
2020-05-18 21:06:19 +00:00
|
|
|
200
|
2020-09-28 14:40:47 +00:00
|
|
|
>>> 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")
|
2020-07-22 22:49:34 +00:00
|
|
|
return length * width
|
2020-05-18 21:06:19 +00:00
|
|
|
|
|
|
|
|
2020-07-27 13:29:31 +00:00
|
|
|
def area_square(side_length: float) -> float:
|
2020-05-18 21:06:19 +00:00
|
|
|
"""
|
|
|
|
Calculate the area of a square
|
|
|
|
|
|
|
|
>>> area_square(10)
|
|
|
|
100
|
2020-09-28 14:40:47 +00:00
|
|
|
>>> area_square(-1)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
ValueError: area_square() only accepts non-negative values
|
2020-05-18 21:06:19 +00:00
|
|
|
"""
|
2020-09-28 14:40:47 +00:00
|
|
|
if side_length < 0:
|
|
|
|
raise ValueError("area_square() only accepts non-negative values")
|
2020-07-27 13:29:31 +00:00
|
|
|
return side_length ** 2
|
2020-05-18 21:06:19 +00:00
|
|
|
|
|
|
|
|
2020-07-27 13:29:31 +00:00
|
|
|
def area_triangle(base: float, height: float) -> float:
|
2020-05-18 21:06:19 +00:00
|
|
|
"""
|
|
|
|
Calculate the area of a triangle
|
|
|
|
|
2020-09-28 14:40:47 +00:00
|
|
|
>>> area_triangle(10, 10)
|
2020-05-18 21:06:19 +00:00
|
|
|
50.0
|
2020-09-28 14:40:47 +00:00
|
|
|
>>> 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")
|
2020-07-22 22:49:34 +00:00
|
|
|
return (base * height) / 2
|
2020-05-18 21:06:19 +00:00
|
|
|
|
|
|
|
|
2020-07-27 13:29:31 +00:00
|
|
|
def area_parallelogram(base: float, height: float) -> float:
|
2020-05-18 21:06:19 +00:00
|
|
|
"""
|
|
|
|
Calculate the area of a parallelogram
|
|
|
|
|
2020-09-28 14:40:47 +00:00
|
|
|
>>> area_parallelogram(10, 20)
|
2020-05-18 21:06:19 +00:00
|
|
|
200
|
2020-09-28 14:40:47 +00:00
|
|
|
>>> 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")
|
2020-05-18 21:06:19 +00:00
|
|
|
return base * height
|
|
|
|
|
|
|
|
|
2020-07-27 13:29:31 +00:00
|
|
|
def area_trapezium(base1: float, base2: float, height: float) -> float:
|
2020-05-18 21:06:19 +00:00
|
|
|
"""
|
|
|
|
Calculate the area of a trapezium
|
|
|
|
|
2020-09-28 14:40:47 +00:00
|
|
|
>>> area_trapezium(10, 20, 30)
|
2020-07-22 22:49:34 +00:00
|
|
|
450.0
|
2020-09-28 14:40:47 +00:00
|
|
|
>>> 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")
|
2020-05-18 21:06:19 +00:00
|
|
|
return 1 / 2 * (base1 + base2) * height
|
|
|
|
|
|
|
|
|
2020-07-27 13:29:31 +00:00
|
|
|
def area_circle(radius: float) -> float:
|
2020-05-18 21:06:19 +00:00
|
|
|
"""
|
|
|
|
Calculate the area of a circle
|
|
|
|
|
2020-07-22 22:49:34 +00:00
|
|
|
>>> area_circle(20)
|
2020-05-18 21:06:19 +00:00
|
|
|
1256.6370614359173
|
2020-09-28 14:40:47 +00:00
|
|
|
>>> area_circle(-1)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
ValueError: area_circle() only accepts non-negative values
|
2020-05-18 21:06:19 +00:00
|
|
|
"""
|
2020-09-28 14:40:47 +00:00
|
|
|
if radius < 0:
|
|
|
|
raise ValueError("area_circle() only accepts non-negative values")
|
2020-07-27 13:29:31 +00:00
|
|
|
return pi * radius ** 2
|
2020-05-18 21:06:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
print("Areas of various geometric shapes: \n")
|
2020-07-22 22:49:34 +00:00
|
|
|
print(f"Rectangle: {area_rectangle(10, 20)}")
|
|
|
|
print(f"Square: {area_square(10)}")
|
|
|
|
print(f"Triangle: {area_triangle(10, 10)}")
|
|
|
|
print(f"Parallelogram: {area_parallelogram(10, 20)}")
|
|
|
|
print(f"Trapezium: {area_trapezium(10, 20, 30)}")
|
|
|
|
print(f"Circle: {area_circle(20)}")
|
|
|
|
print("\nSurface Areas of various geometric shapes: \n")
|
|
|
|
print(f"Cube: {surface_area_cube(20)}")
|
|
|
|
print(f"Sphere: {surface_area_sphere(20)}")
|
2020-05-18 21:06:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2020-07-22 22:49:34 +00:00
|
|
|
|
|
|
|
import doctest
|
|
|
|
|
|
|
|
doctest.testmod(verbose=True) # verbose so we can see methods missing tests
|
|
|
|
|
2020-05-18 21:06:19 +00:00
|
|
|
main()
|