mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-31 06:33:44 +00:00
Add surface area class (#2183)
* add surface area class * add new line to end of file * move surface area class into area class * add missing import * added pi import * fix typo * added blank line * fixed more import issues * comment fix * comment fixes
This commit is contained in:
parent
2c75a7b3dd
commit
48df91d48b
|
@ -1,8 +1,34 @@
|
||||||
"""
|
"""
|
||||||
Find the area of various geometric shapes
|
Find the area of various geometric shapes
|
||||||
"""
|
"""
|
||||||
|
from math import pi
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
import math
|
|
||||||
|
def surface_area_cube(side_length: Union[int, float]) -> float:
|
||||||
|
"""
|
||||||
|
Calculate the Surface Area of a Cube.
|
||||||
|
|
||||||
|
>>> surface_area_cube(1)
|
||||||
|
6
|
||||||
|
>>> surface_area_cube(3)
|
||||||
|
54
|
||||||
|
"""
|
||||||
|
return 6 * pow(side_length, 2)
|
||||||
|
|
||||||
|
|
||||||
|
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
|
||||||
|
"""
|
||||||
|
return 4 * pi * pow(radius, 2)
|
||||||
|
|
||||||
|
|
||||||
def area_rectangle(base, height):
|
def area_rectangle(base, height):
|
||||||
|
@ -62,7 +88,7 @@ def area_circle(radius):
|
||||||
>> area_circle(20)
|
>> area_circle(20)
|
||||||
1256.6370614359173
|
1256.6370614359173
|
||||||
"""
|
"""
|
||||||
return math.pi * radius * radius
|
return pi * radius * radius
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
@ -73,6 +99,9 @@ def main():
|
||||||
print(f"Parallelogram: {area_parallelogram(10, 20)=}")
|
print(f"Parallelogram: {area_parallelogram(10, 20)=}")
|
||||||
print(f"Trapezium: {area_trapezium(10, 20, 30)=}")
|
print(f"Trapezium: {area_trapezium(10, 20, 30)=}")
|
||||||
print(f"Circle: {area_circle(20)=}")
|
print(f"Circle: {area_circle(20)=}")
|
||||||
|
print("Surface Areas of various geometric shapes: \n")
|
||||||
|
print(f"Cube: {surface_area_cube(20)=}")
|
||||||
|
print(f"Sphere: {surface_area_sphere(20)=}")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
Loading…
Reference in New Issue
Block a user