Skip to content

Instantly share code, notes, and snippets.

@nejni-marji
Created May 11, 2020 21:29
Show Gist options
  • Save nejni-marji/ba069e96df6b241f6597094c6c6ef1ce to your computer and use it in GitHub Desktop.
Save nejni-marji/ba069e96df6b241f6597094c6c6ef1ce to your computer and use it in GitHub Desktop.
convert words by using the periodic table of elements (symbol -> atomic number) as a hash
#!/usr/bin/env python3
from sys import argv
# written by nejni-marji
# Elem Hash
eh = { "h": "1", "he": "2", "li": "3", "be": "4", "b": "5", "c": "6", "n": "7", "o": "8", "f": "9", "ne": "10", "na": "11", "mg": "12", "al": "13", "si": "14", "p": "15", "s": "16", "cl": "17", "ar": "18", "k": "19", "ca": "20", "sc": "21", "ti": "22", "v": "23", "cr": "24", "mn": "25", "fe": "26", "co": "27", "ni": "28", "cu": "29", "zn": "30", "ga": "31", "ge": "32", "as": "33", "se": "34", "br": "35", "kr": "36", "rb": "37", "sr": "38", "y": "39", "zr": "40", "nb": "41", "mo": "42", "tc": "43", "ru": "44", "rh": "45", "pd": "46", "ag": "47", "cd": "48", "in": "49", "sn": "50", "sb": "51", "te": "52", "i": "53", "xe": "54", "cs": "55", "ba": "56", "la": "57", "ce": "58", "pr": "59", "nd": "60", "pm": "61", "sm": "62", "eu": "63", "gd": "64", "tb": "65", "dy": "66", "ho": "67", "er": "68", "tm": "69", "yb": "70", "lu": "71", "hf": "72", "ta": "73", "w": "74", "re": "75", "os": "76", "ir": "77", "pt": "78", "au": "79", "hg": "80", "tl": "81", "pb": "82", "bi": "83", "po": "84", "at": "85", "rn": "86", "fr": "87", "ra": "88", "ac": "89", "th": "90", "pa": "91", "u": "92", "np": "93", "pu": "94", "am": "95", "cm": "96", "bk": "97", "cf": "98", "es": "99", "fm": "100", "md": "101", "no": "102", "lr": "103", "rf": "104", "db": "105", "sg": "106", "bh": "107", "hs": "108", "mt": "109", "m": "12" }
# Elem List
el = list(eh)
# var key: Word, Resp, Symbol
def conv_word(word):
w = word # conversion is destructive
r = []
while w:
# words can start with multiple so we use array
opts = [i for i in el if w.startswith(i)]
# print(opts)
# if 1, use it. if 2, use the longer one
# if neither, quit elegantly
if len(opts) == 1:
s = opts[0]
elif len(opts) == 2:
s = [i for i in opts if len(i) == 2][0]
else:
# print(f'{word}: not permitted')
return word
# print(s)
# print(w)
# remove substring and put numbers in array
w = w.replace(s, '', 1)
r.append(eh[s])
return ','.join(r)
output = []
for i in argv[1:]:
output.append(conv_word(i))
print(' '.join(output))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment