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
36 changes: 36 additions & 0 deletions math_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""Math utility functions."""
import math


def fibonacci(n: int) -> list[int]:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Missing docstring: All public functions must have a docstring describing what the function does, its parameters, and its return value (org guideline).

Suggested change
def fibonacci(n: int) -> list[int]:
def fibonacci(n: int) -> list[int]:
"""Return the first n numbers in the Fibonacci sequence.
Args:
n: How many numbers to generate. Returns [] for n <= 0.
Returns:
A list of n Fibonacci numbers starting from 0.
"""
  • Mark as noise

if n <= 0:
return []
if n == 1:
return [0]
seq = [0, 1]
for _ in range(2, n):
seq.append(seq[-1] + seq[-2])
return seq


def is_prime(n: int) -> bool:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Missing docstring: Public function lacks a docstring (org guideline).

Suggested change
def is_prime(n: int) -> bool:
def is_prime(n: int) -> bool:
"""Return True if n is a prime number, False otherwise.
Args:
n: The integer to test. Values less than 2 return False.
Returns:
True if n is prime, False otherwise.
"""
  • Mark as noise

if n < 2:
return False
for i in range(2, int(math.sqrt(n)) + 1):
if n % i == 0:
return False
return True


Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Missing docstring: Public function lacks a docstring (org guideline).

Suggested change
def gcd(a: int, b: int) -> int:
"""Return the greatest common divisor of a and b using the Euclidean algorithm.
Args:
a: First integer.
b: Second integer.
Returns:
The GCD of a and b.
"""
  • Mark as noise

def gcd(a: int, b: int) -> int:
while b:
a, b = b, a % b
return a

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Missing docstring: Public function lacks a docstring (org guideline).

Suggested change
return a
def lcm(a: int, b: int) -> int:
"""Return the least common multiple of a and b.
Args:
a: First integer.
b: Second integer.
Returns:
The LCM of a and b. Returns 0 if either argument is 0.
"""
  • Mark as noise


Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bug: lcm(0, 0) raises ZeroDivisionError. gcd(0, 0) returns 0 (the loop exits immediately because b is falsy), making the division crash.

Also, even for non-zero inputs, multiplying first (a * b) and dividing after is safe in Python due to arbitrary-precision integers, but the conventional approach divides first to keep intermediate values smaller.

Suggested change
if a == 0 or b == 0:
return 0
return abs(a) // gcd(a, b) * abs(b)
  • Mark as noise

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Missing test coverage: The lcm function has a division-by-zero bug when both inputs are 0 (see the bug comment above). A unit test for lcm(0, 0), lcm(0, 5), and lcm(5, 0) would catch this class of regression.

  • Mark as noise


def lcm(a: int, b: int) -> int:
return abs(a * b) // gcd(a, b)


def clamp(value: float, min_val: float, max_val: float) -> float:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Missing docstring: Public function lacks a docstring (org guideline).

Suggested change
def clamp(value: float, min_val: float, max_val: float) -> float:
def clamp(value: float, min_val: float, max_val: float) -> float:
"""Clamp value to the range [min_val, max_val].
Args:
value: The value to clamp.
min_val: Lower bound (inclusive).
max_val: Upper bound (inclusive).
Returns:
value constrained to [min_val, max_val].
"""
  • Mark as noise

return max(min_val, min(value, max_val))
Loading