From fbc038d5324b82b12bb686c84b9589fdac64a236 Mon Sep 17 00:00:00 2001 From: LethargicLeprechaun <64550669+LethargicLeprechaun@users.noreply.github.com> Date: Wed, 29 Apr 2020 23:23:51 +0100 Subject: [PATCH] Added A1Z26 Cipher (#1914) * A1Z26 Cipher * A1Z26 Cipher * Added type hints * Added Doctests * removed tabs, spaces instead * corrected doctest * corrected doctest * info URLs added * Condensed decode to one line * Condensed encode function to a single line * Nice one! Co-authored-by: Christian Clauss --- ciphers/a1z26.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 ciphers/a1z26.py diff --git a/ciphers/a1z26.py b/ciphers/a1z26.py new file mode 100644 index 000000000..3684c8cb3 --- /dev/null +++ b/ciphers/a1z26.py @@ -0,0 +1,29 @@ +""" +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: + """ + >>> encode("myname") + [13, 25, 14, 1, 13, 5] + """ + return [ord(elem) - 96 for elem in plain] + +def decode(encoded: list) -> str: + """ + >>> decode([13, 25, 14, 1, 13, 5]) + 'myname' + """ + return "".join(chr(elem + 96) for elem in encoded) + +def main(): + encoded = encode(input("->").strip().lower()) + print("Encoded: ", encoded) + print("Decoded:", decode(encoded)) + +if __name__ == "__main__": + main()