Skip to content

feat(algorithms, backtracking): is additive number#193

Merged
BrianLusina merged 3 commits intomainfrom
feat/algorithms-backtracking-additive-number
Apr 16, 2026
Merged

feat(algorithms, backtracking): is additive number#193
BrianLusina merged 3 commits intomainfrom
feat/algorithms-backtracking-additive-number

Conversation

@BrianLusina
Copy link
Copy Markdown
Owner

@BrianLusina BrianLusina commented Apr 15, 2026

Describe your change:

Algorithm to check if a string is an additive number

  • 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

  • Documentation

    • Updated repository index with new algorithm entries in Backtracking, Counting, Dynamic Programming, Graphs, Intervals and a Design Patterns → Creational → Factory → Notification subtree.
    • Added problem write-up and solution explanation for Additive Number.
  • New Features

    • Added support for validating additive number sequences.
  • Tests

    • Added unit tests covering the new additive-number validation implementations.

BrianLusina and others added 2 commits April 15, 2026 09:09
Algorithm to check if a string is an additive number
@BrianLusina BrianLusina self-assigned this Apr 15, 2026
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 15, 2026

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 24782399-4bc9-40ba-975c-6e1b6d55b1a1

📥 Commits

Reviewing files that changed from the base of the PR and between 125464d and cf0fa07.

📒 Files selected for processing (3)
  • algorithms/backtracking/additive_number/README.md
  • algorithms/backtracking/additive_number/__init__.py
  • algorithms/backtracking/additive_number/test_additive_number.py

📝 Walkthrough

Walkthrough

A new Additive Number algorithm is added to the backtracking category, featuring two distinct implementations (DFS-based and backtracking-based) that determine whether a numeric string forms a valid additive sequence. The addition includes documentation, unit tests, and repository index updates.

Changes

Cohort / File(s) Summary
Backtracking Algorithm Implementation
algorithms/backtracking/additive_number/__init__.py
Two new functions added: is_additive_number_dfs() and is_additive_number_backtrack() implementing additive-sequence validation with leading-zero rules, recursion/backtracking, and pruning.
Documentation
algorithms/backtracking/additive_number/README.md
New README describing the Additive Number problem, examples, backtracking/DFS approach, and complexity notes.
Tests
algorithms/backtracking/additive_number/test_additive_number.py
New parameterised unit tests exercising both implementations with shared test cases.
Directory Index
DIRECTORY.md
Repository index updated to include Additive Number and additional entries: Rank Teams By Votes, Total Appeal Of A String, Number Of Distinct Islands, Shortest Path Length, Find Right Interval, Remove Min Intervals, and Design Patterns → Creational → Factory → Notification subtree.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

Poem

🐇 I hopped through digits, one by one,

Found sums that danced beneath the sun,
Backtrack footsteps light and clever,
Two paths to proof, entwined together.
Code and carrot — joy forever. 🥕

