Skip to content

feat(algorithms, hash-maps): powerful integers#196

Merged
BrianLusina merged 3 commits intomainfrom
feat/algorithms-powerful-integers
Apr 21, 2026
Merged

feat(algorithms, hash-maps): powerful integers#196
BrianLusina merged 3 commits intomainfrom
feat/algorithms-powerful-integers

Conversation

@BrianLusina
Copy link
Copy Markdown
Owner

@BrianLusina BrianLusina commented Apr 21, 2026

Describe your change:

Algorithm to find the powerful integers given x and y and an upper bound

  • 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

    • Added Powerful Integers functionality supporting two solution approaches for calculating unique numbers representable as x^i + y^j within a given bound.
  • Documentation

    • Added comprehensive documentation for the Powerful Integers problem, including problem definition, example cases, constraints, and both solution approaches with complexity analysis.
    • Enhanced maximum subarray documentation with detailed explanation of Kadane's algorithm, including dynamic-programming subproblems and complexity analysis (O(n) time, O(1) space).
  • Tests

    • Added complete unit test coverage for Powerful Integers implementations with multiple test cases.

@BrianLusina BrianLusina self-assigned this Apr 21, 2026
@BrianLusina BrianLusina added enhancement Algorithm Algorithm Problem Documentation Documentation Updates Hash Map Hash Map Data structure Hash Set Hash Set Data Structure Math labels Apr 21, 2026
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 21, 2026

Warning

Rate limit exceeded

@BrianLusina has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 41 minutes and 15 seconds before requesting another review.

Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 41 minutes and 15 seconds.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 7e10f01c-6d73-4c3f-bf0d-dd5e23ed7d93

📥 Commits

Reviewing files that changed from the base of the PR and between 1ce9d47 and 50a762d.

📒 Files selected for processing (1)
  • algorithms/dynamic_programming/max_subarray/README.md
📝 Walkthrough

Walkthrough

This change introduces a new "Powerful Integers" algorithm to the hash table section, featuring two implementation approaches and comprehensive documentation. Additionally, documentation was added to the max subarray problem, and a minor formatting adjustment was made to an existing file.

Changes

Cohort / File(s) Summary
Powerful Integers Algorithm
algorithms/hash_table/powerful_integers/__init__.py, algorithms/hash_table/powerful_integers/README.md, algorithms/hash_table/powerful_integers/test_powerful_integers.py
Added two solution implementations—powerful_integers and powerful_integers_logarithmic_bounds—that generate unique integers representable as x^i + y^j within a given bound. Both use nested loops with set deduplication and handle edge cases where x or y equals 1. Includes comprehensive documentation of algorithmic approaches and parameterised unit tests.
Documentation Updates
algorithms/dynamic_programming/max_subarray/README.md, DIRECTORY.md
Added solution documentation for the max subarray problem explaining Kadane's algorithm with time/space complexity analysis. Updated directory index with new Powerful Integers entry.
Code Formatting
algorithms/trie/index_pairs_of_a_string/__init__.py
Added blank line between import section and function definition.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 Hark! New integers rise with power,
x and y in each hour,
Bounded by logarithmic might,
Sets deduplicate with delight,
Powerful sums, uniquely bright!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: adding a powerful integers algorithm to the hash table algorithms section.
Description check ✅ Passed The description is complete with a clear summary of the change and all required checklist items addressed, though one optional item about issue references is unchecked.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/algorithms-powerful-integers

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
Copy Markdown
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

🧹 Nitpick comments (3)
algorithms/hash_table/powerful_integers/README.md (1)

56-62: Math notation could be clearer.

m^n <= bound implies n <= log_m(bound), but the rendered text n<=log(m) bound reads ambiguously (it looks like log(m) * bound). Consider rewriting as n <= log_m(bound) or using LaTeX: $n \le \log_m(\text{bound})$ for clarity. Similarly in the Algorithm section (Lines 66-67), a=log(x)bound / b=log(y)bound would be clearer as a = log_x(bound) / b = log_y(bound).

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@algorithms/hash_table/powerful_integers/README.md` around lines 56 - 62,
Rewrite ambiguous logarithm notation in the README: replace "m^n <= bound"
implication text "n<=log(m) bound" with a clear form such as "n <= log_m(bound)"
(or LaTeX "$n \\le \\log_m(\\text{bound})$"), and likewise change
"a=log(x)bound" / "b=log(y)bound" in the Algorithm section to "a = log_x(bound)"
and "b = log_y(bound)" (or use LaTeX "$a = \\log_x(\\text{bound})$, $b =
\\log_y(\\text{bound})$") so the base of the log is explicit for symbols m, n,
x, y, a, b.
algorithms/hash_table/powerful_integers/test_powerful_integers.py (1)

9-17: Good test coverage.

Nice inclusion of edge cases: bound=0 returning [], x=y=1 returning [2], and a large-bound case (100, 100, 1000000) that exercises the log-based path. Consider also adding a case where x**k == bound exactly (e.g. (2, 3, 1024) or similar) to guard against any future int(log(...)) rounding regressions noted in __init__.py.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@algorithms/hash_table/powerful_integers/test_powerful_integers.py` around
lines 9 - 17, Add a test case to POWERFUL_INTEGERS_TEST_CASE that covers the
exact-equality boundary where x**k (or y**k) equals bound (for example use a
tuple like (2, 3, 1024, [...expected results...]) or another appropriate exact
power) so the test guards against off-by-one/rounding issues in the log-based
path; update the expected output list accordingly and ensure the new case is
appended to the existing POWERFUL_INTEGERS_TEST_CASE in
test_powerful_integers.py (this addresses the potential rounding regression
noted in __init__.py).
algorithms/hash_table/powerful_integers/__init__.py (1)

