Skip to content

Commit b395003

Browse files
authored
update variable names for consistency using standard formula terms; (TheAlgorithms#2223)
* update variable names for consistency using standard formula terms; fix flake8 syntax errors; fix doctests; * tweak to variable name
1 parent 9ec71cb commit b395003

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed

maths/area.py

+25-20
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ def surface_area_sphere(radius: float) -> float:
3131
return 4 * pi * pow(radius, 2)
3232

3333

34-
def area_rectangle(base, height):
34+
def area_rectangle(length, width):
3535
"""
3636
Calculate the area of a rectangle
3737
38-
>> area_rectangle(10,20)
38+
>>> area_rectangle(10,20)
3939
200
4040
"""
41-
return base * height
41+
return length * width
4242

4343

4444
def area_square(side_length):
@@ -48,24 +48,24 @@ def area_square(side_length):
4848
>>> area_square(10)
4949
100
5050
"""
51-
return side_length * side_length
51+
return pow(side_length, 2)
5252

5353

54-
def area_triangle(length, breadth):
54+
def area_triangle(base, height):
5555
"""
5656
Calculate the area of a triangle
5757
5858
>>> area_triangle(10,10)
5959
50.0
6060
"""
61-
return 1 / 2 * length * breadth
61+
return (base * height) / 2
6262

6363

6464
def area_parallelogram(base, height):
6565
"""
6666
Calculate the area of a parallelogram
6767
68-
>> area_parallelogram(10,20)
68+
>>> area_parallelogram(10,20)
6969
200
7070
"""
7171
return base * height
@@ -75,8 +75,8 @@ def area_trapezium(base1, base2, height):
7575
"""
7676
Calculate the area of a trapezium
7777
78-
>> area_trapezium(10,20,30)
79-
450
78+
>>> area_trapezium(10,20,30)
79+
450.0
8080
"""
8181
return 1 / 2 * (base1 + base2) * height
8282

@@ -85,24 +85,29 @@ def area_circle(radius):
8585
"""
8686
Calculate the area of a circle
8787
88-
>> area_circle(20)
88+
>>> area_circle(20)
8989
1256.6370614359173
9090
"""
91-
return pi * radius * radius
91+
return pi * pow(radius, 2)
9292

9393

9494
def main():
9595
print("Areas of various geometric shapes: \n")
96-
print(f"Rectangle: {area_rectangle(10, 20)=}")
97-
print(f"Square: {area_square(10)=}")
98-
print(f"Triangle: {area_triangle(10, 10)=}")
99-
print(f"Parallelogram: {area_parallelogram(10, 20)=}")
100-
print(f"Trapezium: {area_trapezium(10, 20, 30)=}")
101-
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)=}")
96+
print(f"Rectangle: {area_rectangle(10, 20)}")
97+
print(f"Square: {area_square(10)}")
98+
print(f"Triangle: {area_triangle(10, 10)}")
99+
print(f"Parallelogram: {area_parallelogram(10, 20)}")
100+
print(f"Trapezium: {area_trapezium(10, 20, 30)}")
101+
print(f"Circle: {area_circle(20)}")
102+
print("\nSurface Areas of various geometric shapes: \n")
103+
print(f"Cube: {surface_area_cube(20)}")
104+
print(f"Sphere: {surface_area_sphere(20)}")
105105

106106

107107
if __name__ == "__main__":
108+
109+
import doctest
110+
111+
doctest.testmod(verbose=True) # verbose so we can see methods missing tests
112+
108113
main()

0 commit comments

Comments
 (0)