Skip to content

Add utility functions#4

Open
Whamdrew wants to merge 2 commits intomainfrom
feature-stats
Open

Add utility functions#4
Whamdrew wants to merge 2 commits intomainfrom
feature-stats

Conversation

@Whamdrew
Copy link
Copy Markdown
Owner

@Whamdrew Whamdrew commented Apr 27, 2026

Summary by CodeRabbit

  • New Features
    • Added statistical helper utilities for mathematical calculations: average, maximum, minimum, and range. These helpers make it easier to compute common summary statistics when working with numeric collections.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 27, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: cc376e2f-7a7f-45f0-aecc-5128262e7e6c

📥 Commits

Reviewing files that changed from the base of the PR and between a7bfbae and 9eafedd.

📒 Files selected for processing (1)
  • python/math_utils.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • python/math_utils.py

📝 Walkthrough

Walkthrough

Updated python/math_utils.py to add four statistical helper functions—mean, maximum, minimum, and range_of—and to update the module description. Existing math functions remain unchanged; no input validation was added.

Changes

Cohort / File(s) Summary
Math utilities
python/math_utils.py
Added mean(numbers), maximum(numbers), minimum(numbers), range_of(numbers); updated module-level description; existing functions (add, multiply, is_even, factorial) unchanged; no input validation; newline added at EOF.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 I hopped to the code, gave it a tweak,
Four bright helpers now hide in the creek.
Mean, min, max, and a range to adore,
Small tidy functions—oh, hop for more! 🥕

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Title check ❓ Inconclusive The title 'Add utility functions' is vague and generic, failing to specify which utility functions were added or their purpose. Use a more descriptive title such as 'Add statistical utility functions (mean, maximum, minimum, range)' to clarify the specific changes.
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature-stats

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 4

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@python/math_utils.py`:
- Line 5: The maximum(numbers) function currently uses a one-liner that calls
max(numbers) and will raise ValueError on an empty list; rewrite maximum as a
multi-line function consistent with mean() and range_of(), validate the input
(e.g., if not numbers or len(numbers) == 0) and return an appropriate value or
raise a clearer exception with message, then otherwise compute and return the
maximum using max(numbers); ensure the function signature remains
maximum(numbers) and update any docstring or tests if needed.
- Around line 2-3: The mean(numbers) function currently divides by len(numbers)
and will raise ZeroDivisionError for an empty list; update mean to validate the
input first (e.g., if not numbers: raise ValueError("mean() requires at least
one data point") or return None depending on project convention) or replace its
implementation by delegating to statistics.mean(numbers) to get standard error
handling and edge-case behavior; modify the function named mean in
python/math_utils.py accordingly and ensure any callers handle the new behavior.
- Line 7: The one-line minimum(numbers) should be rewritten in multi-line form
consistent with mean() and range_of(): validate the input to handle an empty
sequence (e.g., if not numbers: return None or a sensible default) to avoid
ValueError from min(), then compute and return min(numbers); update the function
name minimum to include that guard and keep behavior consistent with mean() and
range_of().
- Around line 9-10: The range_of function currently calls max() and min()
separately and will raise on an empty input; update range_of to first validate
that numbers is non-empty and raise a clearer ValueError (or return a defined
value) when empty, then compute both min and max in a single pass (iterate once
over numbers, tracking current_min and current_max) to return current_max -
current_min; reference the range_of function to locate and replace the existing
implementation.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 94916013-8e1a-4063-86af-09c8f49907b6

📥 Commits

Reviewing files that changed from the base of the PR and between 7d685fd and a7bfbae.

📒 Files selected for processing (1)
  • python/math_utils.py

Comment thread python/math_utils.py
Comment on lines +2 to +3
def mean(numbers):
return sum(numbers) / len(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.

⚠️ Potential issue | 🔴 Critical

Handle empty list to prevent ZeroDivisionError.

The function will raise ZeroDivisionError when called with an empty list. Add validation or consider using statistics.mean() from the standard library, which provides better error messages and handles edge cases.

🛡️ Proposed fix with input validation
 def mean(numbers):
+    if not numbers:
+        raise ValueError("Cannot calculate mean of empty list")
     return sum(numbers) / len(numbers)

Alternatively, prefer the standard library implementation:

♻️ Alternative: Use statistics.mean
+from statistics import mean
+
-def mean(numbers):
-    return sum(numbers) / len(numbers)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
def mean(numbers):
return sum(numbers) / len(numbers)
def mean(numbers):
if not numbers:
raise ValueError("Cannot calculate mean of empty list")
return sum(numbers) / len(numbers)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@python/math_utils.py` around lines 2 - 3, The mean(numbers) function
currently divides by len(numbers) and will raise ZeroDivisionError for an empty
list; update mean to validate the input first (e.g., if not numbers: raise
ValueError("mean() requires at least one data point") or return None depending
on project convention) or replace its implementation by delegating to
statistics.mean(numbers) to get standard error handling and edge-case behavior;
modify the function named mean in python/math_utils.py accordingly and ensure
any callers handle the new behavior.

