Skip to content

Commit 093a56e

Browse files
authored
Remove function overhead in area (TheAlgorithms#2233)
* remove function overhead add type hints * remove unused import
1 parent bd74f20 commit 093a56e

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

maths/area.py

+11-12
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
Find the area of various geometric shapes
33
"""
44
from math import pi
5-
from typing import Union
65

76

8-
def surface_area_cube(side_length: Union[int, float]) -> float:
7+
def surface_area_cube(side_length: float) -> float:
98
"""
109
Calculate the Surface Area of a Cube.
1110
@@ -14,7 +13,7 @@ def surface_area_cube(side_length: Union[int, float]) -> float:
1413
>>> surface_area_cube(3)
1514
54
1615
"""
17-
return 6 * pow(side_length, 2)
16+
return 6 * side_length ** 2
1817

1918

2019
def surface_area_sphere(radius: float) -> float:
@@ -28,10 +27,10 @@ def surface_area_sphere(radius: float) -> float:
2827
>>> surface_area_sphere(1)
2928
12.566370614359172
3029
"""
31-
return 4 * pi * pow(radius, 2)
30+
return 4 * pi * radius ** 2
3231

3332

34-
def area_rectangle(length, width):
33+
def area_rectangle(length: float, width: float) -> float:
3534
"""
3635
Calculate the area of a rectangle
3736
@@ -41,17 +40,17 @@ def area_rectangle(length, width):
4140
return length * width
4241

4342

44-
def area_square(side_length):
43+
def area_square(side_length: float) -> float:
4544
"""
4645
Calculate the area of a square
4746
4847
>>> area_square(10)
4948
100
5049
"""
51-
return pow(side_length, 2)
50+
return side_length ** 2
5251

5352

54-
def area_triangle(base, height):
53+
def area_triangle(base: float, height: float) -> float:
5554
"""
5655
Calculate the area of a triangle
5756
@@ -61,7 +60,7 @@ def area_triangle(base, height):
6160
return (base * height) / 2
6261

6362

64-
def area_parallelogram(base, height):
63+
def area_parallelogram(base: float, height: float) -> float:
6564
"""
6665
Calculate the area of a parallelogram
6766
@@ -71,7 +70,7 @@ def area_parallelogram(base, height):
7170
return base * height
7271

7372

74-
def area_trapezium(base1, base2, height):
73+
def area_trapezium(base1: float, base2: float, height: float) -> float:
7574
"""
7675
Calculate the area of a trapezium
7776
@@ -81,14 +80,14 @@ def area_trapezium(base1, base2, height):
8180
return 1 / 2 * (base1 + base2) * height
8281

8382

84-
def area_circle(radius):
83+
def area_circle(radius: float) -> float:
8584
"""
8685
Calculate the area of a circle
8786
8887
>>> area_circle(20)
8988
1256.6370614359173
9089
"""
91-
return pi * pow(radius, 2)
90+
return pi * radius ** 2
9291

9392

9493
def main():

0 commit comments

Comments
 (0)