feat(algorithms greedy) minimum moves to spread stones & minimum boats to rescue people#121
Conversation
WalkthroughThis PR adds two new greedy algorithm implementations to the algorithms library: a boats rescue problem using two-pointer technique and a stone spreading puzzle using permutation-based Manhattan distance minimization, along with documentation and unit tests for each. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
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. Comment |
… of github.com:BrianLusina/PythonSnips into feat/algorithms-greedy-minimum-movees-to-spread-stones
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (4)
DIRECTORY.md (1)
86-87: Fix markdown list indentation for consistency.The indentation for the new subsections doesn't match the expected markdown list formatting used throughout the rest of the file.
🔎 Apply this diff to fix the indentation:
- * Boats - * [Test Boats To Save People](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/greedy/boats/test_boats_to_save_people.py) + * Boats + * [Test Boats To Save People](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/greedy/boats/test_boats_to_save_people.py)- * Spread Stones - * [Test Minimum Moves To Spread Stones](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/greedy/spread_stones/test_minimum_moves_to_spread_stones.py) + * Spread Stones + * [Test Minimum Moves To Spread Stones](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/greedy/spread_stones/test_minimum_moves_to_spread_stones.py)Based on static analysis hints.
Also applies to: 96-97
algorithms/greedy/spread_stones/test_minimum_moves_to_spread_stones.py (1)
16-20: Rename test method for better clarity.The test method name
test_somethingis not descriptive. Consider renaming it to better reflect what's being tested.🔎 Apply this diff to improve the method name:
@parameterized.expand(TEST_CASES) - def test_something(self, grid: List[List[int]], expected: int): + def test_minimum_moves(self, grid: List[List[int]], expected: int): actual = minimum_moves(grid) self.assertEqual(expected, actual)algorithms/greedy/spread_stones/README.md (1)
1-13: Consider enhancing documentation to match the boats README standard.The current documentation covers the problem statement and constraints, which is good. However, the boats README provides a more comprehensive guide including:
- Solution approach explanation (naive vs. optimized)
- Time and space complexity analysis
- Visual examples and walkthroughs
Adding these sections would make the documentation more helpful for contributors and users.
algorithms/greedy/spread_stones/__init__.py (1)
5-44: LGTM! Correct permutation-based solution.The implementation correctly solves the problem by:
- Building surplus and empty cell lists
- Trying all permutations of surplus-to-empty assignments
- Calculating Manhattan distance for each assignment
- Returning the minimum
The brute-force permutation approach has O(k! × k) complexity, but this is acceptable for the 3×3 grid constraint where k ≤ 8.
Minor: Line 30 uses loop variable
conly for counting. Consider using_to indicate the value isn't used:for _ in range(count - 1):
📜 Review details
Configuration used: defaults
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (15)
algorithms/greedy/boats/images/examples/boats_to_save_people_example_1.pngis excluded by!**/*.pngalgorithms/greedy/boats/images/examples/boats_to_save_people_example_2.pngis excluded by!**/*.pngalgorithms/greedy/boats/images/examples/boats_to_save_people_example_3.pngis excluded by!**/*.pngalgorithms/greedy/boats/images/examples/boats_to_save_people_example_4.pngis excluded by!**/*.pngalgorithms/greedy/boats/images/examples/boats_to_save_people_example_5.pngis excluded by!**/*.pngalgorithms/greedy/boats/images/solutions/boats_to_save_people_solution_1.pngis excluded by!**/*.pngalgorithms/greedy/boats/images/solutions/boats_to_save_people_solution_10.pngis excluded by!**/*.pngalgorithms/greedy/boats/images/solutions/boats_to_save_people_solution_2.pngis excluded by!**/*.pngalgorithms/greedy/boats/images/solutions/boats_to_save_people_solution_3.pngis excluded by!**/*.pngalgorithms/greedy/boats/images/solutions/boats_to_save_people_solution_4.pngis excluded by!**/*.pngalgorithms/greedy/boats/images/solutions/boats_to_save_people_solution_5.pngis excluded by!**/*.pngalgorithms/greedy/boats/images/solutions/boats_to_save_people_solution_6.pngis excluded by!**/*.pngalgorithms/greedy/boats/images/solutions/boats_to_save_people_solution_7.pngis excluded by!**/*.pngalgorithms/greedy/boats/images/solutions/boats_to_save_people_solution_8.pngis excluded by!**/*.pngalgorithms/greedy/boats/images/solutions/boats_to_save_people_solution_9.pngis excluded by!**/*.png
📒 Files selected for processing (7)
DIRECTORY.md(2 hunks)algorithms/greedy/boats/README.md(1 hunks)algorithms/greedy/boats/__init__.py(1 hunks)algorithms/greedy/boats/test_boats_to_save_people.py(1 hunks)algorithms/greedy/spread_stones/README.md(1 hunks)algorithms/greedy/spread_stones/__init__.py(1 hunks)algorithms/greedy/spread_stones/test_minimum_moves_to_spread_stones.py(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
algorithms/greedy/boats/test_boats_to_save_people.py (1)
algorithms/greedy/boats/__init__.py (1)
rescue_boats(4-41)
algorithms/greedy/spread_stones/test_minimum_moves_to_spread_stones.py (1)
algorithms/greedy/spread_stones/__init__.py (1)
minimum_moves(5-44)
🪛 markdownlint-cli2 (0.18.1)
DIRECTORY.md
86-86: Unordered list indentation
Expected: 2; Actual: 4
(MD007, ul-indent)
87-87: Unordered list indentation
Expected: 4; Actual: 6
(MD007, ul-indent)
96-96: Unordered list indentation
Expected: 2; Actual: 4
(MD007, ul-indent)
97-97: Unordered list indentation
Expected: 4; Actual: 6
(MD007, ul-indent)
🔇 Additional comments (3)
algorithms/greedy/boats/test_boats_to_save_people.py (1)
1-27: LGTM! Well-structured test suite.The test file is well-organized with comprehensive test cases covering various scenarios including edge cases (single person, all same weight, etc.). The use of parameterized testing keeps the code DRY and maintainable.
algorithms/greedy/boats/README.md (1)
1-97: LGTM! Excellent documentation.The documentation is comprehensive and well-structured, providing:
- Clear problem statement with real-world context
- Explicit constraints
- Visual examples and solution walkthroughs
- Comparison of naive vs. optimized approaches
- Detailed complexity analysis
- Step-by-step implementation guide
This sets a great standard for algorithm documentation in the repository.
algorithms/greedy/boats/__init__.py (1)
1-41: LGTM! Clean and correct implementation.The greedy two-pointer approach is correctly implemented:
- Properly sorts weights and uses two pointers to pair lightest with heaviest
- Defensive programming: copies input to avoid mutation
- Clear variable names and helpful comments
- Matches the algorithm described in the README
The implementation correctly handles all edge cases and has the expected O(n log n) time complexity.
Describe your change:
Adds algorithm challenges:
Checklist:
Fixes: #{$ISSUE_NO}.Summary by CodeRabbit
Release Notes
New Features
Documentation
✏️ Tip: You can customize this high-level summary in your review settings.