13
\$\begingroup\$

You've got a set of tiles with the symbols from the periodic table. Each symbol appears once. You're thinking up words to make but you want to know if it's possible or not.

The Challenge

Write a program in your favourite language that will take in a string as an input parameter. You may assume that input is not null, has no spaces and consists of ASCII characters.

Your program should take that string and output a truthy value if that word can made up of symbols from the periodic table of elements, and a falsey value if the word cannot.

To make this challenge more difficult, you may not use a symbol twice. So if you use Nitrogen N you may not use N again in the same word.

Rules

Standard loopholes are not allowed. You may use symbols from elements 1-118 (Hydrogen to Unun­octium). You can find a list of all the elements here. You may read the list of symbols in from a file or input arguments if you wish.

Test Cases:

Laos - true (LaOs)
Amputation - true (AmPuTaTiON)
Heinous - true (HeINoUS)
Hypothalamus - true (HYPoThAlAmUS)
Singapore - true (SiNGaPoRe)
Brainfuck - true (BRaInFUCK)
Candycane - false

This is a code golf challenge, shortest code wins.

BEFORE YOU CLOSE AS DUPLICATE: While this may seem similar to this challenge, I feel it is different because it is not 'Generate a list of all words that are possible from the periodic table', it is 'Take in arbitrary input and determine if it can be made from the periodic table'

\$\endgroup\$
14
  • 2
    \$\begingroup\$ There's this which is more closely related but was itself closed as a duplicate of the challenge you linked. I think the fact you can only use a symbol once is the greatest differentiation between the challenges though. In terms of golfing I doubt there's a better way than brute forcing all possible words and checking if the input is among them. \$\endgroup\$ Jun 23, 2016 at 7:24
  • 1
    \$\begingroup\$ "You may read the list of symbols in from a file if you wish." - how do we count it? Is size of the file added to the code length? Or we can use it for free? \$\endgroup\$
    – Qwertiy
    Jun 23, 2016 at 8:06
  • 2
    \$\begingroup\$ Can we take the list of elements as an argument to the program? \$\endgroup\$
    – Emigna
    Jun 23, 2016 at 8:07
  • 1
    \$\begingroup\$ @Qwertiy, the size of the file is free, but the bytes to read it in are not. \$\endgroup\$
    – JamesENL
    Jun 23, 2016 at 8:16
  • 10
    \$\begingroup\$ Boron, Radium, Iodine, Nitrogen, Fluorine, Uranium, Carbon, Potassium. \$\endgroup\$
    – Neil
    Jun 23, 2016 at 8:46

5 Answers 5

3
\$\begingroup\$

05AB1E, 16 bytes

œvyŒ€J})˜Ùvy²Q}O

Explained

œv                # for each permutation of the list of elements
  yŒ              # get all sublist of elements
    €J            # join as strings to form the words possible to spell
      })˜Ù        # convert to list of unique spellable strings
          vy²Q}   # compare each word with input word
               O  # sum giving 1 if the word is found, else 0

Warning: Extremely slow. I recommend testing on a much smaller subset of elements in the online interpreter.

Takes list of elements as first argument.
Takes word to test as second argument.
Returns 1 for true and 0 for false.

Try it online on a small subset of elements

\$\endgroup\$
3
\$\begingroup\$

Brachylog, 7 bytes

spc~@l.

Call with the list of symbols (all lowercase) as Input, and the word as Output, e.g. run_from_atom('spc~@l.', ["he":"n":"o":"li"], "Nohe"). .

Warning: this is extremely inefficent when all symbols are in the list.

Explanation

spc        Create a string from a permutation of a subset of the Input
   ~@l.    This string can unify with the lowercase version of the Output
\$\endgroup\$
1
  • 4
    \$\begingroup\$ @Downvoter care to explain? \$\endgroup\$
    – Fatalize
    Jun 23, 2016 at 16:12
1
\$\begingroup\$

JavaScript (Firefox 48 or earlier), 103 bytes

f=(w,e=`
H
He
... (list of elements not included in the byte count) ...
Uus
Uuo
`)=>!w||e.match(/\w+/g).some(s=>!w.search(s,`i`)&&f(w.slice(s.length),e.replace(`
${s}
`,`
`)))
\$\endgroup\$
1
\$\begingroup\$

Pyth - 13 bytes

Just checks if any partition of lowercased input has all parts in the periodic table.

sm.A}RQd./rzZ

On mobile, so couldn't set up an actual test suite, but try this.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ You wrote this on a phone!? \$\endgroup\$
    – JamesENL
    Jun 24, 2016 at 0:53
  • \$\begingroup\$ This doesn't ensure an element is not used multiple times. Example. \$\endgroup\$ Jun 24, 2016 at 11:47
1
\$\begingroup\$

Pyth, 11 bytes

s}RySQSM./z

Try it online. Test suite.

Written on my phone, but should work. Very slow for a large number of elements or a long string.

Explanation

  • Take all partitions (./) of the input (z).
  • Sort (S) each partition (M).
  • For each partition (R), see if it is in (}) the list of all subsets (y) of the sorted (S) periodic table given as input (Q).
  • Sum (s) the resulting list of booleans.
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.