6
\$\begingroup\$

If you convert a chemical formula to lower case, it may become ambiguous: co can be both CO or Co.

Given an input consisting of a-z0-9, where 0 can only stand behind 0-9. Check whether it's ambiguous, unambiguous, or impossible. Return 3 different values for them. Shortest code wins.

Examples:

co     - ambiguous
2h83o6 - unambiguous
li     - unambiguous
l      - impossible
cli    - ambiguous
c10h7o - unambiguous

One-sentence meaning: Is there zero, one or multiple ways to split input into concatenation of the following strings, case insensitive:

0,1,2,3,4,5,6,7,8,9,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

Notes

  • You need to code the table into your program, since as I searched there was no question requiring an unordered chemical element table.
\$\endgroup\$
8
  • \$\begingroup\$ Given an input consisting of a-z0-9 ~> So the input may contain j or q even if they're not used in any symbol, right? \$\endgroup\$ – Arnauld Jun 3 at 15:25
  • 3
    \$\begingroup\$ Most of the byte count is going to be that long string (or a variation thereof, possibly compressed), rather than the actual code. This makes the challenge less interesting, and is in fact an archetype of chameleon challenge. For similar challenges in the future, consider taking the string as an input \$\endgroup\$ – Luis Mendo Jun 3 at 16:49
  • \$\begingroup\$ Wait... how is 2h83o6 unambiguous in the test cases? Surely this could be decomposed to 2h8 3o6 or 2h 83o6 or 2h83 o6? \$\endgroup\$ – KinuTheDragon Jun 8 at 23:53
  • \$\begingroup\$ @KinuTheDragon What's 2H8 3O6? \$\endgroup\$ – l4m2 Jun 9 at 2:38
  • \$\begingroup\$ @l4m2 I meant that 2h83o6 could possibly be reordered to have 2h8 3o6 in the chemical formula instead of the other forms. Basically, is a multi-digit number ambiguous? (e.g. 83 could be split 8 and 3) \$\endgroup\$ – KinuTheDragon Jun 9 at 10:45
5
\$\begingroup\$

JavaScript (ES6), 265 bytes

Saved 6 bytes thanks to @tsh

Expects an array of characters. Returns 0 for unambiguous, 1 for ambiguous, or -1 for impossible.

s=>s.reduce((p,c,i)=>p|=1/c||b?b=0:/[bcfhiknopsuvwy]/.test(c)+(b=/([ace][rsu]|[cgn][ade]|[iz][nr]|a[cglmt]|b[aehikr]|c[fl-o]|d[bsy]|f[elmr]|h[efgos]|kr|l[airuv]|m[cdgnot]|n[bhiop]|o[gs]|p[abdmortu]|r[abe-hnu]|s[bcegimnr]|t[abcehilms]|xe|yb)$/.test(c+s[i+1]))-1,b=0)

Try it online!

How?

The variable \$p\$ holds the final result. The flag \$b\$ is set whenever a 2-character chemical symbol is matched.

For each character \$c\$ at position \$i\$ in the input array:

  • If \$c\$ is a digit or the flag \$b\$ is set, we clear \$b\$ and leave \$p\$ unchanged.

  • Otherwise, we compute:

    p |= /E1/.test(c) + (b = /E2/.test(c + s[i + 1])) - 1
    

    where /E1/ and /E2/ are regular expressions matching 1-character and 2-character chemical symbols respectively.

    Which leads to:

     /E1/ matching | /E2/ matching | sum - 1 | meaning
    ---------------+---------------+---------+------------
     no            | no            |   -1    | impossible
     no            | yes           |    0    | unchanged
     yes           | no            |    0    | unchanged
     yes           | yes           |    1    | ambiguous
    
\$\endgroup\$
3
  • \$\begingroup\$ i[nr]|...|z[nr] -> [iz][nr] \$\endgroup\$ – tsh Jun 4 at 2:36
  • \$\begingroup\$ a[cglmr-u] ... c[adefl-orsu] ... e[rsu] -> a[cglmt] ... c[adefl-o] ... [ace][rsu] \$\endgroup\$ – tsh Jun 4 at 2:47
  • \$\begingroup\$ c[adefl-o] ... g[ade] ... n[abdehiop] -> c[fl-o] ... [cgn][ade] ... n[bhiop] \$\endgroup\$ – tsh Jun 4 at 2:49
3
\$\begingroup\$

Jelly, 194 bytes

“×ĿcṚ¦mßṛy|3¹,GƊ§ḋCƤṂỴƇƈÇ¡ɱɓkȷ¶ʂạ⁺ịọ9ėżḣ÷8ÆC⁼³°0|KṪƈŀ@:ḤvƥÄƭsɲẸpI0⁷tÞO§ƒ;ḷ8ḟhıA€ḊṪḷ`ɦḊa¥IƭịƤñ¡÷?²ạ©œ5EẏṢɲ⁺ȷɗL4Q¤F⁹~Ṿ⁸ṪṄ|eėgṖ€ṂɠƲ2ʂ⁺ɱḌḳWƘɠṛ;⁵2nȦƝ]ṇUṆ84¥ẸṪḲdþ¹Ẋd©’ṃØẠØD;µe€ØD;ØA¤œṗ
¢Œl⁸ŒṖ¤e€€ȦƇL«2

Try it online!

-2 bytes thanks to caird coinheringaahing via better usage of chaining

Output 2 for ambiguous, 1 for unambiguous, 0 for impossible.

The logic behind this is pretty trivial, I just need to figure out a better way of encoding this periodic table...

“...’ṃØẠØD;µe€ØD;ØA¤œṗ  Helper Link; produce the list of components
“...’                   Compressed Integer
     ṃØẠ                Base-decoded into the upper and lower-case alphabet
        ØD;             Prepend the digits (as strings) as well
           µ            With the list of components (joined on nothing) as the left argument
            e€          Check if each element is in
              ØD;ØA¤    The digits + uppercase alphabet
                    œṗ  And partition the components before those indices

¢Œl⁸ŒṖ¤e€€ȦƇL«2         Main Link
¢Œl                     The components above, lowercased
   ⁸ŒṖ¤                 All ways of partitioning the input
         €              For each partition
        €               For each component
       e                Is it in the list of valid components?
          ȦƇ            Filter to keep partitions where every component was valid
            L           Length; 0 if impossible, 1 if unambiguous, 2+ if ambiguous
             «2         Minimum of the above and 2 (sets ambiguous case to 2 no matter how many occurrences existed)
\$\endgroup\$
6
1
\$\begingroup\$

05AB1E, 117 bytes

Prints -1 for impossible, 0 for unambiguous and 1 for ambiguous.

.œε.•B‚Δ—₅H^ƶƵXJ∍擺l¦¯í-āмIõVÑK:Λ‘(·÷ΓKå<70'V∍∞±ζښ˛¿ĆÅRÙΔu”ípā<À¿ýÌQмÛì‰Óǝ“§KhúγIÑËM`Êιš•#€SASõšøε`ì}˜žhS«såP}O<.±

Try it online!

.œ              # push all partitions of the input
  ε      }      # map over the partitions:
   ...          #   push the table of valid parts (see below)
      s         #   swap to the current partition
       åP       #   test if every part is in the table
          O     # take the sum over all results
           <    # decrease by 1
            .±  # print the sign of this

Compressing the table

.•B...š•                  # alphabet compressed string (contains 26 spaces)
        #                 # split on spaces
         €S               # split each string into a list of chars
           ASõš           # the characters of the alphabet with the empty string prepended
               øε`ì}      # zip alphabet with previous list and prepend matching characters
                    ˜     # flatten the list
                     žhS« # append the digits

Try this part with step-by-step output!

\$\endgroup\$
1
\$\begingroup\$

Jelly, 124 bytes

“€ṡƥvẏÄhʠ!ịẈ?iḥẋỌ¹'ṅɦçç⁹.Sð$ṚṠȥṢK}/>œ;ỵZƤẓ}:;tÆṾḶj⁶ḄỵḋİʠUḊẈ½^ɲṆßṇµ⁶æḟɦ1¥©ḟþjHṀṪỤƑgạȥḂṾḞƓƑ@Ð⁾æƭVẈṣ’ḃ90Äḃ26ịØaØDW€¤;
ŒṖfƑƇ¢L«2

Try it online!

A set of links that takes a string argument and returns 0 for impossible, 1 for a single valid interpretation and 2 for ambiguous.

Loosely based on @hyper-neutrino’s answer, but mostly rewritten.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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