如果你把化学公式转换成小写,它可能变得模棱两可:co既可以是CO,也可以是Co。
给定由a-z0-9组成的输入,其中0只能支持0-9。检查它是模糊的,明确的,还是不可能的。为它们返回3个不同的值。最短代码获胜。
示例:
co - ambiguous
2h83o6 - unambiguous
li - unambiguous
l - impossible
cli - ambiguous
c10h7o - unambiguous一句意思:是否有零个、一种或多种方式将输入分割成以下字符串的串联,不区分大小写:
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发布于 2021-06-03 17:39:42
由于@tsh保存了6个字节
需要一个字符数组。无歧义返回0,歧义返回1,不可能返回-1。
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)变量p保存最终结果。只要匹配两个字符的化学符号,就会设置b标志.
对于输入数组中位置为c的每个字符i:
/E1/和/E2/分别是匹配1-字符和2-字符化学符号的正则表达式。这导致: /E1/匹配的区/E2/匹配的区和-1倍,意思是---------------+---------------+---------+------------,不,不可能,_发布于 2021-06-03 14:08:10
“×Ŀ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-2字节,感谢凯恩斯通过更好地使用链接来继承chaining
输出2表示不明确,1表示无歧义,0表示不可能。
这背后的逻辑非常琐碎,我只需要想出一种更好的方法来编码这个周期表。
“...’ṃØẠØ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)发布于 2021-06-04 08:35:45
打印-1表示不可能,0打印表示无歧义,1打印表示模糊。
.œε.•B‚Δ—₅H^ƶƵXJ∍擺l¦¯í-āмIõVÑK:Λ‘(·÷ΓKå<70'V∍∞±ζښ˛¿ĆÅRÙΔu”ípā<À¿ýÌQмÛì‰Óǝ“§KhúγIÑËM`Êιš•#€SASõšøε`ì}˜žhS«såP}O<.±.œ # 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.•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 digitshttps://codegolf.stackexchange.com/questions/226849
复制相似问题