🚥 Pre-merge checks | ✅ 2 | ❌ 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 (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: adding an algorithm to check if a string is an additive number in the backtracking domain.
Description check ✅ Passed The PR description follows the required template structure and comprehensively covers all checklist items, with the author explicitly confirming compliance with contribution guidelines and test requirements.

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

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/algorithms-backtracking-additive-number

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.

🧹 Nitpick comments (3)
algorithms/backtracking/additive_number/test_additive_number.py (1)

8-16: Add explicit leading-zero rejection cases.

Please add at least one negative case like ("1023", False) to directly lock in the “no leading zero” rule for non-zero numbers.

✅ Suggested test-case additions
 IS_ADDITIVE_NUMBER_TEST_CASES = [
     ("112358", True),
     ("199100199", True),
     ("11235813", True),
     ("12345", False),
     ("000", True),
+    ("101", True),
+    ("1023", False),
     ("1", False),
     ("0", False),
 ]
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@algorithms/backtracking/additive_number/test_additive_number.py` around lines
8 - 16, Add a negative test asserting leading-zero rejection by updating the
IS_ADDITIVE_NUMBER_TEST_CASES list in test_additive_number.py (the variable name
is IS_ADDITIVE_NUMBER_TEST_CASES) to include at least ("1023", False) so the
test suite explicitly covers the rule that multi-digit numbers cannot have
leading zeros; place the new tuple among the existing cases.
algorithms/backtracking/additive_number/__init__.py (1)

14-18: Tighten DFS prefix loop to the remaining substring length.

Line 14 currently iterates to n (full input length), which performs redundant slice/int work after the remaining suffix is exhausted.

♻️ Suggested fix
-        for i in range(1, n + 1):
+        for i in range(1, len(number) + 1):
             if a + b == int(number[:i]):
                 if dfs(b, a + b, number[i:]):
                     return True
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@algorithms/backtracking/additive_number/__init__.py` around lines 14 - 18,
The DFS loop currently uses n (original full input length) causing unnecessary
slices when the remaining suffix is shorter; in dfs(a, b, number) replace the
loop "for i in range(1, n + 1):" with a loop bounded by the remaining substring
length (e.g., remaining = len(number); for i in range(1, remaining + 1):) so you
only attempt prefixes that exist, and keep using dfs, a, b and number unchanged
otherwise.
algorithms/backtracking/additive_number/README.md (1)

77-80: Clarify complexity assumptions in this paragraph.

Line 77 currently states O(n^3), while Line 80 also notes per-conversion costs up to O(n). Consider clarifying the model used (fixed-width arithmetic vs big-int/parse costs) to avoid mixed signals.

✏️ Suggested wording update
-The time complexity of the solution is O(n^3) in the worst case, where n is the length of the string num. The outer two
-levels of recursion effectively choose the lengths of the first two numbers, which gives O(n^2) combinations. For each
-combination, verifying the rest of the string takes O(n) time since the additive property uniquely determines each
-subsequent number. Additionally, converting substrings to integers takes up to O(n) per conversion.
+The time complexity is commonly described as O(n^3): O(n^2) choices for the first two numbers, and O(n) to validate the
+rest for each choice. If you also account for substring-to-integer parsing cost, the practical bound can be higher.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@algorithms/backtracking/additive_number/README.md` around lines 77 - 80,
Clarify the complexity paragraph by stating the arithmetic model: specify
whether we assume fixed-width integer arithmetic (so substring-to-integer
parsing is O(1) and overall time O(n^3) dominated by combination +
verification), or we account for parsing/big-int costs (so each substring
conversion is O(n) and total becomes O(n^4)); update the wording around the
terms "O(n^3)", "O(n) per conversion", and the variable "num" to explicitly
state which model is used and how it affects the final complexity bound.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@algorithms/backtracking/additive_number/__init__.py`:
- Around line 14-18: The DFS loop currently uses n (original full input length)
causing unnecessary slices when the remaining suffix is shorter; in dfs(a, b,
number) replace the loop "for i in range(1, n + 1):" with a loop bounded by the
remaining substring length (e.g., remaining = len(number); for i in range(1,
remaining + 1):) so you only attempt prefixes that exist, and keep using dfs, a,
b and number unchanged otherwise.

In `@algorithms/backtracking/additive_number/README.md`:
- Around line 77-80: Clarify the complexity paragraph by stating the arithmetic
model: specify whether we assume fixed-width integer arithmetic (so
substring-to-integer parsing is O(1) and overall time O(n^3) dominated by
combination + verification), or we account for parsing/big-int costs (so each
substring conversion is O(n) and total becomes O(n^4)); update the wording
around the terms "O(n^3)", "O(n) per conversion", and the variable "num" to
explicitly state which model is used and how it affects the final complexity
bound.

In `@algorithms/backtracking/additive_number/test_additive_number.py`:
- Around line 8-16: Add a negative test asserting leading-zero rejection by
updating the IS_ADDITIVE_NUMBER_TEST_CASES list in test_additive_number.py (the
variable name is IS_ADDITIVE_NUMBER_TEST_CASES) to include at least ("1023",
False) so the test suite explicitly covers the rule that multi-digit numbers
cannot have leading zeros; place the new tuple among the existing cases.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 27de1407-791f-4680-bc38-d383b85d7735

📥 Commits

Reviewing files that changed from the base of the PR and between 3683dcf and 125464d.

📒 Files selected for processing (4)
  • DIRECTORY.md
  • algorithms/backtracking/additive_number/README.md
  • algorithms/backtracking/additive_number/__init__.py
  • algorithms/backtracking/additive_number/test_additive_number.py

@BrianLusina BrianLusina merged commit 99f5f91 into main Apr 16, 2026
3 of 6 checks passed
@BrianLusina BrianLusina deleted the feat/algorithms-backtracking-additive-number branch April 16, 2026 06:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant