Skip to content

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

Description

@thejesh23

Repository commit

c0db072 (master)

Python version (python --version)

Python 3.12.13

Dependencies version (pip freeze)

Standard library only.

Expected behavior

The A1Z26 cipher maps the letters az to the integers 126. Any character outside that alphabet is not defined by the cipher and — per the project's contribution guide ("raise Python exceptions (`ValueError`, etc.) on erroneous input values") — should raise ValueError rather than silently return meaningless output.

Actual behavior

ciphers.a1z26.encode performs no input validation and just runs ord(ch) - 96 for every character. As a result it silently produces nonsense output for perfectly plausible inputs such as uppercase strings or strings containing punctuation/whitespace:

>>> from ciphers.a1z26 import encode
>>> encode("A")
[-31]
>>> encode("HELLO")
[-24, -27, -20, -20, -17]
>>> encode("hi there")
[8, 9, -64, 20, 8, 5, 18, 5]

The main() block at the bottom of the file already normalises with .strip().lower(), showing the author is aware only lowercase input is meaningful — but programmatic callers get no such protection, and there is no test coverage for these edge cases.

This is a minor bug (works correctly for the doctested lowercase input), but the silent wrong output is worse than an explicit error, and violates the guideline above.

Suggested fix

Reject any character outside az (and reject the empty string, which is also not meaningful) with a clear ValueError, and add doctests covering the new behaviour. A PR is being opened.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions