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 math_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
def add(a, b):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Logic duplication: add, subtract, multiply, and divide are already implemented in calculator.py with identical logic. A future change to division behavior (e.g. returning None instead of raising, or adding float-rounding rules) would require updating both files. Either remove these four functions and import from calculator.py, or consolidate both modules into one.

  • 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.

Guideline violation: The project requires every Python function to have a docstring explaining its purpose, parameters, and return value. None of the functions in this file have docstrings — this applies to all 11 functions including the two added in this commit.

  • Mark as noise

return a + b


def subtract(a, b):
return a - b


def multiply(a, b):
return a * b


def divide(a, b):
if b == 0:
raise ValueError("Cannot divide by zero")

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: No test covers the b == 0 path. A unit test calling divide(1, 0) and asserting a ValueError is raised would catch any regression to this guard.

  • Mark as noise

return a / b


def power(base, exponent):
return base ** exponent


def is_even(n):
return n % 2 == 0


def factorial(n):
if n < 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.

Missing test coverage: Three scenarios lack tests:

  • factorial(-1) — should raise ValueError
  • factorial(0) — base case returning 1
  • factorial with a large value (e.g. factorial(1000)) — would have caught the recursion depth bug above

Unit tests for each of these would protect against regressions.

  • Mark as noise

raise ValueError("Factorial not defined for negative numbers")
if n == 0:
return 1
return n * factorial(n - 1)

Comment on lines +28 to +33

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: This recursive implementation will raise RecursionError for n ≥ ~995 (Python's default recursion limit is 1000, and the call stack is already non-zero when this is called). Use an iterative approach instead.

Suggested change
if n < 0:
raise ValueError("Factorial not defined for negative numbers")
if n == 0:
return 1
return n * factorial(n - 1)
def factorial(n):
if n < 0:
raise ValueError("Factorial not defined for negative numbers")
result = 1
for i in range(2, n + 1):
result *= i
return result
  • Mark as noise


def clamp(value, min_val, max_val):
return max(min_val, min(max_val, value))

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: When min_val > max_val (an inverted range), this silently returns min_val for any input rather than raising an error or documenting the behavior.

Trace through clamp(5, 10, 3) (value=5, min=10, max=3):

  • min(3, 5)3
  • max(10, 3)10

The function returns 10 regardless of value. Callers passing an inverted range will get a wrong result with no indication something is off. Add a guard:

Suggested change
return max(min_val, min(max_val, value))
def clamp(value, min_val, max_val):
if min_val > max_val:
raise ValueError("min_val must not be greater than max_val")
return max(min_val, min(max_val, value))
  • Mark as noise



def average(numbers):
if not numbers:

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: No test covers the numbers empty path. A unit test calling average([]) and asserting a ValueError is raised would catch any regression to this guard.

  • Mark as noise

raise ValueError("Cannot compute average of empty list")
return sum(numbers) / len(numbers)
# Trigger review

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: No test covers the total == 0 path. A unit test calling percentage(50, 0) and asserting a ValueError is raised would catch any regression to this guard.

  • Mark as noise

def percentage(value, total):
if total == 0:
raise ValueError("total cannot be zero")
return (value / total) * 100

def is_prime(n):
if n < 2:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
Loading