Skip to content

Commit eb4c3e3

Browse files
committed
Potential Energy, and Time complexity added
1 parent facc61f commit eb4c3e3

2 files changed

Lines changed: 69 additions & 27 deletions

File tree

physics/collisions.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ def inelastic_collisions(
3131
Returns:
3232
The final combined velocity of the two objects.
3333
34+
Complexity:
35+
Time Complexity: O(1) - Constant execution time for basic arithmetic.
36+
Space Complexity: O(1) - Constant memory allocation.
37+
3438
Examples:
3539
>>> inelastic_collisions(2.0, 3.0, 5.0, 6.0)
3640
5.6
@@ -60,6 +64,10 @@ def elastic_collisions(
6064
Returns:
6165
A formatted string containing the final velocities of both objects.
6266
67+
Complexity:
68+
Time Complexity: O(1) - Constant execution time for basic math loops.
69+
Space Complexity: O(1) - Constant memory allocation for fixed-size lists.
70+
6371
Examples:
6472
>>> elastic_collisions(1.0, 2.0, -3.0, -1.0)
6573
'-0.34 ; -2.34'
@@ -101,6 +109,10 @@ def type_collision(
101109
Returns:
102110
A string describing the collision type.
103111
112+
Complexity:
113+
Time Complexity: O(1) - Constant execution time for evaluation logic.
114+
Space Complexity: O(1) - Constant memory allocation.
115+
104116
Examples:
105117
>>> type_collision(1.0, 1.0, 2.0, 3.0, 2.0, 3.0)
106118
'Perfectly Elastic Collision'
@@ -127,4 +139,4 @@ def type_collision(
127139
if __name__ == "__main__":
128140
import doctest
129141

130-
doctest.testmod()
142+
doctest.testmod()

physics/potential_energy.py

Lines changed: 56 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,35 @@
3131

3232

3333
def 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

77107
if __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

Comments
 (0)