TheAlgorithms-Python
73 строки · 2.1 Кб
1# dodecahedron.py
2
3"""
4A regular dodecahedron is a three-dimensional figure made up of
512 pentagon faces having the same equal size.
6"""
7
8
9def dodecahedron_surface_area(edge: float) -> float:
10"""
11Calculates the surface area of a regular dodecahedron
12a = 3 * ((25 + 10 * (5** (1 / 2))) ** (1 / 2 )) * (e**2)
13where:
14a --> is the area of the dodecahedron
15e --> is the length of the edge
16reference-->"Dodecahedron" Study.com
17<https://study.com/academy/lesson/dodecahedron-volume-surface-area-formulas.html>
18
19:param edge: length of the edge of the dodecahedron
20:type edge: float
21:return: the surface area of the dodecahedron as a float
22
23
24Tests:
25>>> dodecahedron_surface_area(5)
26516.1432201766901
27>>> dodecahedron_surface_area(10)
282064.5728807067603
29>>> dodecahedron_surface_area(-1)
30Traceback (most recent call last):
31...
32ValueError: Length must be a positive.
33"""
34
35if edge <= 0 or not isinstance(edge, int):
36raise ValueError("Length must be a positive.")
37return 3 * ((25 + 10 * (5 ** (1 / 2))) ** (1 / 2)) * (edge**2)
38
39
40def dodecahedron_volume(edge: float) -> float:
41"""
42Calculates the volume of a regular dodecahedron
43v = ((15 + (7 * (5** (1 / 2)))) / 4) * (e**3)
44where:
45v --> is the volume of the dodecahedron
46e --> is the length of the edge
47reference-->"Dodecahedron" Study.com
48<https://study.com/academy/lesson/dodecahedron-volume-surface-area-formulas.html>
49
50:param edge: length of the edge of the dodecahedron
51:type edge: float
52:return: the volume of the dodecahedron as a float
53
54Tests:
55>>> dodecahedron_volume(5)
56957.8898700780791
57>>> dodecahedron_volume(10)
587663.118960624633
59>>> dodecahedron_volume(-1)
60Traceback (most recent call last):
61...
62ValueError: Length must be a positive.
63"""
64
65if edge <= 0 or not isinstance(edge, int):
66raise ValueError("Length must be a positive.")
67return ((15 + (7 * (5 ** (1 / 2)))) / 4) * (edge**3)
68
69
70if __name__ == "__main__":
71import doctest
72
73doctest.testmod()
74