Comment thread python/math_utils.py
def mean(numbers):
return sum(numbers) / len(numbers)

def maximum(numbers): return max(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.

⚠️ Potential issue | 🔴 Critical

Handle empty list to prevent ValueError.

The function will raise ValueError: max() arg is an empty sequence when called with an empty list. Add validation to handle this edge case.

🛡️ Proposed fix with input validation
-def maximum(numbers): return max(numbers)
+def maximum(numbers):
+    if not numbers:
+        raise ValueError("Cannot find maximum of empty list")
+    return max(numbers)

The one-liner style is inconsistent with mean() and range_of(). Consider using multi-line format for consistency.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
def maximum(numbers): return max(numbers)
def maximum(numbers):
if not numbers:
raise ValueError("Cannot find maximum of empty list")
return max(numbers)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@python/math_utils.py` at line 5, The maximum(numbers) function currently uses
a one-liner that calls max(numbers) and will raise ValueError on an empty list;
rewrite maximum as a multi-line function consistent with mean() and range_of(),
validate the input (e.g., if not numbers or len(numbers) == 0) and return an
appropriate value or raise a clearer exception with message, then otherwise
compute and return the maximum using max(numbers); ensure the function signature
remains maximum(numbers) and update any docstring or tests if needed.

Comment thread python/math_utils.py

def maximum(numbers): return max(numbers)

def minimum(numbers): return min(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.

⚠️ Potential issue | 🔴 Critical

Handle empty list to prevent ValueError.

The function will raise ValueError: min() arg is an empty sequence when called with an empty list. Add validation to handle this edge case.

🛡️ Proposed fix with input validation
-def minimum(numbers): return min(numbers)
+def minimum(numbers):
+    if not numbers:
+        raise ValueError("Cannot find minimum of empty list")
+    return min(numbers)

The one-liner style is inconsistent with mean() and range_of(). Consider using multi-line format for consistency.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@python/math_utils.py` at line 7, The one-line minimum(numbers) should be
rewritten in multi-line form consistent with mean() and range_of(): validate the
input to handle an empty sequence (e.g., if not numbers: return None or a
sensible default) to avoid ValueError from min(), then compute and return
min(numbers); update the function name minimum to include that guard and keep
behavior consistent with mean() and range_of().

Comment thread python/math_utils.py Outdated
Comment on lines +9 to +10
def range_of(numbers):
return max(numbers) - min(numbers) No newline at end of file
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

Handle empty list and improve efficiency.

The function will raise ValueError when called with an empty list. Additionally, calling max() and min() separately traverses the list twice, which is inefficient for large datasets.

🛡️ Proposed fix with validation and efficiency improvement
 def range_of(numbers):
+    if not numbers:
+        raise ValueError("Cannot calculate range of empty list")
-    return max(numbers) - min(numbers)
+    min_val = min(numbers)
+    max_val = max(numbers)
+    return max_val - min_val

For better efficiency with large lists, compute min and max in a single pass:

♻️ Alternative: Single-pass calculation
 def range_of(numbers):
+    if not numbers:
+        raise ValueError("Cannot calculate range of empty list")
-    return max(numbers) - min(numbers)
+    min_val = float('inf')
+    max_val = float('-inf')
+    for num in numbers:
+        if num < min_val:
+            min_val = num
+        if num > max_val:
+            max_val = num
+    return max_val - min_val
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
def range_of(numbers):
return max(numbers) - min(numbers)
def range_of(numbers):
if not numbers:
raise ValueError("Cannot calculate range of empty list")
min_val = float('inf')
max_val = float('-inf')
for num in numbers:
if num < min_val:
min_val = num
if num > max_val:
max_val = num
return max_val - min_val
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@python/math_utils.py` around lines 9 - 10, The range_of function currently
calls max() and min() separately and will raise on an empty input; update
range_of to first validate that numbers is non-empty and raise a clearer
ValueError (or return a defined value) when empty, then compute both min and max
in a single pass (iterate once over numbers, tracking current_min and
current_max) to return current_max - current_min; reference the range_of
function to locate and replace the existing implementation.

@Whamdrew
Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 27, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant