Skip to content

Commit c5a137a

Browse files
thejesh23claude
andcommitted
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) <noreply@anthropic.com>
1 parent c0db072 commit c5a137a

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

ciphers/a1z26.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,17 @@ def encode(plain: str) -> list[int]:
1313
"""
1414
>>> encode("myname")
1515
[13, 25, 14, 1, 13, 5]
16+
>>> encode("Hello")
17+
Traceback (most recent call last):
18+
...
19+
ValueError: plain must contain only lowercase letters a-z
20+
>>> encode("hi there")
21+
Traceback (most recent call last):
22+
...
23+
ValueError: plain must contain only lowercase letters a-z
1624
"""
25+
if not plain or not all("a" <= elem <= "z" for elem in plain):
26+
raise ValueError("plain must contain only lowercase letters a-z")
1727
return [ord(elem) - 96 for elem in plain]
1828

1929

0 commit comments

Comments
 (0)