Skip to content

feat(algorithms, greedy): max swap#173

Merged
BrianLusina merged 2 commits intomainfrom
feat/algorithms-greedy-max-swap
Feb 11, 2026
Merged

feat(algorithms, greedy): max swap#173
BrianLusina merged 2 commits intomainfrom
feat/algorithms-greedy-max-swap

Conversation

@BrianLusina
Copy link
Owner

@BrianLusina BrianLusina commented Feb 11, 2026

Describe your change:

Maximum swap of 1 to form largest number for an integer

  • 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 Maximum Swap algorithm with four solution approaches: Brute Force, Greedy Two Pass, Suboptimal Greedy, and Space Optimized Greedy.
    • Added comprehensive documentation with complexity analysis and examples.
  • Tests

    • Added test coverage for all Maximum Swap implementations.
  • Refactor

    • Updated MinHeap API method naming for consistency.
  • Documentation

    • Fixed formatting inconsistencies in Heapsort documentation.

@BrianLusina BrianLusina self-assigned this Feb 11, 2026
@BrianLusina BrianLusina added Algorithm Algorithm Problem Documentation Documentation Updates Greedy Greedy Algorithm Math labels Feb 11, 2026
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 11, 2026

📝 Walkthrough

Walkthrough

This pull request introduces a new Maximum Swap greedy algorithm with four implementations (brute force, two-pass, single-pass, and suboptimal approaches), comprehensive documentation, and unit tests. Additionally, it corrects a MinHeap API method name reference and fixes typographical errors in heap sort documentation.

Changes

Cohort / File(s) Summary
Maximum Swap Algorithm
DIRECTORY.md, algorithms/greedy/maximum_swap/README.md, algorithms/greedy/maximum_swap/__init__.py, algorithms/greedy/maximum_swap/test_maximum_swap.py
New greedy algorithm implementation with four approaches (brute force, two-pass greedy, single-pass greedy, suboptimal greedy), detailed README documentation with proofs and complexity analysis, and comprehensive parameterized unit tests covering multiple input cases.
MinHeap API Update
algorithms/heap/min_cost_to_connect_sticks/__init__.py
Updated method call from MinHeap.insert_data() to MinHeap.insert() to align with renamed heap API.
Documentation Fixes
algorithms/sorting/heapsort/README.md
Corrected notation duplications in complexity descriptions (e.g., "nn" → "n", "O(1)O(1)" → "O(1)").

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~23 minutes

Suggested labels

enhancement

Poem

🐰 A hop through algorithms, greedy and bright,
Four paths to maximum digits' flight,
From brute force bounds to single-pass grace,
We maximize numbers in algorithmic space!

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 11.11% 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 clearly describes the main change: adding a maximum swap algorithm to the greedy algorithms section.
Description check ✅ Passed The description covers the primary change and includes a complete checklist. However, there is a concerning inconsistency: the author checked both 'Fix a bug or typo' and 'Documentation change' boxes when the PR primarily adds a new algorithm, not fixes bugs or typos.

✏️ 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-greedy-max-swap

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

🤖 Fix all issues with AI agents
In `@algorithms/greedy/maximum_swap/README.md`:
- Line 224: Fix the heading typo "Optimality fof the First Swap" in the README
by changing "fof" to "of" so it reads "Optimality of the First Swap"; locate the
heading text "Optimality fof the First Swap" in the README.md and update it
accordingly.

In `@algorithms/sorting/heapsort/README.md`:
- Around line 31-32: Update the duplicate complexity notation "O(1)O(1)" in the
heapsort README: find the line containing the phrase "Since we cleverly reused
available space at the end of the input list to store the item we removed, we
only need O(1)O(1) space overall for heapsort." and replace the duplicated
"O(1)O(1)" with a single "O(1)" (ensure proper spacing/formatting remains
consistent with surrounding sentences and the term "heapsort" is unchanged).
🧹 Nitpick comments (2)
algorithms/greedy/maximum_swap/__init__.py (1)

1-101: Consider adding docstrings to each function.

The PR checklist mentions doctests that pass automated testing. These functions lack docstrings entirely — adding a brief description, examples (as doctests), and a reference URL would align with the repo's contribution guidelines.

algorithms/greedy/maximum_swap/test_maximum_swap.py (1)

10-18: Consider adding edge-case test inputs.

The test matrix covers multi-digit numbers well, but adding single-digit (e.g., (0, 0), (5, 5)) and boundary inputs (e.g., (100000, 100000) per the constraint num <= 10^5) would strengthen coverage.

@BrianLusina BrianLusina merged commit cbcb729 into main Feb 11, 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 Documentation Documentation Updates Greedy Greedy Algorithm Math

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant