Skip to content

feat(algorithms, two-pointers): three number sum#188

Merged
BrianLusina merged 1 commit intomainfrom
feat/algorithms-three-number-sum
Mar 11, 2026
Merged

feat(algorithms, two-pointers): three number sum#188
BrianLusina merged 1 commit intomainfrom
feat/algorithms-three-number-sum

Conversation

@BrianLusina
Copy link
Owner

@BrianLusina BrianLusina commented Mar 11, 2026

Describe your change:

Three number sum algorithm

  • 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

Release Notes

  • New Features

    • Introduced a new algorithm for identifying all unique triplets that sum to a target value.
  • Documentation

    • Updated algorithm documentation with new guide sections and improved constraint formatting.
  • Tests

    • Expanded test coverage with cases for the new triplet-sum algorithm variant.

@BrianLusina BrianLusina self-assigned this Mar 11, 2026
@BrianLusina BrianLusina added enhancement Algorithm Algorithm Problem Documentation Documentation Updates Sorting Contains sorting in the algorithm Array Array data structure Two Pointers Two pointer algorithm Binary Search Binary Search Algorithm In Place Manipulation labels Mar 11, 2026
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 11, 2026

📝 Walkthrough

Walkthrough

This pull request adds a new three_number_sum function to the two-pointers algorithm module alongside documentation updates and code comments. The function finds all unique triplets in an array that sum to a specified target value, implemented using a three-pointer approach. Documentation is reformatted in README files and inline comments are added to clarify existing algorithm logic.

Changes

Cohort / File(s) Summary
Remove Duplicates Documentation
algorithms/arrays/remove_duplicates/README.md, algorithms/arrays/remove_duplicates/__init__.py
Constraint section reformatted in README and inline comments added to clarify two-pointer logic without altering functionality.
Three Sum Enhancement
algorithms/two_pointers/three_sum/README.md, algorithms/two_pointers/three_sum/__init__.py, algorithms/two_pointers/three_sum/test_three_sum.py
New three_number_sum(array, target_sum) function introduced that finds all unique triplets summing to a target value using three-pointer technique. README extended with problem description and hints. Test coverage added with test cases and assertions.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested labels

Datastructures

Poem

Three pointers dance in sorted grace,
Finding triplets, keeping pace,
Duplicates fade, targets align,
Sums converge on every line,
Comments shine to light the way,
Algorithms thrive and play! 🐰✨

