mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-03-12 09:39:49 +00:00
37 lines
732 B
Python
37 lines
732 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
|
|
"""
|
|
|
|
|
|
from typing import List
|
|
|
|
|
|
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()
|