ciphers/a1z26: reject non-lowercase input in encode()#14934
Open
thejesh23 wants to merge 1 commit into
Open
Conversation
The A1Z26 cipher maps a-z to 1-26. Previously encode() had no input
validation and would silently produce nonsense output (negative numbers
for uppercase, out-of-range values for spaces/punctuation), e.g.
encode("A") returned [-31].
Raise ValueError for any character outside a-z, matching the
contribution guide's expectation that erroneous input should raise
exceptions rather than silently return wrong values. The empty string
is also rejected because encoding nothing has no meaningful result.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Describe your change:
Fixes #14931
ciphers.a1z26.encodecurrently has no input validation and silently produces meaningless output (negative numbers, out-of-range integers) for any character outsidea–z:```
The A1Z26 cipher only defines
a–z→1–26, and the contribution guide asks that erroneous input raise a Python exception rather than silently return wrong values. This PR adds a single-line guard that raisesValueErrorfor the empty string or any character outsidea–z, and adds two doctests demonstrating the new behaviour (uppercase input and input containing whitespace).Lowercase behaviour is unchanged; the existing
encode("myname")doctest still passes.(The doctest additions are the direct demonstration of the new validation and cannot be split out meaningfully without leaving the new behaviour untested.)
Checklist: