代码Golfinator-3000


22

正如大家都知道,加入inator-3000任何名词结束使得它的方式更冷。但是还有什么可以使单词更酷呢?

给定一个ASCII字符串作为输入,输出该单词的简洁性。

计算凉爽度

有4个元素可以计算单词的清爽度。

  • 这个词本身。基本分数是大写字母的数量乘以1000
  • 结束号码。单词末尾的数字(例如burninator- 3000)被添加到基本分数中,但是如果该数字超过4个数字,则表示它们过于贪婪,因此应忽略该数字。
  • 连接器。结束编号前的空格加1000,而连字符加2000,则任何其他符号或完全没有符号无效。
  • 后缀。如果单词以结尾ator,则将最终分数加倍。如果结尾为inator,则将分数提高三倍。这些不区分大小写。

因此例如Burninator-3000可以计算如下:

1 Capital letter - Base Score: 1000
(1000(base) + 3000(number) + 2000(hyphen)) * 3(suffix) = 18000

测试用例

Burninator-3000 -> 18000
Burnator3000 -> 8000
BurNinator 100 -> 9300
BuRnInAtOr-7253 -> 42759
burn -> 0
burn- -> 0
bUrn-1 -> 3001
inator-7 -> 6021
ator 56 -> 2112
burninators 1000 -> 2000
burn_1000 -> 1000
BURNINATOR-9999 -> 65997
burninator 99999 -> 3000
burninator_99999 -> 0
Code Golfinator-3000 -> 21000
inator ator hello world-1000 -> 3000
javaiscool_99999 -> 0
hypen-ated -> 0
1000 -> 1000
-1000 -> 3000
10000 -> 0
-10000 -> 2000
BURN1N470R-3000 -> 11000

计分

这是,因此每种语言中最少的字节都是成功的


3
测试用例:(inator ator hello world-1000或类似的)
TheLethalCoder

@TheLethalCoder添加了
Skidsdev

@ Mr.Xcoder不确定它尚未涵盖的内容,但很有趣,所以我还是要添加它
Skidsdev

好的,现在是一个有用的测试用例:9028-7282(仅数字)
Xcoder先生17年

1
是不是burninator 999993000没有1000?空格加1000,以inator结尾的三倍。
TheLethalCoder

Answers:


13

的JavaScript(ES6),138个 133 128字节

s=>(([,a,b,,c,d]=s.match(/((in)?ator)?((\D?)(\d+))?$/i),s.split(/[A-Z]/).length-1+(c=='-'?2:c==' '))*1e3+(d<1e4&&+d))*(b?3:2-!a)

怎么样?

大写字母的数量由下式给出:

s.split(/[A-Z]/).length - 1

其他所有条件均由以下正则表达式的结果推导得出,该正则表达式分为4个变量:

/((in)?ator)?((\D?)(\d+))?$/i     criterion  | code                    | outcome
 \_________/  \___/\___/          -----------+-------------------------+----------
      a         c    d            end number | d < 1e4 && +d           | 0 or +d
  \___/                           connector  | c == '-' ? 2 : c == ' ' | 0, 1 or 2
    b                             suffix     | * (b ? 3 : 2 - !a)      | 1, 2 or 3

测试用例


7

香草C,447字节

(为便于阅读而包装)

#include<stdio.h>
int main(int c,char**a){char*v=a[1];while(*v++);int m=0,i=
0,bn=0,s=0,b=0,mul=1;char l[256],*lp=l;v--;while(a[1]<v--)
{if(!m){i=!i?1:(i*10),('0'<=*v&&*v<='9')?(bn=bn+i*(*v-'0')
):m++;}if(m==1){(*v=='-'||*v==' ')?(s+=bn?((*v=='-')?2000:
1000):0,v--):0,m++;}if(m==2){(*v>='A'&&*v<='Z')?(s+=1000,*
v+=32):0,*lp++=*v;}}s+=(bn<10000)?bn:0;for(i=0;i<lp-l;i++)
{if(*(l+i)=="rotani"[i]){mul=(i==5)?3:((i==3)?2:mul);}}s*=
mul;printf("%d\n",s);}

甚至星期五的心情!

(我没有使用任何工具来放置代码。实际上,我真的很幸运,我没有进行任何预先计算就选择了正确的列宽。它甚至可以编译!)

香草C,789字节