🚥 Pre-merge checks | ✅ 1 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 40.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Description check ❓ Inconclusive The PR description follows the required template with all major sections completed, though it checks multiple checklist items that appear inconsistent with the actual changes made. Clarify why both 'Add an algorithm' and 'Fix a bug or typo in an existing algorithm' are marked; the changes appear to add new functionality, not fix bugs. Verify all checklist items accurately reflect the PR contents.
✅ Passed checks (1 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the main change: introducing a three number sum algorithm in the two-pointers algorithms module.

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

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/algorithms-three-number-sum

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.

🧹 Nitpick comments (3)
algorithms/two_pointers/three_sum/__init__.py (2)

54-56: Add a docstring for consistency with three_sum.

The existing three_sum function has a comprehensive docstring documenting complexity analysis and parameters. Adding similar documentation to three_number_sum would improve maintainability and API discoverability.

📝 Proposed docstring
 def three_number_sum(array: List[int], target_sum: int) -> List[List[int]]:
+    """
+    Finds all unique triplets in the array that sum up to the target sum.
+
+    Complexity Analysis:
+    Time Complexity: O(n²) where n is the length of the input array.
+    Space Complexity: O(n) for the sorted copy, plus O(n²) for storing results.
+
+    Args:
+        array (list): input list of distinct integers
+        target_sum (int): target sum for the triplets
+    Returns:
+        list: list of triplets, each ordered ascending, with triplets ordered ascending
+    """
     if not array:
         return []
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@algorithms/two_pointers/three_sum/__init__.py` around lines 54 - 56, Add a
comprehensive docstring to the three_number_sum function matching the style of
three_sum: describe the function purpose, parameters (array: List[int],
target_sum: int), return value (List[List[int]]), and include time/space
complexity notes and any ordering or uniqueness guarantees; update the top of
the three_number_sum function to include this docstring so it matches the
existing documentation format used by three_sum.

65-67: Consider adding duplicate skip for the outer loop for robustness.

The three_sum function (line 28) skips duplicate values in the outer loop to avoid duplicate triplets. Whilst the problem statement specifies "distinct integers", adding a similar check here would make the function more robust and consistent with three_sum.

♻️ Proposed fix to handle duplicate outer values
     for idx, num in enumerate(nums):
+        # Skip duplicate values to avoid duplicate triplets
+        if idx > 0 and num == nums[idx - 1]:
+            continue
+
         left = idx + 1
         right = n - 1
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@algorithms/two_pointers/three_sum/__init__.py` around lines 65 - 67, The
outer loop in the related function should skip duplicate starting values to
avoid producing duplicate triplets: inside the for idx, num in enumerate(nums)
loop in three_sum, add a guard like "if idx > 0 and nums[idx] == nums[idx - 1]:
continue" (use the same sorted nums input as the inner logic expects) so
repeated outer values are skipped; update the loop that defines left = idx + 1
and right = n - 1 to include this duplicate-skip check.
algorithms/two_pointers/three_sum/test_three_sum.py (1)

17-19: Consider adding more test cases for better coverage.

Currently there's only one test case for three_number_sum. Additional edge cases would strengthen confidence in the implementation:

  • Empty array: ([], 0, [])
  • Array with fewer than 3 elements: ([1, 2], 3, [])
  • No matching triplets: ([1, 2, 3, 4], 100, [])
  • Negative target sum: ([1, -2, 3, -4, 5], -3, [[-4, -2, 3]])
🧪 Proposed additional test cases
 THREE_NUMBER_SUM_TEST_CASES = [
     ([12, 3, 1, 2, -6, 5, -8, 6], 0, [[-8, 2, 6], [-8, 3, 5], [-6, 1, 5]]),
+    ([], 0, []),
+    ([1, 2], 3, []),
+    ([1, 2, 3, 4], 100, []),
+    ([1, -2, 3, -4, 5], -3, [[-4, -2, 3]]),
 ]
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@algorithms/two_pointers/three_sum/test_three_sum.py` around lines 17 - 19,
Add a broader set of unit cases to THREE_NUMBER_SUM_TEST_CASES to improve
coverage for the three_number_sum implementation: include an empty array case
([], 0, []), a short-array case with fewer than 3 elements ([1, 2], 3, []), a
no-match case ([1, 2, 3, 4], 100, []), and a negative-target case ([1, -2, 3,
-4, 5], -3, [[-4, -2, 3]]); ensure these new tuples are appended to the existing
THREE_NUMBER_SUM_TEST_CASES list so the test harness that calls three_number_sum
exercises these edge scenarios.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@algorithms/two_pointers/three_sum/__init__.py`:
- Around line 54-56: Add a comprehensive docstring to the three_number_sum
function matching the style of three_sum: describe the function purpose,
parameters (array: List[int], target_sum: int), return value (List[List[int]]),
and include time/space complexity notes and any ordering or uniqueness
guarantees; update the top of the three_number_sum function to include this
docstring so it matches the existing documentation format used by three_sum.
- Around line 65-67: The outer loop in the related function should skip
duplicate starting values to avoid producing duplicate triplets: inside the for
idx, num in enumerate(nums) loop in three_sum, add a guard like "if idx > 0 and
nums[idx] == nums[idx - 1]: continue" (use the same sorted nums input as the
inner logic expects) so repeated outer values are skipped; update the loop that
defines left = idx + 1 and right = n - 1 to include this duplicate-skip check.

In `@algorithms/two_pointers/three_sum/test_three_sum.py`:
- Around line 17-19: Add a broader set of unit cases to
THREE_NUMBER_SUM_TEST_CASES to improve coverage for the three_number_sum
implementation: include an empty array case ([], 0, []), a short-array case with
fewer than 3 elements ([1, 2], 3, []), a no-match case ([1, 2, 3, 4], 100, []),
and a negative-target case ([1, -2, 3, -4, 5], -3, [[-4, -2, 3]]); ensure these
new tuples are appended to the existing THREE_NUMBER_SUM_TEST_CASES list so the
test harness that calls three_number_sum exercises these edge scenarios.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 33ac8942-db84-41be-838d-f7820ad3aebe

📥 Commits

Reviewing files that changed from the base of the PR and between 366ce25 and b15e349.

📒 Files selected for processing (5)
  • algorithms/arrays/remove_duplicates/README.md
  • algorithms/arrays/remove_duplicates/__init__.py
  • algorithms/two_pointers/three_sum/README.md
  • algorithms/two_pointers/three_sum/__init__.py
  • algorithms/two_pointers/three_sum/test_three_sum.py

@BrianLusina BrianLusina merged commit 15abdc5 into main Mar 11, 2026
6 of 8 checks passed
@BrianLusina BrianLusina deleted the feat/algorithms-three-number-sum branch March 11, 2026 11:58
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 Binary Search Binary Search Algorithm Documentation Documentation Updates enhancement In Place Manipulation Sorting Contains sorting in the algorithm Two Pointers Two pointer algorithm

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant