[Br]追求Code Golf [Ba] d


20

考虑以下字符串:

Tin Snips

该字符串在元素周期表中包含几个原子符号。我们可以重写此字符串以标识其中几个:

[Ti][N] [Sn][I][P][S]

当然,我们也可以这样写:

T[In] [S][Ni][P][S]

重写输入的规则如下:

  1. 输入的大小写与匹配原子符号无关紧要。
  2. 如果在原子符号中使用元素,则其大小写必须更改,以便该符号正确。例如:h将变为[H]
  3. 所有元素符号都用ASCII方括号[和括起来]
  4. 保留空白:Big ego无法将“ g”和“ e”合并为[Ge]
  5. 并非所有输入字符都需要组合成一个原子符号:如果没有将输入字符放入符号中,则会按原样传递(大小写无关紧要)。
  6. 如果可以制作符号,则必须制作。换句话说,Tin在上面的示例中不允许输出,因为可以在该单词中创建至少一个符号。字符只能通过未使用的唯一时间是无法用于构造原子符号。
  7. 出于此挑战的目的,从氢(1)到Oganesson(118)的所有元素都是有效的。没有更高的元素有效。
  8. 某些高级元素的名称和符号含糊不清:出于挑战的目的,应使用Wikipedia版本。为方便起见,此处允许的原子符号为: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。

编写一个程序或函数,以从单个提供的输入生成所有可能的输出。输入和输出都可以采用您选择的任何形式。它可以是字符串,字符数组或其他某种数据结构:无论是方便还是清楚地表示输入和输出。输入和输出都可以传入/传出代码,但是您可以选择:标准输入/出,函数参数/返回或其他。

  • 输入应为正长度的字符串(请参见上一段),仅包含任意大小写的ASCII字符和空格(0x20)字符。
  • 您的代码必须生成可以使用上述输入规则创建的所有输出字符串。
  • 输出的顺序是实现定义的。唯一的要求是所有输出字符串都存在。
  • 如果提供的有效输入字符串不包含任何原子符号,则只需输出输入字符串即可。
  • 如果显示的输入字符串不符合上述规则(空,零字符,包含非法字符等),则您的程序可能会执行任何操作(崩溃,空白输出等)
  • 输出是不区分大小写的,除了原子符号需要匹配元素周期表。
  • 不允许出现标准漏洞。

测试用例:

Tin Snips
[Ti][N] [Sn][I][P][S]
[Ti][N] [S][Ni][P][S]
[Ti][N] [S][N][I][P][S]
T[In] [Sn][I][P][S]
T[In] [S][Ni][P][S]
T[In] [S][N][I][P][S]
T[I][N] ...

Quack
Q[U][Ac][K]
Q[U]a[C][K]

hehe
[H]e[H]e
[H]e[He]
[He][H]e
[He][He]

Stack Exchange
[S][Ta][C][K] Ex[C][H]a[N][Ge]
[S]t[Ac][K] Ex[C][H]a[N][Ge]

这是代码高尔夫球,所以让我看看您最短的代码!


1
根据@Rassars的评论,锡T[I][N]不是[T][I][N]因为T不是元素。我的问题(可能还有拉萨尔的问题)是:我们是否仅需给出1。仅输出元素取代的最大数量?2.只有最小的浪费量?(带有氢原子的HeHe表示答案是否定的。)3.匹配完全耗尽的所有输出?(在这种情况下T[I][N],以及T[In]将是有效的。)我认为正确的解释是3
级别河圣


1
因此Quack有2种可能性:Q[U][Ac][K]Q[U]a[C][K]。对?
RootTwo

1
所有案例均已验证。
CalculatorFeline

1
@ Challenger5“您的代码必须生成可以使用上述输入规则创建的所有输出字符串”
Jonathan Allan

Answers:


5

Python 3中,289个 263字节

在Pypi上找到了更完整的库: mendeleev

from mendeleev import*
Z={element(i).symbol for i in range(1,119)}
L,R='[]'
def f(h,r=''):t=h.title();return((Z&{t[0]}and f(h[1:],r+L+t[0]+R)or[])+(Z>{(t+L)[:2]}and f(h[2:],r+L+t[:2]+R)or[])+(not{(r[-1:]+t[0]).title(),t[0]}&Z and f(h[1:],r+h[0])or[]))if h else[r]

旧答案:

