Skip to content

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

Merged
cclauss merged 3 commits into
TheAlgorithms:masterfrom
AshrafHosam:fix/coordinate-compression-consistent-errors
Sep 10, 2026
Merged

fix(coordinate_compression): raise exceptions instead of returning -1#14555
cclauss merged 3 commits into
TheAlgorithms:masterfrom
AshrafHosam:fix/coordinate-compression-consistent-errors

Conversation

@AshrafHosam

Copy link
Copy Markdown
Contributor

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
@cclauss

cclauss commented Sep 10, 2026

Copy link
Copy Markdown
Member

@priya-sundaram-dev, your review, please, as a fix to

@priya-sundaram-dev priya-sundaram-dev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @AshrafHosam — the direction here is right: replacing the silent -1 returns with real exceptions is what #13509 asks for, and the doctests are self-verifying and pass locally (18 tests). Two things to consider before this fully closes the issue:

1. Silent -1 still leaks for duplicate inputs. compress_coordinates() sizes reverse_map as [-1] * len(arr) but only fills one slot per unique value, so duplicates leave trailing -1 sentinels that are still in range for decompress:

>>> cc = CoordinateCompressor([10, 10, 52])
>>> cc.reverse_map
[10, 52, -1]
>>> cc.decompress(2)   # in range (0 <= 2 < 3), so no IndexError
-1

So the "silent failure" this PR removes from the no-duplicates path is reintroduced on the duplicate path. Building the maps from sorted(set(arr)) (so reverse_map has exactly the unique count) closes that hole.

2. Consistency of exception type. The issue title is about a mix of behaviors; compress now raises KeyError while decompress raises IndexError. Both are exceptions (good), but a single type — e.g. ValueError for both — reads as more "consistent" and is easier to document/catch.

Heads-up that #14873 takes the sorted(set(...)) approach and raises ValueError uniformly, which covers both points above — worth a look to align with, or borrow from. Nice work either way.

@priya-sundaram-dev priya-sundaram-dev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM — this is exactly what #13509 asked for. Replacing the -1 sentinels with real exceptions is the right call:

  • compress now surfaces a KeyError for unknown values instead of a magic number a caller could silently treat as a valid coordinate.
  • decompress raises IndexError with a helpful message rather than returning -1 (which collided with a legitimate value/type domain).

The Raises: sections and the Traceback (most recent call last): doctests are correct — I ran them locally and both the KeyError: 7 and IndexError: compressed coordinate 5 is out of range cases reproduce cleanly. No behavioural gaps: every previous -1 return path is now covered by an exception. Nice, minimal diff.

@algorithms-keeper algorithms-keeper Bot removed the awaiting reviews This PR is ready to be reviewed label Sep 10, 2026
@cclauss
cclauss merged commit 5928012 into TheAlgorithms:master Sep 10, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement This PR modified some existing files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bugs

3 participants