Skip to content

Fix recursion depth failure in string transformation reconstruction - #15680

Open
hulei22103-gif wants to merge 1 commit into
TheAlgorithms:masterfrom
hulei22103-gif:fix/string-transformation-recursion
Open

hulei22103-gif wants to merge 1 commit into
TheAlgorithms:masterfrom
hulei22103-gif:fix/string-transformation-recursion

Conversation

@hulei22103-gif

Copy link
Copy Markdown

Describe your change

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Existing doctests are unchanged, following the repository guidance to separate code and test changes.
  • Documentation change?

assemble_transformation() uses one recursive call per edit operation. Converting an empty string to "ab" * 1100 builds its operation table successfully, but reconstructing the 2,200 insertions raises RecursionError with the default recursion limit of 1,000. The deletion-only case fails in the same way.

Walk the same predecessor cells iteratively, collect operations in reverse order, and reverse the result once at the end. This preserves the existing path and operation order without using a Python call stack proportional to the number of edits. The dynamic programming cost calculation is unchanged.

Reproduction, from the repository root:

from strings.min_cost_string_conversion import (
    assemble_transformation,
    compute_transform_tables,
)

text = "ab" * 1100
_, ops = compute_transform_tables("", text, 1, 2, 3, 3)
result = assemble_transformation(ops, 0, len(text))
assert result == ["I" + char for char in text]

Validation on Windows with Python 3.15.2 free-threading build:

  • Existing doctests: 19 examples passed; targeted pytest: 2 items passed.
  • An external regression script (not included in this code-only PR) checks empty input, each operation type, prefix coordinates, table immutability, and 2,200-operation insertion/deletion paths. The original revision fails the two long-path tests; the patch passes all nine tests.
  • The same script checks all pairs of binary strings of lengths 0 through 4 under three cost configurations: 2,883 cases. Every reconstructed sequence matches the original implementation, produces the destination, consumes the source, and has the cost recorded in the cost table.
  • Ruff lint, Ruff format check, and the informational ty check passed for the changed file.
  • Applicable pre-commit checks passed except the filename hook's Windows launcher (exit code 9009). Running the same scripts/validate_filenames.py directly with Python 3.15t passed. uv-lock was skipped because dependencies and the lockfile are unchanged.

Full repository tests and remote CI have not been run locally. This fixes reconstruction stack usage; the cost-table algorithm still requires O(m*n) time and space. Malformed operation tables are outside this change.

AI assistance: OpenAI Codex helped identify and implement this change, run local validation, and prepare this pull request. Human review and understanding of the patch remain necessary.

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.
  • All new Python files are placed inside an existing directory. (No new files.)
  • 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. (Local doctests pass; remote CI is pending.)
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation. (No new algorithm.)
  • If this pull request resolves one or more open issues, then the description above includes the issue numbers with a closing keyword. (No corresponding open issue identified.)

The authorship checkbox is left unchecked because this is an AI-assisted contribution, as disclosed above.

@algorithms-keeper algorithms-keeper Bot added awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files labels Oct 31, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

⌂ Proxy