|
3 | 3 |
|
4 | 4 | Wikipedia reference: https://en.wikipedia.org/wiki/Volume
|
5 | 5 | """
|
| 6 | +from typing import Union |
| 7 | +from math import pi, pow |
6 | 8 |
|
7 |
| -from math import pi |
8 | 9 |
|
| 10 | +def vol_cube(side_length: Union[int, float]) -> float: |
| 11 | + """ |
| 12 | + Calculate the Volume of a Cube. |
9 | 13 |
|
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) |
14 | 20 |
|
15 | 21 |
|
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 | + """ |
19 | 32 | return float(width * height * length)
|
20 | 33 |
|
21 | 34 |
|
22 |
| -def vol_cone(area_of_base, height): |
| 35 | +def vol_cone(area_of_base: float, height: float) -> float: |
23 | 36 | """
|
24 | 37 | Calculate the Volume of a Cone.
|
25 | 38 |
|
26 | 39 | 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 |
28 | 46 | """
|
29 |
| - return (float(1) / 3) * area_of_base * height |
| 47 | + return area_of_base * height / 3.0 |
30 | 48 |
|
31 | 49 |
|
32 |
| -def vol_right_circ_cone(radius, height): |
| 50 | +def vol_right_circ_cone(radius: float, height: float) -> float: |
33 | 51 | """
|
34 | 52 | Calculate the Volume of a Right Circular Cone.
|
35 | 53 |
|
36 | 54 | 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 |
39 | 56 |
|
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 |
41 | 61 |
|
42 | 62 |
|
43 |
| -def vol_prism(area_of_base, height): |
| 63 | +def vol_prism(area_of_base: float, height: float) -> float: |
44 | 64 | """
|
45 | 65 | Calculate the Volume of a Prism.
|
46 |
| -
|
47 |
| - V = Bh |
48 | 66 | 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 |
49 | 73 | """
|
50 | 74 | return float(area_of_base * height)
|
51 | 75 |
|
52 | 76 |
|
53 |
| -def vol_pyramid(area_of_base, height): |
| 77 | +def vol_pyramid(area_of_base: float, height: float) -> float: |
54 | 78 | """
|
55 |
| - Calculate the Volume of a Prism. |
56 |
| -
|
57 |
| - V = (1/3) * Bh |
| 79 | + Calculate the Volume of a Pyramid. |
58 | 80 | 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 |
59 | 87 | """
|
60 |
| - return (float(1) / 3) * area_of_base * height |
| 88 | + return area_of_base * height / 3.0 |
61 | 89 |
|
62 | 90 |
|
63 |
| -def vol_sphere(radius): |
| 91 | +def vol_sphere(radius: float) -> float: |
64 | 92 | """
|
65 | 93 | Calculate the Volume of a Sphere.
|
66 |
| -
|
67 |
| - V = (4/3) * pi * r^3 |
68 | 94 | 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 |
69 | 101 | """
|
70 |
| - return (float(4) / 3) * pi * radius ** 3 |
| 102 | + return 4 / 3 * pi * pow(radius, 3) |
71 | 103 |
|
72 | 104 |
|
73 |
| -def vol_circular_cylinder(radius, height): |
| 105 | +def vol_circular_cylinder(radius: float, height: float) -> float: |
74 | 106 | """Calculate the Volume of a Circular Cylinder.
|
75 |
| -
|
76 | 107 | 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 |
78 | 114 | """
|
79 |
| - return pi * radius ** 2 * height |
| 115 | + return pi * pow(radius, 2) * height |
80 | 116 |
|
81 | 117 |
|
82 | 118 | def main():
|
|
0 commit comments