#include<stdio.h>
int main(int c,     char**a){char       *v=a[1];while      (*v++);int m=0,i=
        0,bn=0,     s=0,     b=0,       mul=1   ;char      l[256],    *lp=l;
          v--;      while  (a[1]<       v--)    {if(!      m){i=!i    ?1:(i*
          10),      ('0'   <=*v&&       *v<=    '9')?      (bn=bn+    i*(*v-
'0')):m++;}if(      m==1)  {(*v==       '-'||    *v==      ' ')?(s    +=bn?(
(*v=='-')?2000      :1000) :0,v--       ):0,m    ++;}      if(m==2     ){(*v
       >='A'&&      *v<=    'Z')?       (s+=    1000,      *v+=32)    :0,*lp
          ++=*      v;}}s  +=(bn<       10000   )?bn:      0;for(i     =0;i<
         lp-l;      i++){  if(*(l       +i)==   "rot"      "ani"[i]   ){mul=
(i==5) ?3: ((i      ==3)?2:mul);}       } s *= mul;        printf("%d\n",s);}

原始代码:

#include <stdio.h>
#include <math.h>

int main(int argc, char** argv) {
    char *v = argv[1];
    while(*v++);
    int m=0,i=-1;
    int bonus_number=0;
    int score=0;
    int b=0;
    int mul=1;
    char letters[256];
    char* lp=letters;
    v--;
    while(argv[1]<v--) {
        printf(" * %c %x\n", *v, *v);
        if (m == 0) {
            if ('0'<=*v&&*v<='9') {
                bonus_number=bonus_number+powl(10,++i)*(*v-'0');
                printf("Digit, bonus is now %d\n", bonus_number);
            } else {
                m++;
            }
        }
        if (m == 1) {
            if (*v=='-'||*v==' ') {
                printf("Dash/space\n");
                if (bonus_number) score += (*v=='-') ? 2000 : 1000;
                v--;
            }
            m++;
        }
        if (m == 2) {
            if(*v>='A'&&*v<='Z') {
                printf("Upper letter\n");
                score += 1000;
                *v += 32;
            }
            *lp++ = *v;
        }
    }
    score += (bonus_number<10000)?bonus_number:0;
    for(i=0;i<lp-letters;i++) {
        // printf("%d: %c\n\n", i, *(letters+i));
        if (*(letters+i) == "rotani"[i]) {
            if (i == 3) {
                printf("2x!\n");
                mul = 2;
            }
            if (i == 5) {
                printf("3x!\n");
                mul = 3;
            }
        }
    }
    score *= mul;
    printf("Score: \n%d\n", score);
}

第一次最小化后:

#include <stdio.h>

int main(int c, char** a) {
    char *v = a[1];while(*v++);
    int m=0,i=0,bn=0,s=0,b=0,mul=1;
    char l[256],*lp=l;
    v--;
    while(a[1]<v--) {
        if (!m) {
            i=!i?1:(i*10),
            ('0'<=*v&&*v<='9') ? (bn=bn+i*(*v-'0')) : m++;
        }
        if (m == 1) {
            (*v=='-'||*v==' ') ? (s += bn ? ((*v=='-') ? 2000 : 1000) : 0, v--):0,m++;
        }
        if (m == 2) {
            (*v>='A'&&*v<='Z') ? (s += 1000, *v += 32):0,
            *lp++ = *v;
        }
    }
    s += (bn<10000)?bn:0;
    for(i=0;i<lp-l;i++) {
        if (*(l+i) == "rotani"[i]) {
            mul=(i==5)?3:((i==3)?2:mul);
        }
    }
    s *= mul;
    printf("%d\n", s);
}

测试用例

#!/usr/bin/env python3
import subprocess

TESTCASES = '''
Burninator-3000 -> 18000
Burnator3000 -> 8000
BurNinator 100 -> 9300
BuRnInAtOr-7253 -> 42759
burn -> 0
burn- -> 0
bUrn-1 -> 3001
inator-7 -> 6021
ator 56 -> 2112
burninators 1000 -> 2000
burn_1000 -> 1000
BURNINATOR-9999 -> 65997
burninator 99999 -> 3000
burninator_99999 -> 0
Code Golfinator-3000 -> 21000
inator ator hello world-1000 -> 3000
javaiscool_99999 -> 0
hypen-ated -> 0
1000 -> 1000
-1000 -> 3000
10000 -> 0
-10000 -> 2000
BURN1N470R-3000 -> 11000
'''

