feat(algorithms, stack): min str length after removing substrings#177
feat(algorithms, stack): min str length after removing substrings#177BrianLusina merged 7 commits intomainfrom
Conversation
|
Caution Review failedThe pull request is closed. 📝 WalkthroughWalkthroughAdds new algorithm implementations and documentation: "Magnetic Force Between Two Balls" (binary search), "Minimum String Length After Removing Substrings" (three approaches), corresponding unit tests, and additional DIRECTORY.md entries including Bloom Filter links (some duplicated placements). Changes
Sequence Diagram(s)(omitted) Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Suggested labels
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ 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 |
There was a problem hiding this comment.
Actionable comments posted: 3
🤖 Fix all issues with AI agents
In
`@algorithms/search/binary_search/magnetic_force_between_two_balls/__init__.py`:
- Around line 17-19: The module/function docstring that begins "Check if we can
place 'm' balls at 'position' with each ball having at least 'x' gap." has a
stray quote after the opening triple-quote (it is currently """ "), causing a
malformed docstring; remove the extra double-quote so the docstring starts with
""" and ensure the closing triple-quote remains intact (locate the docstring for
the helper that checks placement of m balls and fix the opening delimiter).
In `@algorithms/search/binary_search/magnetic_force_between_two_balls/README.md`:
- Around line 51-52: The second bullet point is inverted; change the sentence
that begins "Similarly if you cannot place balls such that the answer is x then
you can do it for y where y > x." to state the correct monotonicity: "If you
cannot place balls such that the answer is x then you cannot do it for y where y
> x." Keep the first bullet ("If you can place balls such that the answer is x
then you can do it for y where y < x.") as-is so the README explains the
monotonic property correctly.
In `@algorithms/stack/minimum_string_length_after_removing_substrings/README.md`:
- Around line 180-182: Fix the broken image reference in README.md: replace the
duplicate second image path that currently points to
"minimum_string_length_after_removing_substrings_in-place_solution_1.png" and
missing "./" with the correct
"./images/solutions/minimum_string_length_after_removing_substrings_in-place_solution_2.png"
so Solution 2 uses the distinct _solution_2.png filename and consistent "./"
prefix like the other image links; leave Solution 1 and Solution 3 lines
unchanged.
🧹 Nitpick comments (2)
algorithms/search/binary_search/magnetic_force_between_two_balls/__init__.py (1)
13-14: Upper bound usessorted_positions[-1]instead of the range.The high bound should be based on
(sorted_positions[-1] - sorted_positions[0]) // (m - 1) + 1rather thansorted_positions[-1] / (m - 1). The current formula works correctly but causes unnecessary binary search iterations when the minimum position is large.♻️ Suggested improvement
- high = int(sorted_positions[-1] / (m - 1.0)) + 1 + high = (sorted_positions[-1] - sorted_positions[0]) // (m - 1) + 1algorithms/stack/minimum_string_length_after_removing_substrings/__init__.py (1)
27-34: Minor inefficiency: redundantinchecks.The
whilecondition already checks for"AB" in s, but theifon line 29 checks again. You could simplify slightly, though this doesn't affect correctness.♻️ Optional simplification
def min_length_string_replace(s: str) -> int: - while "AB" in s or "CD" in s: - if "AB" in s: - s = s.replace("AB", "") - elif "CD" in s: - s = s.replace("CD", "") + while "AB" in s or "CD" in s: + s = s.replace("AB", "").replace("CD", "") return len(s)
algorithms/search/binary_search/magnetic_force_between_two_balls/__init__.py
Outdated
Show resolved
Hide resolved
algorithms/search/binary_search/magnetic_force_between_two_balls/README.md
Outdated
Show resolved
Hide resolved
algorithms/stack/minimum_string_length_after_removing_substrings/README.md
Show resolved
Hide resolved
…gs/README.md Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
…ls/__init__.py Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
…ls/README.md Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
There was a problem hiding this comment.
Actionable comments posted: 3
🤖 Fix all issues with AI agents
In
`@algorithms/search/binary_search/magnetic_force_between_two_balls/__init__.py`:
- Around line 4-14: The function max_distance can raise ZeroDivisionError when m
== 1 and lacks validation for m <= 0; update max_distance to handle edge cases
up front: if m <= 0 raise a ValueError (or handle per project conventions), and
if m == 1 return 0 (or the correct value for placing one ball) before computing
high; then compute high using (m - 1.0) safely only when m > 1, keeping the rest
of the binary-search logic unchanged and referencing variables sorted_positions,
low, high, result, n within max_distance.
In `@algorithms/stack/minimum_string_length_after_removing_substrings/README.md`:
- Line 218: Update the C++ space complexity note sentence that currently reads
"String are mutable in C++." to correct grammar by changing it to "Strings are
mutable in C++." in the README entry for the
minimum_string_length_after_removing_substrings algorithm; ensure the rest of
the sentence ("So, all modifications can be done on the input string itself. No
additional data structures") remains unchanged and punctuation is consistent.
- Around line 24-26: Example 4's step descriptions incorrectly claim whole
strings ("ABFCACDB", "FCACDB", "FCAB") are removed; update the three steps so
each operation removes only an "AB" or "CD" substring as allowed by the problem
and update the resulting s after each removal accordingly (i.e., reference
Example 4 and replace the current removals of "ABFCACDB", "FCACDB", "FCAB" with
the correct sequence of removing "AB" or "CD" and the correct intermediate
strings).
🧹 Nitpick comments (1)
algorithms/search/binary_search/magnetic_force_between_two_balls/__init__.py (1)
14-14: Upper bound can be tightened.The search space upper bound uses
sorted_positions[-1](the absolute max value) instead of the rangesorted_positions[-1] - sorted_positions[0]. The current bound is correct but looser than necessary, resulting in a few extra binary search iterations.♻️ Suggested tighter bound
- high = int(sorted_positions[-1] / (m - 1.0)) + 1 + high = int((sorted_positions[-1] - sorted_positions[0]) / (m - 1.0)) + 1
algorithms/search/binary_search/magnetic_force_between_two_balls/__init__.py
Show resolved
Hide resolved
algorithms/stack/minimum_string_length_after_removing_substrings/README.md
Outdated
Show resolved
Hide resolved
algorithms/stack/minimum_string_length_after_removing_substrings/README.md
Show resolved
Hide resolved
…gs/README.md Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
…ls/__init__.py Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Describe your change:
Minimum String length after removing substrings
Checklist:
Fixes: #{$ISSUE_NO}.Summary by CodeRabbit
New Features
Documentation
Tests