Last active
April 9, 2018 20:55
-
-
Save kach/7862258 to your computer and use it in GitHub Desktop.
Convert a string to a list of element symbols, if possible.
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
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] | |
else: | |
for i in complete(raw_input("> ")): | |
print i[:-1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You fell into the && trap. You need
and
. My code wouldn't run.And you removed the while True for non-piping.