Skip to content

Commit 48df91d

Browse files
authored
Add surface area class (TheAlgorithms#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
1 parent 2c75a7b commit 48df91d

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

maths/area.py

+31-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,34 @@
11
"""
22
Find the area of various geometric shapes
33
"""
4+
from math import pi
5+
from typing import Union
46

5-
import math
7+
8+
def surface_area_cube(side_length: Union[int, float]) -> float:
9+
"""
10+
Calculate the Surface Area of a Cube.
11+
12+
>>> surface_area_cube(1)
13+
6
14+
>>> surface_area_cube(3)
15+
54
16+
"""
17+
return 6 * pow(side_length, 2)
18+
19+
20+
def surface_area_sphere(radius: float) -> float:
21+
"""
22+
Calculate the Surface Area of a Sphere.
23+
Wikipedia reference: https://en.wikipedia.org/wiki/Sphere
24+
:return 4 * pi * r^2
25+
26+
>>> surface_area_sphere(5)
27+
314.1592653589793
28+
>>> surface_area_sphere(1)
29+
12.566370614359172
30+
"""
31+
return 4 * pi * pow(radius, 2)
632

733

834
def area_rectangle(base, height):
@@ -62,7 +88,7 @@ def area_circle(radius):
6288
>> area_circle(20)
6389
1256.6370614359173
6490
"""
65-
return math.pi * radius * radius
91+
return pi * radius * radius
6692

6793

6894
def main():
@@ -73,6 +99,9 @@ def main():
7399
print(f"Parallelogram: {area_parallelogram(10, 20)=}")
74100
print(f"Trapezium: {area_trapezium(10, 20, 30)=}")
75101
print(f"Circle: {area_circle(20)=}")
102+
print("Surface Areas of various geometric shapes: \n")
103+
print(f"Cube: {surface_area_cube(20)=}")
104+
print(f"Sphere: {surface_area_sphere(20)=}")
76105

77106

78107
if __name__ == "__main__":

0 commit comments

Comments
 (0)