33-55: Minor efficiency and style nits in powerful_integers_logarithmic_bounds.

A few small refinements:

  • x**i is recomputed on every inner-loop iteration; hoist it out of the j loop.
  • When x == 1, a = bound allocates a potentially huge range(bound+1) even though the outer break at Line 52-53 ensures only a single iteration runs. Since the break already guards this, a = 0 (and likewise b = 0) makes the intent clearer and avoids the oversized range.
  • Once value > bound for a given i, all further j yield larger sums (when y > 1), so the inner loop can break early.
  • set([]) can be simplified to set().
♻️ Proposed refactor
-    a = bound if x == 1 else int(log(bound, x))
-    b = bound if y == 1 else int(log(bound, y))
-
-    result_set = set([])
-
-    for i in range(a + 1):
-        for j in range(b + 1):
-            value = x**i + y**j
-
-            if value <= bound:
-                result_set.add(value)
-
-            if y == 1:
-                break
-
-        if x == 1:
-            break
+    a = 0 if x == 1 else int(log(bound, x))
+    b = 0 if y == 1 else int(log(bound, y))
+
+    result_set: Set[int] = set()
+
+    for i in range(a + 1):
+        pow_x = x**i
+        for j in range(b + 1):
+            value = pow_x + y**j
+            if value > bound:
+                break
+            result_set.add(value)
+            if y == 1:
+                break
+        if x == 1:
+            break
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@algorithms/hash_table/powerful_integers/__init__.py` around lines 33 - 55, In
powerful_integers_logarithmic_bounds, avoid recomputing x**i and oversized
ranges: set a = 0 when x == 1 (and b = 0 when y == 1) instead of bound, replace
set([]) with set(), hoist x_pow = x**i (or compute x_pow incrementally per outer
loop) before the inner j loop, and inside the inner loop break early when value
> bound for y > 1 (since further j will only increase the sum); update uses of
a, b, result_set, x_pow, and the inner break accordingly in the function.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@algorithms/dynamic_programming/max_subarray/README.md`:
- Line 50: Remove the comma before "because" in the sentence that reads "The
time complexity of this solution is O(n), because we are iterating the array
once, where n is the total number of" in README.md; edit that sentence to read
"The time complexity of this solution is O(n) because we are iterating the array
once, where n is the total number of" so the essential clause is not set off by
a comma.

---

Nitpick comments:
In `@algorithms/hash_table/powerful_integers/__init__.py`:
- Around line 33-55: In powerful_integers_logarithmic_bounds, avoid recomputing
x**i and oversized ranges: set a = 0 when x == 1 (and b = 0 when y == 1) instead
of bound, replace set([]) with set(), hoist x_pow = x**i (or compute x_pow
incrementally per outer loop) before the inner j loop, and inside the inner loop
break early when value > bound for y > 1 (since further j will only increase the
sum); update uses of a, b, result_set, x_pow, and the inner break accordingly in
the function.

In `@algorithms/hash_table/powerful_integers/README.md`:
- Around line 56-62: Rewrite ambiguous logarithm notation in the README: replace
"m^n <= bound" implication text "n<=log(m) bound" with a clear form such as "n
<= log_m(bound)" (or LaTeX "$n \\le \\log_m(\\text{bound})$"), and likewise
change "a=log(x)bound" / "b=log(y)bound" in the Algorithm section to "a =
log_x(bound)" and "b = log_y(bound)" (or use LaTeX "$a =
\\log_x(\\text{bound})$, $b = \\log_y(\\text{bound})$") so the base of the log
is explicit for symbols m, n, x, y, a, b.

In `@algorithms/hash_table/powerful_integers/test_powerful_integers.py`:
- Around line 9-17: Add a test case to POWERFUL_INTEGERS_TEST_CASE that covers
the exact-equality boundary where x**k (or y**k) equals bound (for example use a
tuple like (2, 3, 1024, [...expected results...]) or another appropriate exact
power) so the test guards against off-by-one/rounding issues in the log-based
path; update the expected output list accordingly and ensure the new case is
appended to the existing POWERFUL_INTEGERS_TEST_CASE in
test_powerful_integers.py (this addresses the potential rounding regression
noted in __init__.py).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: a3f246af-d16d-41a7-8507-fad496d403d2

📥 Commits

Reviewing files that changed from the base of the PR and between 4e409aa and 1ce9d47.

📒 Files selected for processing (6)
  • DIRECTORY.md
  • algorithms/dynamic_programming/max_subarray/README.md
  • algorithms/hash_table/powerful_integers/README.md
  • algorithms/hash_table/powerful_integers/__init__.py
  • algorithms/hash_table/powerful_integers/test_powerful_integers.py
  • algorithms/trie/index_pairs_of_a_string/__init__.py

Comment thread algorithms/dynamic_programming/max_subarray/README.md Outdated
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
@BrianLusina BrianLusina merged commit ad930f7 into main Apr 21, 2026
6 of 8 checks passed
@BrianLusina BrianLusina deleted the feat/algorithms-powerful-integers branch April 21, 2026 08:32
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 enhancement Hash Map Hash Map Data structure Hash Set Hash Set Data Structure Math

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant