Skip to content

ciphers/a1z26: reject non-lowercase input in encode()#14934

Open
thejesh23 wants to merge 1 commit into
TheAlgorithms:masterfrom
thejesh23:fix/a1z26-encode-validation
Open

ciphers/a1z26: reject non-lowercase input in encode()#14934
thejesh23 wants to merge 1 commit into
TheAlgorithms:masterfrom
thejesh23:fix/a1z26-encode-validation

Conversation

@thejesh23

Copy link
Copy Markdown

Describe your change:

Fixes #14931

ciphers.a1z26.encode currently has no input validation and silently produces meaningless output (negative numbers, out-of-range integers) for any character outside az:

```

encode("A")
[-31]
encode("HELLO")
[-24, -27, -20, -20, -17]
encode("hi there")
[8, 9, -64, 20, 8, 5, 18, 5]
```

The A1Z26 cipher only defines az126, 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 raises ValueError for the empty string or any character outside az, 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.

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

(The doctest additions are the direct demonstration of the new validation and cannot be split out meaningfully without leaving the new behaviour untested.)

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".

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>
@algorithms-keeper algorithms-keeper Bot added enhancement This PR modified some existing files awaiting reviews This PR is ready to be reviewed labels Jul 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.

ciphers/a1z26.py: encode() silently returns nonsense for non-lowercase input (no validation)

1 participant