Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions physics/orbital_period.py
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, this is a well-structured implementation with clear validation and useful doctests. A few improvements around readability, constants, and style (imports placement, expression clarity) would make it production-quality.

Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""
The time period of a satellite is the time taken by a satellite to
complete one full orbit around a celestial body.

T = 2π * sqrt((R + h)^3 / (G * M))
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider formatting the formula using consistent mathematical notation (e.g., with proper spacing or LaTeX-style formatting) to improve readability, especially since this is a physics-related implementation.


G is the Universal Gravitational Constant and equals 6.674 X 10^-11 N m²/kg²
M is the mass of the celestial body (kg)
R is the radius of the celestial body (m)
h is the height of the satellite above the surface (m)

Reference: https://en.wikipedia.org/wiki/Orbital_period
"""


def time_period_of_satellite(mass: float, radius: float, height: float) -> float:
"""
Calculate the time period of a satellite orbiting a celestial body

>>> time_period_of_satellite(5.972e24, 6.371e6, 3.5e5)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice use of realistic Earth values 👍 Consider explicitly mentioning units (kg, meters) in the docstring examples to avoid ambiguity for users.

5562.65
>>> time_period_of_satellite(5.972e24, 6.371e6, 0)
5060.84
>>> time_period_of_satellite(7.342e22, 1.737e6, 1.0e5)
7045.95
>>> time_period_of_satellite(6.39e23, 3.389e6, 3.0e5)
7113.64
>>> time_period_of_satellite(-5.972e24, 6.371e6, 3.5e5)
Traceback (most recent call last):
...
ValueError: Mass must be a positive value
>>> time_period_of_satellite(5.972e24, -6.371e6, 3.5e5)
Traceback (most recent call last):
...
ValueError: Radius must be a positive value
>>> time_period_of_satellite(5.972e24, 6.371e6, -3.5e5)
Traceback (most recent call last):
...
ValueError: Height must be a non-negative value
"""
if mass <= 0:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good validation checks 👍 Consider grouping these validations into a single block or helper function for better readability and maintainability.

raise ValueError("Mass must be a positive value")
if radius <= 0:
raise ValueError("Radius must be a positive value")
if height < 0:
raise ValueError("Height must be a non-negative value")

import math
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imports should be placed at the top of the file according to Python style guidelines (PEP8). Consider moving this import to the top.


return round(2 * math.pi * ((radius + height) ** 3 / (6.674e-11 * mass)) ** 0.5, 2)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This expression is correct but a bit hard to read. Consider breaking it into intermediate variables (e.g., orbital_radius, gravitational_term) to improve clarity and maintainability.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rounding inside the function may limit flexibility. Consider returning the raw value and letting the caller decide the precision.



if __name__ == "__main__":
import doctest

doctest.testmod()
Loading