Created
February 13, 2019 16:45
-
-
Save elitak/4352b8c0e19e065e95e81d2ed2000459 to your computer and use it in GitHub Desktop.
morse funcs
This file contains hidden or 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 python | |
keylist = " 5H4S!V3I?F@U$^2E L R - A P W J .6B=D/X7N C K Y T Z G Q M8: O9)0" | |
def decode(code): | |
o = i = len(keylist) >> 1 | |
for c in code: | |
o >>= 1 | |
i += o if c == '-' else -o | |
return keylist[i] | |
print decode("****") | |
print decode("**") | |
print decode("") | |
# Take this a step further and recognize that each bit in the index to a given | |
# glyph can be treated as a branch decision in the tree: | |
def encode(text): | |
output = "" | |
for c in text.upper(): | |
if c == ' ': | |
continue | |
i = keylist.find(c) | |
code = "" | |
for b in range(5)[::-1]: | |
if decode(code) == c: | |
break | |
code += "-" if i>>1 & (1<<b) else "*" | |
output += " " + code | |
return output.strip() | |
print encode("hi tgunz sos plz") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment