Files
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

4.5 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
6a15cadf5f240d05a264955f Challenge 321: Periodic Spelling 29 challenge-321

--description--

Given a word, determine if it can be spelled using element symbols from the periodic table.

  • Ignore casing when spelling a word. "neon" can be spelled with the symbols "Ne", "O", and "N".

Here's a full list of the element symbols:

["H","He","Li","Be","B","C","N","O","F","Ne","Na","Mg","Al","Si","P","S","Cl","Ar","K","Ca","Sc","Ti","V","Cr","Mn","Fe","Co","Ni","Cu","Zn","Ga","Ge","As","Se","Br","Kr","Rb","Sr","Y","Zr","Nb","Mo","Tc","Ru","Rh","Pd","Ag","Cd","In","Sn","Sb","Te","I","Xe","Cs","Ba","La","Ce","Pr","Nd","Pm","Sm","Eu","Gd","Tb","Dy","Ho","Er","Tm","Yb","Lu","Hf","Ta","W","Re","Os","Ir","Pt","Au","Hg","Tl","Pb","Bi","Po","At","Rn","Fr","Ra","Ac","Th","Pa","U","Np","Pu","Am","Cm","Bk","Cf","Es","Fm","Md","No","Lr","Rf","Db","Sg","Bh","Hs","Mt","Ds","Rg","Cn","Nh","Fl","Mc","Lv","Ts","Og"];

Return an array of the elements used to spell the word, in their original casing and in the order to spell the word. Or, an empty array if it can't be spelled.

--hints--

get_periodic_spelling("neon") should return ["Ne", "O", "N"].

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_periodic_spelling("neon"), ["Ne", "O", "N"])`)
}})

get_periodic_spelling("rational") should return ["Ra", "Ti", "O", "N", "Al"].

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_periodic_spelling("rational"), ["Ra", "Ti", "O", "N", "Al"])`)
}})

get_periodic_spelling("yarn") should return ["Y", "Ar", "N"].

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_periodic_spelling("yarn"), ["Y", "Ar", "N"])`)
}})

get_periodic_spelling("carbon") should return ["C", "Ar", "B", "O", "N"] or ["Ca", "Rb", "O", "N"].

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIn(get_periodic_spelling("carbon"), [["C", "Ar", "B", "O", "N"], ["Ca", "Rb", "O", "N"]])`)
}})

get_periodic_spelling("noisy") should return ["N", "O", "I", "S", "Y"] or ["No", "I", "S", "Y"].

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIn(get_periodic_spelling("noisy"), [["N", "O", "I", "S", "Y"], ["No", "I", "S", "Y"]])`)
}})

get_periodic_spelling("bicycles") should return ["B", "I", "C", "Y", "Cl", "Es"] or ["Bi", "C", "Y", "Cl", "Es"].

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIn(get_periodic_spelling("bicycles"), [["B", "I", "C", "Y", "Cl", "Es"], ["Bi", "C", "Y", "Cl", "Es"]])`)
}})

get_periodic_spelling("optics") should return ["O", "P", "Ti", "C", "S"], ["O", "P", "Ti", "Cs"], ["O", "Pt", "I", "C", "S"], or ["O", "Pt", "I", "Cs"].

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIn(get_periodic_spelling("optics"), [["O", "P", "Ti", "C", "S"], ["O", "P", "Ti", "Cs"], ["O", "Pt", "I", "C", "S"], ["O", "Pt", "I", "Cs"]])`)
}})

get_periodic_spelling("value") should return [].

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_periodic_spelling("value"), [])`)
}})

--seed--

--seed-contents--

def get_periodic_spelling(word):

    return word

--solutions--

def get_periodic_spelling(word):
    elements = ["H","He","Li","Be","B","C","N","O","F","Ne","Na","Mg","Al","Si","P","S","Cl","Ar","K","Ca","Sc","Ti","V","Cr","Mn","Fe","Co","Ni","Cu","Zn","Ga","Ge","As","Se","Br","Kr","Rb","Sr","Y","Zr","Nb","Mo","Tc","Ru","Rh","Pd","Ag","Cd","In","Sn","Sb","Te","I","Xe","Cs","Ba","La","Ce","Pr","Nd","Pm","Sm","Eu","Gd","Tb","Dy","Ho","Er","Tm","Yb","Lu","Hf","Ta","W","Re","Os","Ir","Pt","Au","Hg","Tl","Pb","Bi","Po","At","Rn","Fr","Ra","Ac","Th","Pa","U","Np","Pu","Am","Cm","Bk","Cf","Es","Fm","Md","No","Lr","Rf","Db","Sg","Bh","Hs","Mt","Ds","Rg","Cn","Nh","Fl","Mc","Lv","Ts","Og"]
    lower = {e.lower(): e for e in elements}

    def spell(w, i):
        if i == len(w):
            return []
        two = w[i:i + 2]
        one = w[i:i + 1]
        if two in lower:
            rest = spell(w, i + 2)
            if rest is not None:
                return [lower[two]] + rest
        if one in lower:
            rest = spell(w, i + 1)
            if rest is not None:
                return [lower[one]] + rest
        return None

    result = spell(word.lower(), 0)
    return result if result is not None else []