Skip to content

Conversation

@BrianLusina
Copy link
Owner

@BrianLusina BrianLusina commented Dec 14, 2025

Describe your change:

Rectangle area

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

Summary by CodeRabbit

  • New Features

    • Rectangle Area: compute total area covered by two axis-aligned rectangles, accounting for overlap.
    • Max Consecutive Ones: compute the maximum number of consecutive 1s in a binary array.
  • Documentation

    • Added READMEs detailing inputs, constraints and example cases for both tasks.
  • Tests

    • Added and parameterized unit tests covering multiple scenarios for both features.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 14, 2025

Walkthrough

Adds a new Rectangle Area feature under pymath (implementation, README, tests) and introduces a max-consecutive-ones function with README updates and parameterized tests for the puzzles/arrays collection.

Changes

Cohort / File(s) Summary
Index / Directory
DIRECTORY.md
Added a "Rectangle Area" entry under Pymath with a Test Compute Area link.
Rectangle Area — Docs
pymath/rectangle_area/README.md
New README specifying two axis-aligned rectangles by bottom-left/top-right coordinates, naming conventions, constraints, and four example illustrations.
Rectangle Area — Implementation
pymath/rectangle_area/__init__.py
New compute_area(ax1: int, ay1: int, ax2: int, ay2: int, bx1: int, by1: int, bx2: int, by2: int) -> int computing combined area via inclusion–exclusion with clamped overlap dimensions.
Rectangle Area — Tests
pymath/rectangle_area/test_compute_area.py
New parameterized unit tests (5 cases) asserting expected total area across overlap scenarios.
Max Consecutive Ones — Docs
puzzles/arrays/max_consecutive_ones/README.md
Added problem description and constraints for the max consecutive ones variant.
Max Consecutive Ones — Implementation
puzzles/arrays/max_consecutive_ones/__init__.py
New find_max_consecutive_ones(nums: List[int]) -> int that returns the maximum run of consecutive 1s using a single-pass O(1) extra-space algorithm.
Max Consecutive Ones — Tests
puzzles/arrays/max_consecutive_ones/test_max_consecutive_ones.py
Refactored tests to use parameterized cases for both longest-ones behavior and find_max_consecutive_ones, replacing ad-hoc single-case tests.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20–30 minutes

  • Review overlap computation in pymath/rectangle_area/__init__.py (intersection bounds, non-negative clamping).
  • Verify test coverage and edge cases in pymath/rectangle_area/test_compute_area.py (no overlap, partial, full containment, identical).
  • Confirm correctness and edge handling (empty list, all zeros/ones) in puzzles/arrays/max_consecutive_ones/__init__.py.
  • Check test refactor in puzzles/arrays/max_consecutive_ones/test_max_consecutive_ones.py for correct parameterization and imports.

Suggested labels

enhancement, Algorithm, Documentation

Poem

I nibble numbers, hop and chart,
Two rectangles meet — I play my part.
I find the overlap, then sum with glee,
A rabbit’s math, precise and free. 🐰✨

Pre-merge checks and finishing touches

❌ Failed checks (3 warnings)
Check name Status Explanation Resolution
Title check ⚠️ Warning The title mentions two features (rectangle area and max consecutive ones) but the PR description only covers rectangle area, and the checklist conflicts indicate incomplete scope. Align the title with the actual changes or split into separate PRs for each algorithm as per the checklist requirement to change only one algorithm per PR.
Description check ⚠️ Warning The PR description is incomplete: it only states 'Rectangle area' without explaining the actual changes, and multiple conflicting checklist items are checked. Provide a detailed description of all changes made, specify which new functions/files were added, and ensure all checklist items accurately reflect the actual changes in this PR.
Docstring Coverage ⚠️ Warning Docstring coverage is 16.67% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/rectangle-aread

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

Copy link
Contributor

@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: 3

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6b8bcc5 and d3045ee.

