Python/ciphers/a1z26.py
Dhruv Manilawala 14bcb580d5
fix(mypy): Fix annotations for 13 cipher algorithms (#4278)
* Initial fix for mypy errors in some cipher algorithms

* fix(mypy): Update type hints

* fix(mypy): Update type hints for enigma_machine2.py

* Update as per the suggestion

Co-authored-by: Christian Clauss <cclauss@me.com>

Co-authored-by: Christian Clauss <cclauss@me.com>
2021-03-22 07:59:51 +01:00

34 lines
706 B
Python

"""
Convert a string of characters to a sequence of numbers
corresponding to the character's position in the alphabet.
https://www.dcode.fr/letter-number-cipher
http://bestcodes.weebly.com/a1z26.html
"""
def encode(plain: str) -> list[int]:
"""
>>> encode("myname")
[13, 25, 14, 1, 13, 5]
"""
return [ord(elem) - 96 for elem in plain]
def decode(encoded: list[int]) -> str:
"""
>>> decode([13, 25, 14, 1, 13, 5])
'myname'
"""
return "".join(chr(elem + 96) for elem in encoded)
def main() -> None:
encoded = encode(input("-> ").strip().lower())
print("Encoded: ", encoded)
print("Decoded:", decode(encoded))
if __name__ == "__main__":
main()