from elements import*
Z={e.symbol for e in ELEMENTS}|{*'Cn Ds Fl Lv Mc Nh Og Rg Ts'.split()}
L,R='[]'
def f(h,r=''):t=h.title();return((Z&{t[0]}and f(h[1:],r+L+t[0]+R)or[])+(Z>{(t+L)[:2]}and f(h[2:],r+L+t[:2]+R)or[])+(not{(r[-1:]+t[0]).title(),t[0]}&Z and f(h[1:],r+h[0])or[]))if h else[r]

使用库elements.pyhttp://www.lfd.uci.edu/~gohlke/code/elements.py.html。它缺少110到118元素,但它是我能找到的最新库。花费40个字节来添加缺少的元素。

最棘手的部分是逻辑,何时字符可以通过而无需成为元素符号的一部分。


1
恩,不是mendeleev用户,不是图书馆吗?
马修·罗

3

果冻 192个  191字节

-1通过使用Ɗ(自快速开发以来)

“¦UV2ḤF2ı½ṅḶḊ⁼5JI5MẇvẋẊẊ¬Ḥḳ'ƈ<ḷėƤ7*⁾ṾxMæS⁺`?^Ƭb¦ɗDß⁼pþɲOṃ⁽2Ė>,Ḣ(ḞŒƊOGƤ×⁺ṇṂ®ȤT0y°^Ẇ⁺:Þ]ṢṬ¶ịṪḂƇ ñAƬCṫ$wÆĿĖỴỴƇẓƊqḌ@;ẏ`ṃFƥḣ⁽²»ḟ⁶s2;“¤²R.ȯ7ŒL£ɦ»Œt
Œte¢
ŒṖµL€=1oÇ€ṂµÐfµṡ2;€ÇÐfÇ€€S€¬SµÐḟ⁾[]jŒtƊ¹Ç?€€

在线尝试!-在60秒内完成“ Stack Exchange”测试用例的效率太低(离线运行它会在2分钟内给出正确的结果)。

怎么样?

代码的第一行是一条尼拉度链接,用于创建包含所有118个元素符号的列表。为此,它连接了两个列表,第一个包含所有长度为2的字符(即字符串)列表,第二个包含字符列表和标题列表(结果列表)。这两个列表本身主要是通过在Jelly词典中查找单词以创建单个字符串来创建的。

这些压缩的第一个是:

“¦UV2ḤF2ı½ṅḶḊ⁼5JI5MẇvẋẊẊ¬Ḥḳ'ƈ<ḷėƤ7*⁾ṾxMæS⁺`?^Ƭb¦ɗDß⁼pþɲOṃ⁽2Ė>,Ḣ(ḞŒƊOGƤ×⁺ṇṂ®ȤT0y°^Ẇ⁺:Þ]ṢṬ¶ịṪḂƇ ñAƬCṫ$wÆĿĖỴỴƇẓƊqḌ@;ẏ`ṃFƥḣ⁽²»

产生

" biznagas sepmag ratbag catchflies paracmes mdse bharal ramcat monopteros irrepressibilities lunarnauts geniculate hopbinds rutabaga potlache broghs bergamas crossbirth purblind xebecs nonhardy classism fleurets moneybag scarce corf Mg Sr Zr CD HG CF FM Lr SG TM Gd Bk Fr Rh Fe Sn lv cndbmnnbkrmtpdnp"

除了最后一个条目(由空格分隔)之外的所有条目都是Jelly词典中的条目。用过滤掉空格ḟ⁶,然后将结果分成两部分:

["bi","zn","ag","as","se","pm","ag","ra","tb","ag","ca","tc","hf","li","es","pa","ra","cm","es","md","se","bh","ar","al","ra","mc","at","mo","no","pt","er","os","ir","re","pr","es","si","bi","li","ti","es","lu","na","rn","au","ts","ge","ni","cu","la","te","ho","pb","in","ds","ru","ta","ba","ga","po","tl","ac","he","br","og","hs","be","rg","am","as","cr","os","sb","ir","th","pu","rb","li","nd","xe","be","cs","no","nh","ar","dy","cl","as","si","sm","fl","eu","re","ts","mo","ne","yb","ag","sc","ar","ce","co","rf","Mg","Sr","Zr","CD","HG","CF","FM","Lr","SG","TM","Gd","Bk","Fr","Rh","Fe","Sn","lv","cn","db","mn","nb","kr","mt","pd","np"]