📒 Files selected for processing (4)
  • DIRECTORY.md (1 hunks)
  • pymath/rectangle_area/README.md (1 hunks)
  • pymath/rectangle_area/__init__.py (1 hunks)
  • pymath/rectangle_area/test_compute_area.py (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
pymath/rectangle_area/test_compute_area.py (1)
pymath/rectangle_area/__init__.py (1)
  • compute_area (1-33)
🪛 markdownlint-cli2 (0.18.1)
DIRECTORY.md

707-707: Unordered list indentation
Expected: 0; Actual: 2

(MD007, ul-indent)


708-708: Unordered list indentation
Expected: 2; Actual: 4

(MD007, ul-indent)

🔇 Additional comments (1)
pymath/rectangle_area/test_compute_area.py (1)

1-33: Add type hint expected: int for parameter consistency.

The parameterized dependency is already declared in pyproject.toml (version ^0.9.0), so no verification is needed. However, add the type hint to match the pattern used in similar test files across the codebase:

-        expected,
+        expected: int,

Comment on lines +1 to +33
def compute_area(
ax1: int, ay1: int, ax2: int, ay2: int, bx1: int, by1: int, bx2: int, by2: int
):
# 1. Calculate the area of each individual rectangle
area_a = (ax2 - ax1) * (ay2 - ay1)
area_b = (bx2 - bx1) * (by2 - by1)

# 2. Calculate the area of the overlap (A intersect B)

# Determine the coordinates of the overlap rectangle: (ix1, iy1) to (ix2, iy2)

# The left edge of the overlap is the max of the two left edges
ix1 = max(ax1, bx1)
# The right edge of the overlap is the min of the two right edges
ix2 = min(ax2, bx2)

# The bottom edge of the overlap is the max of the two bottom edges
iy1 = max(ay1, by1)
# The top edge of the overlap is the min of the two top edges
iy2 = min(ay2, by2)

# Calculate the width and height of the overlap
overlap_width = max(0, ix2 - ix1)
overlap_height = max(0, iy2 - iy1)

# The max(0, ...) ensures that if the rectangles do not overlap
# (e.g., ix2 < ix1), the width/height is 0, and the overlap_area is 0.
overlap_area = overlap_width * overlap_height

# 3. Apply the Inclusion-Exclusion Principle
total_area = area_a + area_b - overlap_area

return total_area
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Add return type + docstring/doctest + reference URL (per repo checklist).

compute_area is missing a return type annotation and (per the PR checklist you referenced) a doctest + a URL comment pointing to Wikipedia/similar.

-def compute_area(
+def compute_area(
     ax1: int, ay1: int, ax2: int, ay2: int, bx1: int, by1: int, bx2: int, by2: int
-):
+) -> int:
+    """
+    Compute total area covered by two axis-aligned rectangles.
+
+    Reference: https://en.wikipedia.org/wiki/Inclusion%E2%80%93exclusion_principle
+
+    >>> compute_area(0, 0, 2, 2, 1, 1, 3, 3)
+    7
+    """

(Optional hardening): consider guarding against reversed coordinates (e.g., ax2 < ax1) to avoid negative areas if callers violate constraints.

Comment on lines +3 to +8
You are given the coordinates of two axis-aligned rectangles in a 2D plane. Your task is to calculate the total area
covered by both rectangles.

The first rectangle is specified by the coordinates of its bottom-left corner (ax1, ay1) and top-right corner (ay1, ay2).

Similarly, the second rectangle is defined by its bottom-left corner (bx1, by1) and top-right corner (bx2, by2).
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Fix coordinate typo in rectangle definition (ax2 vs ay1).

Line 6 currently says top-right is (ay1, ay2); that should be (ax2, ay2).

-The first rectangle is specified by the coordinates of its bottom-left corner (ax1, ay1) and top-right corner (ay1, ay2).
+The first rectangle is specified by the coordinates of its bottom-left corner (ax1, ay1) and top-right corner (ax2, ay2).

Also applies to: 14-17

🤖 Prompt for AI Agents
In pymath/rectangle_area/README.md around lines 3-8 and again around lines
14-17, the rectangle coordinate descriptions misuse ax2 as ay1: change the
top-right coordinate of the first rectangle from (ay1, ay2) to (ax2, ay2) in
both occurrences so the first rectangle is defined by bottom-left (ax1, ay1) and
top-right (ax2, ay2).

@BrianLusina BrianLusina changed the title Feat/rectangle aread feat(puzzles, math): rectangle area and max consecutive ones Dec 15, 2025
@BrianLusina BrianLusina added enhancement Algorithm Algorithm Problem Array Array data structure labels Dec 15, 2025
Copy link
Contributor

@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: 1

🧹 Nitpick comments (2)
puzzles/arrays/max_consecutive_ones/__init__.py (1)

44-45: Consider more Pythonic empty check.

The empty check is correct but could be more idiomatic.

-    if len(nums) == 0:
+    if not nums:
         return 0
puzzles/arrays/max_consecutive_ones/test_max_consecutive_ones.py (1)

17-27: Consider adding edge case tests.

The parameterized tests provide good coverage for typical scenarios. Consider adding a few edge cases to make the test suite more comprehensive:

     @parameterized.expand([
         ([1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0], 4),
         ([0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1], 4),
         ([1,1,0,0,1,1,1,0,0,1,0], 3),
         ([1,1,0,0,1,1], 2),
         ([1,1,1,0,1,1,1,1], 4),
         ([0,0,0,0], 0),
+        ([], 0),  # empty array
+        ([1,1,1,1], 4),  # all ones
+        ([0], 0),  # single zero
+        ([1], 1),  # single one
     ])
     def test_find_max_consecutive_ones(self, nums: List[int], expected: int):
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d3045ee and bc0b23d.

📒 Files selected for processing (3)
  • puzzles/arrays/max_consecutive_ones/README.md (1 hunks)
  • puzzles/arrays/max_consecutive_ones/__init__.py (1 hunks)
  • puzzles/arrays/max_consecutive_ones/test_max_consecutive_ones.py (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • puzzles/arrays/max_consecutive_ones/README.md
🧰 Additional context used
🧬 Code graph analysis (1)
puzzles/arrays/max_consecutive_ones/test_max_consecutive_ones.py (1)
puzzles/arrays/max_consecutive_ones/__init__.py (2)
  • longest_ones (4-22)
  • find_max_consecutive_ones (24-55)
🔇 Additional comments (3)
puzzles/arrays/max_consecutive_ones/__init__.py (1)

46-55: LGTM!

The algorithm implementation is correct and efficient. The logic properly maintains the current consecutive count and maximum count with a single pass through the array, achieving the documented O(n) time and O(1) space complexity.

puzzles/arrays/max_consecutive_ones/test_max_consecutive_ones.py (2)

2-4: LGTM!

The import additions are appropriate for the parameterized test refactoring and type annotations.


9-15: LGTM!

The parameterized test structure properly exercises the longest_ones function with multiple scenarios, improving test coverage.

Comment on lines +24 to +43
def find_max_consecutive_ones(nums: List[int]) -> int:
"""
Finds the maximum consecutive ones in a binary array and returns it.
The most straightforward way to solve this is to use a single pass through the array, keeping track of two values
as we go. First, we need a counter for the current streak of consecutive 1s we're seeing. Second, we need to
remember the maximum streak we've encountered so far.
As we examine each element, if we see a 1, we increment our current streak counter. If we see a 0, that breaks our
streak, so we reset the counter to 0. Importantly, every time we update our current streak, we check whether it's
larger than our maximum and update the maximum if needed.
Time complexity is O(n) where n is the length of the input array
Space complexity is O(1) as no extra space is required
Args:
nums(list): a list of 1s and 0s.
Returns:
int: maximum number of consecutive 1s in the nums binary array
"""
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Add reference URL as per PR checklist.

The PR checklist confirms "All new algorithms include a URL in comments pointing to Wikipedia or similar," but no reference URL is present for this algorithm. Consider adding a comment with a link to the problem description or relevant documentation.

For example:

+# Reference: https://leetcode.com/problems/max-consecutive-ones/
+# or https://en.wikipedia.org/wiki/Longest_run
 def find_max_consecutive_ones(nums: List[int]) -> int:
     """
     Finds the maximum consecutive ones in a binary array and returns it.
📝 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 find_max_consecutive_ones(nums: List[int]) -> int:
"""
Finds the maximum consecutive ones in a binary array and returns it.
The most straightforward way to solve this is to use a single pass through the array, keeping track of two values
as we go. First, we need a counter for the current streak of consecutive 1s we're seeing. Second, we need to
remember the maximum streak we've encountered so far.
As we examine each element, if we see a 1, we increment our current streak counter. If we see a 0, that breaks our
streak, so we reset the counter to 0. Importantly, every time we update our current streak, we check whether it's
larger than our maximum and update the maximum if needed.
Time complexity is O(n) where n is the length of the input array
Space complexity is O(1) as no extra space is required
Args:
nums(list): a list of 1s and 0s.
Returns:
int: maximum number of consecutive 1s in the nums binary array
"""
# Reference: https://leetcode.com/problems/max-consecutive-ones/
# or https://en.wikipedia.org/wiki/Longest_run
def find_max_consecutive_ones(nums: List[int]) -> int:
"""
Finds the maximum consecutive ones in a binary array and returns it.
The most straightforward way to solve this is to use a single pass through the array, keeping track of two values
as we go. First, we need a counter for the current streak of consecutive 1s we're seeing. Second, we need to
remember the maximum streak we've encountered so far.
As we examine each element, if we see a 1, we increment our current streak counter. If we see a 0, that breaks our
streak, so we reset the counter to 0. Importantly, every time we update our current streak, we check whether it's
larger than our maximum and update the maximum if needed.
Time complexity is O(n) where n is the length of the input array
Space complexity is O(1) as no extra space is required
Args:
nums(list): a list of 1s and 0s.
Returns:
int: maximum number of consecutive 1s in the nums binary array
"""
🤖 Prompt for AI Agents
puzzles/arrays/max_consecutive_ones/__init__.py lines 24-43: the function
docstring is missing the required reference URL per the PR checklist; add a
short comment or line in the docstring pointing to an authoritative source
(e.g., LeetCode problem page or Wikipedia) for the "Max Consecutive Ones"
problem, placing the URL near the top of the docstring or immediately above the
function definition so it’s clearly associated with the algorithm.

@BrianLusina BrianLusina merged commit ec7bf4d into main Dec 15, 2025
6 of 8 checks passed
@BrianLusina BrianLusina deleted the feat/rectangle-aread branch December 15, 2025 07:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Algorithm Algorithm Problem Array Array data structure enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants