Skip to content

feat(algorithms, backtracking): find subsets#175

Merged
BrianLusina merged 2 commits intomainfrom
feat/algorithms-backtracking-find-subsets
Feb 13, 2026
Merged

feat(algorithms, backtracking): find subsets#175
BrianLusina merged 2 commits intomainfrom
feat/algorithms-backtracking-find-subsets

Conversation

@BrianLusina
Copy link
Owner

@BrianLusina BrianLusina commented Feb 12, 2026

Describe your change:

Find all subsets

  • 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

    • Added subset generation algorithms with support for handling duplicate elements.
  • Documentation

    • Added comprehensive documentation and examples for subset generation, including constraints and test scenarios.
  • Chores

    • Reorganized subset-related algorithms for improved categorization and structure.

@BrianLusina BrianLusina self-assigned this Feb 12, 2026
@BrianLusina BrianLusina added enhancement Algorithm Algorithm Problem Documentation Documentation Updates Backtracking Backtracking Algorithm Array Array data structure Recursion Subsets labels Feb 12, 2026
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 12, 2026

📝 Walkthrough

Walkthrough

This PR relocates subset-related algorithms from algorithms/subsets/ to algorithms/backtracking/subsets/, adds a new variant to handle subsets with duplicate elements, updates corresponding documentation and tests, and reflects these structural changes in the directory manifest.

Changes

Cohort / File(s) Summary
Directory & Documentation Structure
DIRECTORY.md, algorithms/backtracking/subsets/find_all_subsets/README.md
Updated directory hierarchy to move subset algorithms from Stack to Backtracking category. Added new "Subsets II" documentation section describing duplicate handling with constraints and examples.
New Backtracking Implementation
algorithms/backtracking/subsets/find_all_subsets/__init__.py
Introduced two public functions: find_all_subsets() for basic subset generation and find_all_subsets_with_duplicates() for handling duplicate elements via in-place sorting and careful backtracking logic.
New Test Coverage
algorithms/backtracking/subsets/find_all_subsets/test_find_all_subsets.py, algorithms/backtracking/subsets/cascading_subsets/test_cascading_subsets.py
Added comprehensive parameterized test suite for both subset functions covering empty inputs, unique elements, and duplicates. Updated cascading subsets test import path to reflect new location.
Removed Old Implementation
algorithms/subsets/find_all_subsets/__init__.py, algorithms/subsets/find_all_subsets/test_find_all_subsets.py
Deleted original subset implementation and associated tests from old location, completing the module relocation.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested labels

Datastructures

Poem

🐰 From Stack to Backtrack, our subsets now flow,
With duplicates handled and tests all aglow,
We hop through recursion with pop and with push,
Where each subset finds home in the algorithmic brush! 🌿✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 16.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: adding a backtracking algorithm to find all subsets in the algorithms collection.
Description check ✅ Passed The description follows the template structure, marks relevant checklist items, and provides context about the changes made to implement the subset-finding algorithm.

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

✨ 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/algorithms-backtracking-find-subsets

Tip

Issue Planner is now in beta. Read the docs and try it out! Share your feedback on Discord.


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

🤖 Fix all issues with AI agents
In `@algorithms/backtracking/subsets/find_all_subsets/__init__.py`:
- Around line 7-8: The base case for empty input is incorrect: change the n == 0
return in find_all_subsets to return [[]] (a list containing the empty subset)
instead of [] so the power set of an empty list is represented correctly; make
the analogous change in find_all_subsets_with_duplicates (where lines 28-29
currently return []) to return [[]] as well, and add a brief comment in both
functions noting this is the mathematical power set base case.
🧹 Nitpick comments (3)
algorithms/backtracking/subsets/find_all_subsets/__init__.py (3)

35-35: nums.sort() mutates the caller's input list.

This is a side effect that may surprise callers. Consider using sorted() to avoid modifying the input:

Proposed fix
-    nums.sort()
+    nums = sorted(nums)

37-37: Incorrect type annotation for subsets.

subsets accumulates integers and is later copied as List[int] into result: List[List[int]], so the annotation is technically correct at the variable level. However, for clarity and consistency with find_all_subsets (which names its accumulator curr), consider renaming to curr or current_subset.


12-12: Inner backtrack function lacks type hints on parameters.

The backtrack helper in find_all_subsets has no type annotations for first and curr, unlike the backtrack in find_all_subsets_with_duplicates which is properly annotated. Consider adding annotations for consistency.

Comment on lines +7 to +8
if n == 0:
return []
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

Empty input returns [] instead of [[]].

Mathematically, the power set of an empty set is {∅}, i.e., [[]]. Returning [] (no subsets at all) deviates from the standard definition. The same applies to find_all_subsets_with_duplicates at lines 28-29. If this is intentional, consider adding a comment explaining the design choice.

🤖 Prompt for AI Agents
In `@algorithms/backtracking/subsets/find_all_subsets/__init__.py` around lines 7
- 8, The base case for empty input is incorrect: change the n == 0 return in
find_all_subsets to return [[]] (a list containing the empty subset) instead of
[] so the power set of an empty list is represented correctly; make the
analogous change in find_all_subsets_with_duplicates (where lines 28-29
currently return []) to return [[]] as well, and add a brief comment in both
functions noting this is the mathematical power set base case.

@BrianLusina BrianLusina merged commit f52c20c into main Feb 13, 2026
5 of 6 checks passed
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 Backtracking Backtracking Algorithm Documentation Documentation Updates enhancement Recursion Subsets

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant