Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Convert a string to a list of element symbols, if possible.
import sys
# try: cat /usr/share/dict/words | python symbols.py -
symbols = [['ac', 'actinium'], ['ag', 'silver'], ['al', 'aluminum'], ['am', 'americium'], ['ar', 'argon'], ['as', 'arsenic'], ['at', 'astatine'], ['au', 'gold'], ['b', 'boron'], ['ba', 'barium'], ['be', 'beryllium'], ['bh', 'bohrium'], ['bi', 'bismuth'], ['bk', 'berkelium'], ['br', 'bromine'], ['c', 'carbon'], ['ca', 'calcium'], ['cd', 'cadmium'], ['ce', 'cerium'], ['cf', 'californium'], ['cl', 'chlorine'], ['cm', 'curium'], ['cn', 'copernicium'], ['co', 'cobalt'], ['cr', 'chromium'], ['cs', 'cesium'], ['cu', 'copper'], ['db', 'dubnium'], ['ds', 'darmstadtium'], ['dy', 'dysprosium'], ['er', 'erbium'], ['es', 'einsteinium'], ['eu', 'europium'], ['f', 'fluorine'], ['fe', 'iron'], ['fl', 'flerovium'], ['fm', 'fermium'], ['fr', 'francium'], ['ga', 'gallium'], ['gd', 'gadolinium'], ['ge', 'germanium'], ['h', 'hydrogen'], ['he', 'helium'], ['hf', 'hafnium'], ['hg', 'mercury'], ['ho', 'holmium'], ['hs', 'hassium'], ['i', 'iodine'], ['in', 'indium'], ['ir', 'iridium'], ['k', 'potassium'], ['kr', 'krypton'], ['la', 'lanthanum'], ['li', 'lithium'], ['lr', 'lawrencium'], ['lu', 'lutetium'], ['lv', 'livermorium'], ['md', 'mendelevium'], ['mg', 'magnesium'], ['mn', 'manganese'], ['mo', 'molybdenum'], ['mt', 'meitnerium'], ['n', 'nitrogen'], ['na', 'sodium'], ['nb', 'niobium'], ['nd', 'neodymium'], ['ne', 'neon'], ['ni', 'nickel'], ['no', 'nobelium'], ['np', 'neptunium'], ['o', 'oxygen'], ['os', 'osmium'], ['p', 'phosphorus'], ['pa', 'protactinium'], ['pb', 'lead'], ['pd', 'palladium'], ['pm', 'promethium'], ['po', 'polonium'], ['pr', 'praseodymium'], ['pt', 'platinum'], ['pu', 'plutonium'], ['ra', 'radium'], ['rb', 'rubidium'], ['re', 'rhenium'], ['rf', 'rutherfordium'], ['rg', 'roentgenium'], ['rh', 'rhodium'], ['rn', 'radon'], ['ru', 'ruthenium'], ['s', 'sulfur'], ['sb', 'antimony'], ['sc', 'scandium'], ['se', 'selenium'], ['sg', 'seaborgium'], ['si', 'silicon'], ['sm', 'samarium'], ['sn', 'tin'], ['sr', 'strontium'], ['ta', 'tantalum'], ['tb', 'terbium'], ['tc', 'technetium'], ['te', 'tellurium'], ['th', 'thorium'], ['ti', 'titanium'], ['tl', 'thallium'], ['tm', 'thulium'], ['u', 'uranium'], ['uun', 'ununnilium'], ['uuo', 'ununoctium'], ['uup', 'ununpentium'], ['uus', 'ununseptium'], ['uut', 'ununtrium'], ['uuu', 'ununumium'], ['v', 'vanadium'], ['w', 'tungsten'], ['xe', 'xenon'], ['y', 'yttrium'], ['yb', 'ytterbium'], ['zn', 'zinc'], ['zr', 'zirconium']]
def complete(s):
if len(s) == 0:
yield ""
else:
for i in symbols:
if s[:len(i[0])] == i[0]:
for k in complete(s[len(i[0]):]):
yield i[1]+"-"+k
if len(sys.argv) > 1 and sys.argv[1] == "-":
for line in sorted(sys.stdin.readlines(), key=lambda x:-len(x)):
for i in complete(line[:-1]):
print line, i[:-1]
print
else:
for i in complete(raw_input("> ")):
print i[:-1]

You fell into the && trap. You need and. My code wouldn't run.
And you removed the while True for non-piping.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment