Skip to content

fix(coordinate_compression): raise exceptions instead of returning -1#14555

Open
AshrafHosam wants to merge 2 commits intoTheAlgorithms:masterfrom
AshrafHosam:fix/coordinate-compression-consistent-errors
Open

fix(coordinate_compression): raise exceptions instead of returning -1#14555
AshrafHosam wants to merge 2 commits intoTheAlgorithms:masterfrom
AshrafHosam:fix/coordinate-compression-consistent-errors

Conversation

@AshrafHosam
Copy link
Copy Markdown

Describe your change:

Fixes inconsistent error handling in data_compression/coordinate_compression.py reported in #13509.

Previously:

  • compress(x) returned -1 on missing input (silent failure)
  • decompress(n) returned -1 on out-of-range input (silent failure)
  • coordinate_map[x] raised KeyError (exception)

This mix of sentinels and exceptions made errors easy to miss and hard to debug.

This PR unifies the behavior to raise exceptions consistently:

  • compress(x) now raises KeyError (same as the underlying coordinate_map dict)
  • decompress(n) now raises IndexError with a descriptive message
  • Docstrings updated to document the new Raises: contract
  • Doctests updated to cover both the happy path and the error path

Fixes #13509

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing solely the doctests of 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 include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

Previously `compress()` and `decompress()` returned -1 on invalid input while
`coordinate_map[...]` raised KeyError, giving inconsistent error semantics.

This change makes error handling uniform: `compress` raises KeyError and
`decompress` raises IndexError, matching Python conventions and the underlying
dict/list. Docstrings and doctests updated.

Fixes TheAlgorithms#13509
@algorithms-keeper algorithms-keeper bot added enhancement This PR modified some existing files awaiting reviews This PR is ready to be reviewed tests are failing Do not merge until tests pass labels Apr 14, 2026
@algorithms-keeper algorithms-keeper bot removed the tests are failing Do not merge until tests pass label Apr 14, 2026
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.

Bugs

1 participant