第二,

“¤²R.ȯ7ŒL£ɦ»

由单词“ finch”,“ pub”,“ sky”和“ vow”(不带空格)的串联组成,因此是一个字符列表:

['f','i','n','c','h','p','u','b','s','k','y','v','o','w']

这两个列表是用串联在一起的;,每个条目都使用进行标题大小写转换Œt,产生:

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

包含所有118个必需元素符号的列表(有重复项,但是可以)。

第二行代码是monadic链接(一种用于接受一个输入的辅助函数),如果输入的标题大小写存在于上面创建的列表中,则返回1,否则返回0。

第三行代码是主链接,这是一个单子函数,它接受一个字符串并根据需要返回一个字符列表(即字符串)的列表:

ŒṖµL€=1oÇ€ṂµÐfµṡ2;€ÇÐfÇ€€S€¬SµÐḟ⁾[]jŒtƊ¹Ç?€€ - Main link: s
ŒṖ                                           - all partitions of s
  µ        µÐf                               - filter keep:
   L€=1                                      -     length €ach equals (vectorises) 1
       o                                     -     or
        ǀ                                   -     last link as a monad (is an element when title-cased)
          Ṃ                                  -     minimum 
                                             - (i.e. all partitions that are all single characters OR are strings that when title-cased are elements)
              µ              µÐḟ             - filter discard:
               ṡ2                            -     slices of length 2
                 ;€                          -     concatenate €ach
                    Ðf                       -     filter keep:
                   Ç                         -         last link as a monad (is an element when title-cased)
                      Ç€€                    -     last link as a monad for €ach for €ach
                         S€                  -     sum €ach
                           ¬                 -     logical not
                            S                -     sum
                                             - (i.e. discard any partitions that contain a run of two that joined together and title-cased ARE an element but separately NEITHER are)
                                         ?€€ - if then else for €ach (partition) for €ach (part):
                                        Ç    -     IF: last link as a monad (is an element when title-cased)
                                             -   THEN:
                                      Ɗ      -         last three links as a monad:
                                ⁾[]                      "[]"
                                   j         -           joined by:
                                    Œt       -           title case the part
                                             -   ELSE:
                                       ¹     -         the part itsef (¹ is the identity atom)

1

C ++ 11,944个 928字节

这是一段真正可怕的代码,但它应该可以工作。可能还会缩短很多。

#import<iostream>
#import<set>
using namespace std;int r,i;set<string>O;S(string&s){s[0]-=s[0]>90?32:0;if(s[1])s[1]+=s[1]<91?32:0;char*l="HHeLiBeBCNOFNeNaMgAlSiPSClArKCaScTiVCrMnFeCoNiCuZnGaGeAsSeBrKrRbSrYZrNbMoTcRuRhPdAgCdInSnSbTeIXeCsBaLaCePrNdPmSmEuGdTbDyHoErTmYbLuHfTaWReOsIrPtAuHgTlPbBiPoAtRnFrRaAcThPaUNpPuAmCmBkCfEsFmMdNoLrRfDbSgBhHsMtDsRgCnNhFlMcLvTsOg";for(r=0;*l++;)if(*l>90){if(*(l++-1)==s[0]&&*(l-1)==s[1])r=1;}else if(*(l-1)==s[0]&&!s[1])r=1;}P(set<string>*V,string s,string o,int b,int l=0,int m=0){if(!s[b])O.insert(o);else if(l)P(V,s,o,b+1);else if(V[b].size()==0)P(V,s,o+s[b],b+1);else for(auto t:V[b]){P(V,s,o+"["+t+"]",b+1,t.length()-1);if(t.length()>1&&V[b].size()==1&&V[b+1].size()>0&&!m)P(V,s,o+s[b],b+1,0,1);}}F(string s){set<string>V[s.length()];for(i=0;s[i++];){string t="";t+=s[i-1];S(t);if(r)V[i-1].insert(t);t+=s[i];S(t);if(r&&s[i])V[i-1].insert(t);}P(V,s,"",0);for(auto o:O)cout<<o<<"\n";O.clear();}

致电:

int main()
{
    F("Tin Snips");cout << "\n";
    F("Quack");cout << "\n";
    F("hehe");cout << "\n";
    F("Stack Exchange");
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.