Skip to content

Commit 1cc817b

Browse files
yuriimchgcclauss
andcommitted
update volumes with type hints + some refactoring (TheAlgorithms#1353)
* update volumes with type hints + some refactoring * added docstrings * Use float instead of ints in doctest results Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent d4fc55c commit 1cc817b

File tree

1 file changed

+67
-31
lines changed

1 file changed

+67
-31
lines changed

maths/volume.py

+67-31
Original file line numberDiff line numberDiff line change
@@ -3,80 +3,116 @@
33
44
Wikipedia reference: https://en.wikipedia.org/wiki/Volume
55
"""
6+
from typing import Union
7+
from math import pi, pow
68

7-
from math import pi
89

10+
def vol_cube(side_length: Union[int, float]) -> float:
11+
"""
12+
Calculate the Volume of a Cube.
913
10-
def vol_cube(side_length):
11-
"""Calculate the Volume of a Cube."""
12-
# Cube side_length.
13-
return float(side_length ** 3)
14+
>>> vol_cube(1)
15+
1.0
16+
>>> vol_cube(3)
17+
27.0
18+
"""
19+
return pow(side_length, 3)
1420

1521

16-
def vol_cuboid(width, height, length):
17-
"""Calculate the Volume of a Cuboid."""
18-
# Multiply lengths together.
22+
def vol_cuboid(width: float, height: float, length: float) -> float:
23+
"""
24+
Calculate the Volume of a Cuboid.
25+
:return multiple of width, length and height
26+
27+
>>> vol_cuboid(1, 1, 1)
28+
1.0
29+
>>> vol_cuboid(1, 2, 3)
30+
6.0
31+
"""
1932
return float(width * height * length)
2033

2134

22-
def vol_cone(area_of_base, height):
35+
def vol_cone(area_of_base: float, height: float) -> float:
2336
"""
2437
Calculate the Volume of a Cone.
2538
2639
Wikipedia reference: https://en.wikipedia.org/wiki/Cone
27-
volume = (1/3) * area_of_base * height
40+
:return (1/3) * area_of_base * height
41+
42+
>>> vol_cone(10, 3)
43+
10.0
44+
>>> vol_cone(1, 1)
45+
0.3333333333333333
2846
"""
29-
return (float(1) / 3) * area_of_base * height
47+
return area_of_base * height / 3.0
3048

3149

32-
def vol_right_circ_cone(radius, height):
50+
def vol_right_circ_cone(radius: float, height: float) -> float:
3351
"""
3452
Calculate the Volume of a Right Circular Cone.
3553
3654
Wikipedia reference: https://en.wikipedia.org/wiki/Cone
37-
volume = (1/3) * pi * radius^2 * height
38-
"""
55+
:return (1/3) * pi * radius^2 * height
3956
40-
return (float(1) / 3) * pi * (radius ** 2) * height
57+
>>> vol_right_circ_cone(2, 3)
58+
12.566370614359172
59+
"""
60+
return pi * pow(radius, 2) * height / 3.0
4161

4262

43-
def vol_prism(area_of_base, height):
63+
def vol_prism(area_of_base: float, height: float) -> float:
4464
"""
4565
Calculate the Volume of a Prism.
46-
47-
V = Bh
4866
Wikipedia reference: https://en.wikipedia.org/wiki/Prism_(geometry)
67+
:return V = Bh
68+
69+
>>> vol_prism(10, 2)
70+
20.0
71+
>>> vol_prism(11, 1)
72+
11.0
4973
"""
5074
return float(area_of_base * height)
5175

5276

53-
def vol_pyramid(area_of_base, height):
77+
def vol_pyramid(area_of_base: float, height: float) -> float:
5478
"""
55-
Calculate the Volume of a Prism.
56-
57-
V = (1/3) * Bh
79+
Calculate the Volume of a Pyramid.
5880
Wikipedia reference: https://en.wikipedia.org/wiki/Pyramid_(geometry)
81+
:return (1/3) * Bh
82+
83+
>>> vol_pyramid(10, 3)
84+
10.0
85+
>>> vol_pyramid(1.5, 3)
86+
1.5
5987
"""
60-
return (float(1) / 3) * area_of_base * height
88+
return area_of_base * height / 3.0
6189

6290

63-
def vol_sphere(radius):
91+
def vol_sphere(radius: float) -> float:
6492
"""
6593
Calculate the Volume of a Sphere.
66-
67-
V = (4/3) * pi * r^3
6894
Wikipedia reference: https://en.wikipedia.org/wiki/Sphere
95+
:return (4/3) * pi * r^3
96+
97+
>>> vol_sphere(5)
98+
523.5987755982989
99+
>>> vol_sphere(1)
100+
4.1887902047863905
69101
"""
70-
return (float(4) / 3) * pi * radius ** 3
102+
return 4 / 3 * pi * pow(radius, 3)
71103

72104

73-
def vol_circular_cylinder(radius, height):
105+
def vol_circular_cylinder(radius: float, height: float) -> float:
74106
"""Calculate the Volume of a Circular Cylinder.
75-
76107
Wikipedia reference: https://en.wikipedia.org/wiki/Cylinder
77-
volume = pi * radius^2 * height
108+
:return pi * radius^2 * height
109+
110+
>>> vol_circular_cylinder(1, 1)
111+
3.141592653589793
112+
>>> vol_circular_cylinder(4, 3)
113+
150.79644737231007
78114
"""
79-
return pi * radius ** 2 * height
115+
return pi * pow(radius, 2) * height
80116

81117

82118
def main():

0 commit comments

Comments
 (0)