Skip to content

Commit 3e23055

Browse files
committed
fix: raise ValueError in encode() for non-lowercase input
encode() previously accepted uppercase letters, digits, and other non-lowercase characters silently, producing incorrect/out-of-range values (e.g. negative numbers for uppercase letters) instead of failing. Add input validation using str.islower() and str.isalpha() to raise a ValueError when the input isn't purely lowercase a-z. Added a doctest covering the new error case.
1 parent c0db072 commit 3e23055

1 file changed

Lines changed: 4 additions & 0 deletions

File tree

ciphers/a1z26.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ def encode(plain: str) -> list[int]:
1313
"""
1414
>>> encode("myname")
1515
[13, 25, 14, 1, 13, 5]
16+
>>> encode("ABCD")
17+
ValueError: plain must contain only lowercase letters (a-z)
1618
"""
19+
if not plain.islower() or not plain.isalpha():
20+
raise ValueError("plain must contain only lowercase letters (a-z)")
1721
return [ord(elem) - 96 for elem in plain]
1822

1923

0 commit comments

Comments
 (0)