首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >模糊化学公式

模糊化学公式
EN

Code Golf用户
提问于 2021-06-03 13:58:14
回答 4查看 277关注 0票数 6

如果你把化学公式转换成小写,它可能变得模棱两可:co既可以是CO,也可以是Co

给定由a-z0-9组成的输入,其中0只能支持0-9。检查它是模糊的,明确的,还是不可能的。为它们返回3个不同的值。最短代码获胜。

示例:

代码语言:javascript
复制
co     - ambiguous
2h83o6 - unambiguous
li     - unambiguous
l      - impossible
cli    - ambiguous
c10h7o - unambiguous

一句意思:是否有零个、一种或多种方式将输入分割成以下字符串的串联,不区分大小写:

代码语言:javascript
复制
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

  • 您需要将该表编码到您的程序中,因为当我搜索时,没有问题需要一个无序的化学元素表。
EN

回答 4

Code Golf用户

发布于 2021-06-03 17:39:42

JavaScript (ES6),265个字节

由于@tsh保存了6个字节

需要一个字符数组。无歧义返回0,歧义返回1,不可能返回-1。

代码语言:javascript
复制
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

  • 如果c是一个数字或设置了b标志,则清除b并保持p不变。
  • 另外,我们计算了P |= /E1/..test(C)+ (b =/E2/..test(c+ sI+1)) -1,其中/E1//E2/分别是匹配1-字符和2-字符化学符号的正则表达式。这导致: /E1/匹配的区/E2/匹配的区和-1倍,意思是---------------+---------------+---------+------------,不,不可能,_
票数 5
EN

Code Golf用户

发布于 2021-06-03 14:08:10

果冻,194个字节

代码语言:javascript
复制
“×Ŀ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表示不可能。

这背后的逻辑非常琐碎,我只需要想出一种更好的方法来编码这个周期表。

代码语言:javascript
复制
“...’ṃØẠØ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)
票数 3
EN

Code Golf用户

发布于 2021-06-04 08:35:45

05AB1E,117个字节

打印-1表示不可能,0打印表示无歧义,1打印表示模糊。

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

在网上试试!

代码语言:javascript
复制
.œ              # 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

压缩表

代码语言:javascript
复制
.•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

用一步一步的输出来尝试这个部分!

票数 1
EN
页面原文内容由Code Golf提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codegolf.stackexchange.com/questions/226849

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档