3131
3232
3333def gravitational_potential_energy (mass : float , height : float ) -> float :
34- # function will accept mass and height as parameters and return potential energy
3534 """
36- >>> gravitational_potential_energy(10,10)
37- 980.665
38- >>> gravitational_potential_energy(0,5)
39- 0.0
40- >>> gravitational_potential_energy(8,0)
41- 0.0
42- >>> gravitational_potential_energy(10,5)
43- 490.3325
44- >>> gravitational_potential_energy(0,0)
45- 0.0
46- >>> gravitational_potential_energy(2,8)
47- 156.9064
48- >>> gravitational_potential_energy(20,100)
49- 19613.3
35+ Function calculates the gravitational potential energy of an object.
36+
37+ Parameters:
38+ mass: Mass of the object
39+ height: Height the object is at
40+
41+ Returns:
42+ The value of energy in Joules
43+
44+ Complexity:
45+ Time Complexity: O(1) - Constant execution time for evaluation logic.
46+ Space Complexity: O(1) - Constant memory allocation.
47+
48+ Examples:
49+ >>> gravitational_potential_energy(10,10)
50+ 980.665
51+ >>> gravitational_potential_energy(0,5)
52+ 0.0
53+ >>> gravitational_potential_energy(8,0)
54+ 0.0
55+ >>> gravitational_potential_energy(10,5)
56+ 490.3325
57+ >>> gravitational_potential_energy(0,0)
58+ 0.0
59+ >>> gravitational_potential_energy(2,8)
60+ 156.9064
61+ >>> gravitational_potential_energy(20,100)
62+ 19613.3
5063 """
5164 if mass < 0 :
5265 # handling of negative values of mass
@@ -57,24 +70,41 @@ def gravitational_potential_energy(mass: float, height: float) -> float:
5770
5871 return mass * g * height
5972
60- def spring_potential_energy (spring_constant : float , displacement : float ):
61- #Function will except the spring constant and the displacemnt of the spring from equilibrium
73+ def spring_potential_energy (spr_con : float , dspl : float ) -> float :
6274 """
63- >>> spring_potential_energy(100,2)
64- 200
65- >>> gravitational_potential_energy(10,0.5)
66- 1.25
67- >>> spring_potential_energy(8,0)
68- 0.0
75+ Function calculates the spring potential energy of an object.
76+
77+ Parameters:
78+ spr_con: The spring constant of a spring
79+ dspl: The length of the displacement of the spring
80+
81+ Returns:
82+ The value of energy in Joules
83+
84+ Complexity:
85+ Time Complexity: O(1) - Constant execution time for evaluation logic.
86+ Space Complexity: O(1) - Constant memory allocation.
87+
88+ Examples:
89+ >>> spring_potential_energy(100,2)
90+ 200.0
91+ >>> spring_potential_energy(10,0.5)
92+ 1.25
93+ >>> spring_potential_energy(8,0)
94+ 0.0
95+ >>> spring_potential_energy(14.6,8)
96+ 467.2
97+ >>> spring_potential_energy(17,4.5)
98+ 172.125
6999 """
70- if spring_constant < 0 :
100+ if spr_con < 0 :
71101 raise ValueError ("The values for spring_constant cannot be negative" )
72102
73- return 0.5 * spring_constant * (displacement ** 2 )
103+ return 0.5 * spr_con * (dspl ** 2 )
74104
75105
76106
77107if __name__ == "__main__" :
78108 from doctest import testmod
79-
109+ print ( spring_potential_energy ( 17 , 4.5 ))
80110 testmod (name = "gravitational_potential_energy" )
0 commit comments