TESTCASES = dict(map(lambda x: x.split(' -> '), filter(None, TESTCASES.split('\n'))))


def process(arg):
    return subprocess.Popen(['./a.out', arg], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0].strip().split(b'\n')[-1].decode('utf-8')


for key, value in TESTCASES.items():
    assert value == process(key), '"{}" should yield {}, but got {}'.format(
        key, value, process(key)
    )

2

C#,322个 317字节

namespace System.Linq{s=>{var m=Text.RegularExpressions.Regex.Match(s,"(.*?)([- ])?(\\d+)$");string t=m.Groups[1].Value.ToLower(),c=m.Groups[2].Value,n=m.Groups[3].Value;return(s.Count(char.IsUpper)*1000+(n.Length>4|n==""?0:int.Parse(n))+(c==" "?1000:c=="-"?2000:0))*(t.EndsWith("inator")?3:t.EndsWith("ator")?2:1);}}

在线尝试!

完整/格式化版本:

namespace System.Linq
{
    class P
    {
        static void Main()
        {
            Func<string, int> f = s =>
            {
                var m = Text.RegularExpressions.Regex.Match(s, "(.*?)([- ])?(\\d+)$");
                string t = m.Groups[1].Value.ToLower(), c = m.Groups[2].Value, n = m.Groups[3].Value;

                return (
                           s.Count(char.IsUpper) * 1000 +
                           (n.Length > 4 | n == "" ? 0 : int.Parse(n)) +
                           (c == " " ? 1000 : c == "-" ? 2000 : 0)
                        )
                        * (t.EndsWith("inator") ? 3 : t.EndsWith("ator") ? 2 : 1);
            };

            string[] testCases =
            {
                "Burninator-3000", "Burnator3000", "BurNinator 100", "BuRnInAtOr-7253",
                "burn", "burn-", "bUrn-1", "inator-7", "ator 56", "burninators 1000",
                "burn_1000", "BURNINATOR-9999", "burninator 99999", "burninator_99999",
                "Code Golfinator-3000", "inator ator hello world-1000", "javaiscool_99999",
                "hypen-ated", "1000", "-1000", "10000", "-10000"
            };

            foreach (string testCase in testCases)
            {
                Console.WriteLine($"{testCase} -> {f(testCase)}");
            }

            Console.ReadLine();
        }
    }
}

由于只允许一个lambda,所以将正则表达式拆分成自己的方法现在要长4个字节(除非我错过了什么)。它以321字节输入:

namespace System.Linq{string m(string s,int n)=>Text.RegularExpressions.Regex.Match(s,"(.*?)([- ])?(\\d+)$").Groups[1].Value.ToLower();s=>{string t=m(s,1),c=m(s,2),n=m(s,3);return(s.Count(char.IsUpper)*1000+(n.Length>4|n==""?0: int.Parse(n))+(c==" "?1000:c=="-"?2000:0))*(t.EndsWith("inator")?3:t.EndsWith("ator")?2:1);}}

我不知道在上一个meta帖子之后这是否合法,但您确实认为是合法的。这样的其他功能会不会b=c=>d=>c.Groups[d].Value节省一点时间?
LiefdeWen

@LiefdeWen在上一则meta帖子之后,我相信我必须将其声明为完整方法。我可以做,string m(string s,int n)=>Text.RegularExpressions.Regex.Match(s,"(.*?)([- ])?(\\d+)$").Groups[n].Value.ToLower();string t=m(s,1),c=m(s,2),n=m(s,3);但是我相信那会使它增加3个字节。
TheLethalCoder

2

Perl,108个字节

107字节代码+ 1 for -p

$\+=1e3*y/A-Z//;s/( |-)?(\d+)$//;$\+=$2*($2<1e4);$\+={$",1e3,"-",2e3}->{$1};s/(in)?ator$//i&&($\*=$1?3:2)}{

在线尝试!


0

PHP> = 7.1,165字节

preg_match("#((in)?ator)?((\D)?(\d+))?$#i",$argn,$m);[,$a,$i,,$c,$d]=$m;echo($d*($d<1e4)+(preg_match_all("#[A-Z]#",$argn)+($c!="-"?$c==" "?:0:2))*1e3)*($a?$i?3:2:1);

测试用例

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.