From c5a137a55db75dc58e3263c54e5a932fdb71c2d1 Mon Sep 17 00:00:00 2001 From: thejesh Date: Mon, 13 Jul 2026 20:04:42 -0700 Subject: [PATCH] Reject non-lowercase input in a1z26 encode() 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) --- ciphers/a1z26.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/ciphers/a1z26.py b/ciphers/a1z26.py index a1377ea6d397..d47c5bae81c4 100644 --- a/ciphers/a1z26.py +++ b/ciphers/a1z26.py @@ -13,7 +13,17 @@ def encode(plain: str) -> list[int]: """ >>> encode("myname") [13, 25, 14, 1, 13, 5] + >>> encode("Hello") + Traceback (most recent call last): + ... + ValueError: plain must contain only lowercase letters a-z + >>> encode("hi there") + Traceback (most recent call last): + ... + ValueError: plain must contain only lowercase letters a-z """ + if not plain or not all("a" <= elem <= "z" for elem in plain): + raise ValueError("plain must contain only lowercase letters a-z") return [ord(elem) - 96 for elem in plain]