mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-30 16:31:08 +00:00
bc8df6de31
* [pre-commit.ci] pre-commit autoupdate updates: - [github.com/astral-sh/ruff-pre-commit: v0.2.2 → v0.3.2](https://github.com/astral-sh/ruff-pre-commit/compare/v0.2.2...v0.3.2) - [github.com/pre-commit/mirrors-mypy: v1.8.0 → v1.9.0](https://github.com/pre-commit/mirrors-mypy/compare/v1.8.0...v1.9.0) * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
36 lines
742 B
Python
36 lines
742 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 __future__ import annotations
|
|
|
|
|
|
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()
|