|
1 | 1 | """ |
2 | | -Find the kinetic energy of an object, given its mass and velocity. |
3 | | -
|
4 | | -Description : In physics, the kinetic energy of an object is the energy that it |
5 | | -possesses due to its motion.It is defined as the work needed to accelerate a body of a |
6 | | -given mass from rest to its stated velocity.Having gained this energy during its |
7 | | -acceleration, the body maintains this kinetic energy unless its speed changes.The same |
8 | | -amount of work is done by the body when decelerating from its current speed to a state |
9 | | -of rest.Formally, a kinetic energy is any term in a system's Lagrangian which includes |
10 | | -a derivative with respect to time. |
11 | | -
|
12 | | -In classical mechanics, the kinetic energy of a non-rotating object of mass m traveling |
13 | | -at a speed v is ½mv².In relativistic mechanics, this is a good approximation only when |
14 | | -v is much less than the speed of light.The standard unit of kinetic energy is the |
15 | | -joule, while the English unit of kinetic energy is the foot-pound. |
16 | | -
|
17 | | -Reference : https://en.m.wikipedia.org/wiki/Kinetic_energy |
| 2 | +Compute the kinetic energy of an object given its mass and velocity. |
| 3 | +
|
| 4 | +Formula (classical mechanics): |
| 5 | + KE = ½ · m · v² |
| 6 | +
|
| 7 | +Reference: |
| 8 | +https://en.wikipedia.org/wiki/Kinetic_energy |
18 | 9 | """ |
19 | 10 |
|
20 | 11 |
|
21 | 12 | def kinetic_energy(mass: float, velocity: float) -> float: |
22 | 13 | """ |
23 | | - Calculate kinetic energy. |
24 | | -
|
25 | | - The kinetic energy of a non-rotating object of mass m traveling at a speed v is ½mv² |
26 | | -
|
27 | | - >>> kinetic_energy(10,10) |
| 14 | + Calculate the kinetic energy of a non-rotating object. |
| 15 | +
|
| 16 | + Parameters |
| 17 | + ---------- |
| 18 | + mass : float |
| 19 | + Mass of the object (must be non-negative) |
| 20 | + velocity : float |
| 21 | + Velocity of the object |
| 22 | +
|
| 23 | + Returns |
| 24 | + ------- |
| 25 | + float |
| 26 | + Kinetic energy in joules |
| 27 | +
|
| 28 | + Raises |
| 29 | + ------ |
| 30 | + ValueError |
| 31 | + If mass is negative |
| 32 | +
|
| 33 | + Examples |
| 34 | + -------- |
| 35 | + >>> kinetic_energy(10, 10) |
28 | 36 | 500.0 |
29 | | - >>> kinetic_energy(0,10) |
| 37 | + >>> kinetic_energy(0, 10) |
30 | 38 | 0.0 |
31 | | - >>> kinetic_energy(10,0) |
| 39 | + >>> kinetic_energy(10, 0) |
32 | 40 | 0.0 |
33 | | - >>> kinetic_energy(20,-20) |
| 41 | + >>> kinetic_energy(20, -20) |
34 | 42 | 4000.0 |
35 | | - >>> kinetic_energy(0,0) |
| 43 | + >>> kinetic_energy(0, 0) |
36 | 44 | 0.0 |
37 | | - >>> kinetic_energy(2,2) |
| 45 | + >>> kinetic_energy(2, 2) |
38 | 46 | 4.0 |
39 | | - >>> kinetic_energy(100,100) |
| 47 | + >>> kinetic_energy(100, 100) |
40 | 48 | 500000.0 |
41 | 49 | """ |
42 | 50 | if mass < 0: |
43 | | - raise ValueError("The mass of a body cannot be negative") |
44 | | - return 0.5 * mass * abs(velocity) * abs(velocity) |
| 51 | + raise ValueError("Mass must be non-negative") |
| 52 | + |
| 53 | + return 0.5 * mass * velocity ** 2 |
45 | 54 |
|
46 | 55 |
|
47 | 56 | if __name__ == "__main__": |
|
0 commit comments