Last active
November 11, 2021 18:37
-
-
Save Goorzhel/af5154de418d7943adb798a434b760e5 to your computer and use it in GitHub Desktop.
Demilich cipher codec.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
The Finnish death metal band Demilich released an album in 1993 called | |
"Nespithe". The liner notes, as well as the album title and one song title, | |
are written in code: start at the end of the phrase, group the letters in | |
three, and reverse the groups' order. | |
""" | |
from sys import argv, stdin | |
def encode(string): | |
string = string.replace(" ","") | |
newstr = "" | |
for i in range(0,len(string),3): | |
newstr = string[i:i+3] + newstr | |
return newstr.upper() | |
def decode(string): | |
string = string.replace(" ","") | |
newstr = "" | |
for i in range(len(string),2,-3): | |
newstr += string[i-3:i] | |
newstr += string[0:i%3] | |
return newstr.upper() | |
def test(): | |
plain = "THESPINE" | |
cipher = "NESPITHE" | |
assert encode(plain) == cipher | |
assert decode(cipher) == plain | |
OPS = {"-d": decode, "-e": encode} | |
def main(): | |
if len(argv) < 2 or argv[1] not in OPS: | |
raise RuntimeError(f"Argument must be one of: {', '.join(OPS.keys())}") | |
oper = OPS[argv[1]] | |
for line in stdin: | |
line = line.rstrip().replace(" ", "") | |
print(oper(line), end="") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Came back to make this behave more like a Unix text filter, and also attempt some genexpr nonsense—
—that is, until I saw no difference with
hyperfine
and a worse result withtimeit
.