繁琐的7条规则


11

破败不堪

创建一个程序,该程序生成带有随机数的随机长度数组,然后应用一系列更改数组的规则。应用规则后,除非另有说明,否则打印数组的总和

阵列设定

数组的长度必须在5到10之间(包括1和20)并且必须是1到20之间的随机整数。每个数组长度应具有相等的发生概率,每个整数应具有相等的概率被每个元素拾取。

7条规则

规则应像按顺序应用一样起作用(例如:规则1在规则2之前执行),并且仅应用一次。对于规则应用证明,必须在每个规则应用之后且应用任何规则之前将阵列打印到控制台一次。

  1. 如果数组包含7,则从每个元素中减去1
  2. 如果应用了规则1,并且数组现在包含0,则向每个元素添加1
  3. 如果数组包含13,则从数组中排除13及其后面的所有元素
  4. 如果数组包含2,则排除所有奇数
  5. 如果数组包含20,并且第三个元素为偶数,则返回20作为总和,然后终止。如果存在20并且第三个元素为奇数,则返回数组长度的20倍作为总和,然后终止。
  6. 如果总和大于50,请重复删除最后一个元素,直到小于或等于50
  7. 如果数组包含16,则用十进制和十六进制打印总和。

这是一个初始数组,

[20, 2, 5, 7, 14, 8]

可以应用规则1:

[19, 1, 4, 6, 13, 7]

接下来应用规则3:

[19, 1, 4, 6]

不需要其他规则,因此程序返回30作为总和。

笔记

  • 我不是一位经验丰富的代码高尔夫球手,尽管我可以说我的个人记录是369个字节的 Python 3 。
  • 规则不必按顺序实际应用,而必须像它们一样应用。

1
“随机”必须有多随机?
HyperNeutrino

1
@HyperNeutrino可以根据需要设置为sudo随机,但不能挑战投掷。允许重复。
Graviton

您将如何检查我们是否实际应用了规则?我只能生成一个小于50的随机数,从技术上讲,它仍然不会破坏任何规则,我可以说它“遵循”了这些规则。编辑:我现在知道这将行不通,但人们将找到规避随机规则的方法。您要防止这种情况吗?
clismique

1
目前,选择两个数组之一[3 3 3 3 4 3][4 4 3 4 4],每个数组的概率为50%,这与“数组设置”下的内容一致。这样我19每次都能输出?(当然,我真正认为的是,需要澄清“随机”的定义。)
Greg Martin

2
If the array contains a 20, and the third element is even/odd,如果此步骤中数组少于3个元素怎么办?
Emigna

Answers:


8

Python 3中,294个 301 287 356字节

import random as r
r=r.randint
k=[r(i)+1for i in[20]*r(5,11)]
p=print
if 7in k:k=[i-1for i in k]
p(k)
if 0in k:k=[i+1for i in k]
p(k)
i=k.find(13)
if not~i:k=k[:i]
p(k)
if 2in k:k=[i for i in k if~i%2]
p(k)
a=0
z=len(k)>2and k[2]%2
if 20in k:a=20*len(k)**z
if~z:p(k)
while sum(k)>50:k=k[:-1]
if~z:p(k)
if a:p(a)
else:a=sum(k);p(a,hex(a)*(16in k))
if~z:p(k)

我不知道您将如何防止人们规避规则,但这是使用指定的过程。

+7个字节;感谢@YamB节省了一些字节;添加了更多内容以修复先前的错误。
-14个字节感谢@RootTwo和我自己,还纠正了错误。
+83字节; 因为OP不断更改规则,所以这变得非常可怕。-由于@ZacharyT,一些字节


所有人都向我退房,感谢您的诚实。
Graviton

您可以通过导入'randint as r'并将'if in 7in k and 1not in k:k = [i-1 ...''更改为'if 7in k:k = [i + 1-int(1in k )
...'– Graviton

初始化k时,不需要i的值,因此可以使用保存6个字节k=[r(1,20)for _ in'-'*r(5,11)]。您可以使用保存另一个字节k=[i+~-(1in k)*(7in k)for i in k]的规则1和2
RootTwo

1
@ Notts90在我编写此挑战后,规则已更改。当我上电脑时,我会修复它。谢谢。
HyperNeutrino

在第五和第七行,后面没有多余的空格1,您可以print将第二行和第三行的最后一行更改为p。而且您...尚未更新字节数。
扎卡里


6

使用Javascript(ES6),344 342 340 342 335 331 333 313 311 305 298 297 290 289 283 279字节

头晕!最终击败Arnauld 并列

本次交流 *在挑战的意见,并经过深思熟虑后,我决定使用new Date作为种子的随机数发生器来代替Math.random()。这样做意味着数组中的所有整数将具有相同的值。

_=>(l=alert,r=new Date,l(a=[...Array(r%6+5)].map(x=>r%20+1)),i=v=>~a.indexOf(v),i(7)&&l(a=a.map(x=>--x)),i(0)&&l(a=a.map(x=>++x)),i(13)&&l(a=a.slice(0,~i(13))),i(2)&&l(a=a.filter(x=>x%2)),i(t=20)?a[2]%2?t*a.length:t:l(a=a.filter(x=>s+x<51?s+=x:0,s=0))|i(16)?[s,s.toString(16)]:s)

尝试一下

f=
_=>(l=alert,r=new Date,l(a=[...Array(r%6+5)].map(x=>r%20+1)),i=v=>~a.indexOf(v),i(7)&&l(a=a.map(x=>--x)),i(0)&&l(a=a.map(x=>++x)),i(13)&&l(a=a.slice(0,~i(13))),i(2)&&l(a=a.filter(x=>x%2)),i(t=20)?a[2]%2?t*a.length:t:l(a=a.filter(x=>s+x<51?s+=x:0,s=0))|i(16)?[s,s.toString(16)]:s)
alert(f())

  • 通过仅记录规则6中的弹出元素节省了2个字节。
  • 替换Array(x).fill()为节省了2个字节[...Array(x)]
  • 添加了2个字节,因为我对规则5感到困惑!
  • 保存了7个字节来修复我为修复先前的混乱所做的混乱!
  • 保存3感谢字节阿尔诺帮助我治愈上规则2 brainfart并通过更换保存额外字节+1~
  • 增加2个字节,确保 0返回一个空数组。
  • 通过最终弄清楚如何消除雀斑,节省了20个字节 while循环。
  • 通过,使用a 替换最后2条语句之间的|并删除了,节省了2个字节()
  • 替换为console.log,节省了6个字节alert
  • 通过改进的快捷方式节省了7个字节a.includes()
  • 通过编辑规则3的执行,节省了1个字节。
  • 通过丢弃includes()和仅使用indexOf()整个字节节省了7个字节。
  • 通过将s变量的初始声明移到不需要逗号的位置来节省1个字节。
  • 替换为Math.random(),节省了6个字节new Date
  • 通过删除随机数(现在是多余的)舍入,节省了4个字节。

可读和可测试的版本

  • 在代码中添加了换行符和注释
  • 用过的 console.log代替alert您的理智!(最好在浏览器的控制台中查看)
  • 将当前规则编号添加到输出中。
  • 注释掉了随机数组的生成,以允许通过输入逗号分隔的数字列表进行测试。


*截图,以防被删除:


4

C(gcc)621 619 593 585 570 562 557 552 529 517 500 482 461 444 442 441 438字节

这里需要打很多高尔夫球...解决了一个错误,该错误会使列表中的每16个十六进制打印一次。

特别感谢ZacharyT在高尔夫方面的帮助

#define R r[i]
#define P printf("%d "
#define L for(d=i=0;i<l;i++)
d,i,j,l,r[11];p(i){L P,R);puts("");}main(){srand(time(0));for(l=5+rand()%5;i<l;R=1+rand()%20,i++);for(p();i--;)if(R==7){L--R;j=i=1;}for(p();j&&i<l;i++)if(!R){L++R;j=i=l;}p();L if(R==13)l=i;p();L if(R==2)for(d=1;d;)L if(R&1)for(d=1,--l;i<=l;i++)R=r[i+1];p();L if(R==20)return P,r[3]&1?20*l:20);for(j=i=0;i<l&&j+R<51;j+=R,i++);l=i;p();P,j);L if(R==16)printf("0x%x",j,i=l);}

在线尝试!


1
即使您仍然可以打高尔夫球,但您的Java答案已经比它低了1个字节。XD让我们看看我是否可以打高尔夫球以击败您目前的成绩。;)
Kevin Cruijssen

好吧,找到了-3个字节的东西; p
Kevin Cruijssen

如果您可以打高尔夫球,那么您可能可以在其中找到“划掉的444仍然是444”!:D
HyperNeutrino

@HyperNeutrino打了另外2个字节
cleblanc '17

好极了!干得好:D
HyperNeutrino

3

的JavaScript(ES6),296个 295 290 289字节

完整的程序,用于将初始数组,中间结果和最终总数记录到控制台。

f="z=[...Array(5+6j)]Z1+20jw7`-1w0`+1w13_qi+1<-kw2_qn&1^1w20_z[2]&1?a.length*20:20);else{q(s+=n)<51,s=0w16_b.toString(16_;b)}zconsole.log(aw_;if(k=~a.indexOf(v((n,i)=>qz=a.filtervj*Math.random()|0bz.reducevn+i,0)`_z=aZn_))Z.mapv";for(g of "Z_`bjqvwz")e=f.split(g),f=e.join(e.pop());eval(f)

这个怎么运作

这是使用此JS压缩程序压缩的。

分解:

  • 打包的字符串:226225字节
  • 开箱码:69 64字节

以下是原始源代码,其中包含一些其他空格和换行符,以提高可读性。它不是采用标准的高尔夫技巧,而是以产生尽可能多的重复字符串的方式编写,以取悦包装工。例如,if(k=~a.indexOf(N))尽管k仅在第三条规则中使用,但语法在各处都重复。

console.log(a=[...Array(5+6*Math.random()|0)].map((n,i)=>1+20*Math.random()|0));
if(k=~a.indexOf(7))
  console.log(a=a.map((n,i)=>n-1));
if(k=~a.indexOf(0))
  console.log(a=a.map((n,i)=>n+1));
if(k=~a.indexOf(13))
  console.log(a=a.filter((n,i)=>i+1<-k));
if(k=~a.indexOf(2))
  console.log(a=a.filter((n,i)=>n&1^1));
if(k=~a.indexOf(20))
  console.log(a[2]&1?20*a.length:20);
else {
  console.log(a=a.filter((n,i)=>(s+=n)<51,s=0));
  if(k=~a.indexOf(16))
    console.log(a.reduce((n,i)=>n+i,0).toString(16));
  console.log(a.reduce((n,i)=>n+i,0))
}

开箱方法

原始的拆包代码为:

f="packed_string";for(i in g="ABCDEFGHI")e=f.split(g[i]),f=e.join(e.pop());eval(f)

以下所有ES6变体的大小都完全相同:

eval([..."ABCDEFGHI"].reduce((f,g)=>(e=f.split(g)).join(e.pop()),"packed_string"))
[..."ABCDEFGHI"].map(g=>f=(e=f.split(g)).join(e.pop()),f="packed_string")&&eval(f)
eval([..."ABCDEFGHI"].map(g=>f=(e=f.split(g)).join(e.pop()),f="packed_string")[8])

到目前为止,我发现减少几个字节的唯一方法是使用for ... of

f="packed_string";for(g of "ABCDEFGHI")e=f.split(g),f=e.join(e.pop());eval(f)

在电话上阅读此内容可能不对,但在您的解压缩代码中,无论规则1是否适用,似乎都在应用规则2。
毛茸茸的

1
@Shaggy是的。但是除非触发规则1,否则您将不会获得零。
Arnauld

天哪!当然!伙计,在这次挑战中,我的脑筋一直很可笑:(
Shaggy

1
@Shaggy不幸的是没有。但是,我们可以用保存一个字节n&1^1(它根本不会打包,但比短一字节!(n&1))。我曾经考虑过这一点,却忘记了将其包括在内。
Arnauld

1
@蓬松的啊!不错的工作!
Arnauld

2

首先尝试打高尔夫球!

已经被其他javascripter击败!该死的!我会改善!!!=)

使用Javascript - > 550个 402字节

绝对可以改善。现在压缩:

f="ba=[];bl;yz5+5`^=0;i<y;i++)a[i]z20+1|~7j-1|~0j+1|}}~13_l=indexOf(13`ql,y-l-Y_^ in a)if(a[i]%2)qi,Y0)&&(!a[3]%2_k'20'`throw new Error(`}do{l=Vreduce((X,num)=>X+num`ifW)qy-1,1`}whileW|kl`~16))kl.toString(16)`~if(Vincludes(|`ka`z=Zound(Zandom()*yVlengthqVsplice(kalert(j_Vmap((i)=>ibvar `);_)){^for(biZMath.rY1|}~2XtotalW(l>50Va.";for(i in g="VWXYZ^_`bjkqyz|~")e=f.split(g[i]),f=e.join(e.pop());eval(f)

原产地:

var a=[];var l;a.length=Math.round(Math.random()*5+5);for(var i=0;i<a.length;i++)a[i]=Math.round(Math.random()*20+1);alert(a);if(a.includes(7)){a.map((i)=>i-1);alert(a);if(a.includes(0)){a.map((i)=>i+1);alert(a);}}if(a.includes(13)){l=indexOf(13);a.splice(l,a.length-l-1);alert(a);}if(a.includes(2)){for(var i in a)if(a[i]%2)a.splice(i,1);alert(a);}if(a.includes(20)&&(!a[3]%2)){alert('20');throw new Error();}do{l=a.reduce((total,num)=>total+num);if(l>50)a.splice(a.length-1,1);}while(l>50);alert(a);alert(l);if(a.includes(16))alert(l.toString(16));

2

Java 7中,622个 619 618字节

import java.util.*;void c(){Random r=new Random();List<Long>l=new ArrayList();int i=0,c=r.nextInt(6)+5;for(;i++<c;l.add(r.nextInt(20)+1L));p(l);if(l.contains(7)){for(i=0;i<c;l.set(i,l.get(i++)-1));p(l);}if(l.contains(0)){for(i=0;i<c;l.set(i,l.get(i++)+1));p(l);}if((i=l.indexOf(13))>=0){for(;i<l.size();l.remove(i));p(l);}if(l.contains(2)){for(i=0;i<l.size();)if(l.get(i)%2>0)l.remove(l.get(i));else i++;p(l);}if(l.contains(20)){p(20*(l.get(2)%2<1?1:l.size()));return;}i=0;for(long x:l)i+=x;for(;i>50;)i-=l.remove(l.size()-1);p(l);if(l.contains(16))p(Byte.valueOf(i+"",16));p(i);}<T>void p(T t){System.out.println(t);}

-1字节感谢@Poke

说明:

import java.util.*;                      // Imports used for List, ArrayList and Random

void c(){                                // Main method
  Random r=new Random();                 //  Random generator
  List<Long>l=new ArrayList();           //  The list
  int i=0,                               //  Temp index we use multiple times
      q=r.nextInt(6)+5;                  //  Random size of the list (5-10)
  for(;i++<q;l.add(r.nextInt(20)+1L));   //  Fill the list with random long-integers (1-20)
  p(l);                                  //  Print the initial list
  if(l.contains(7)){                     //  If the list contains a 7
    for(i=0;i<c;l.set(i,l.get(i++)-1));  //   Decrease each number in the list by 1
    p(l);                                //   And then print the list again
  }                                      //  End of if
  if(l.contains(0)){                     //  If the list now contains a 0
    for(i=0;i<c;l.set(i,l.get(i++)+1));  //   Increase each number in the list by 1
    p(l);                                //   And then print the list again
  }                                      //  End of if
  if((i=l.indexOf(13))>=0){              //  If the list contains a 13 (and save it's index in `i` at the same time)
    for(;i<l.size();l.remove(i));        //   Remove everything from that index and onward
    p(l);                                //   And then print the list again
  }                                      //  End of if
  if(l.contains(2)){                     //  If the list now contains a 2
    for(i=0;i<l.size();)                 //   Loop over the list
      if(l.get(i)%2>0)                   //    If the current list item is odd
        l.remove(l.get(i));              //     Remove it
      else                               //    If it's even instead
        i++;                             //     Go to the next item
                                         //   End of loop (implicit / single-line body)
    p(l);                                //   And print the list again
  }                                      //  End of if
  if(l.contains(20)){                    //  If the list now contains a 20
    p(20*(l.get(2)%2<1?1:l.size()));     //   Print 20 if the third item in the list is odd, or 20*size if it's even instead
    return;                              //   And then terminate the method
  }                                      //  End of if
  i=0;                                   //  Reset `i` to 0
  for(long x:l)i+=x;                     //  And calculate the total sum of the list (stored in `i`)
  for(;i>50;)                            //  Loop as long as this list's sum is above 50
    i-=l.remove(l.size()-1);             //   Subtract the last item from this sum, and then remove it from the list
                                         //  End of loop (implicit / single line body)
  p(l);                                  //  And print the list again
  if(l.contains(16))                     //  If the list now contains a 16
    p(Byte.valueOf(i+"",16));            //   Print the sum (still stored in `i`) as hexadecimal
                                         //  End of if (implicit / single-line body)
  p(i);                                  //  And print the sum as integer either way
}                                        // End of main method

<T>void p(T t){                          // Separate method with a generic parameter
  System.out.println(t);                 //  Print the given parameter including a new-line
}                                        // End of separated method

样本输出:
不会输出样本输出后面的注释,但我将其添加为澄清。

在这里尝试。

[17, 5, 3, 1, 16, 17, 11, 7, 13]   // Initial print (size 9)
[16, 4, 2, 0, 15, 16, 10, 6, 12]   // Rule 1 (contains a 7)
[17, 5, 3, 1, 16, 17, 11, 7, 13]   // Rule 2 (contains a 0)
[17, 5, 3, 1, 16, 17, 11, 7]       // Rule 3 (contains a 13)
[17, 5, 3, 1, 16]                  // Rule 6 (sum must be <= 50)
66                                 // Rule 7 (contains a 16 -> print as Hexadecimal)
42                                 // Print sum as integer

[4, 18, 17, 12, 11, 8]             // Initial print (size 6)
[4, 18, 17]                        // Rule 6 (sum must be <= 50)
39                                 // Print sum as integer

[4, 14, 6, 14, 7, 20, 2, 2]        // Initial print (size 8)
[3, 13, 5, 13, 6, 19, 1, 1]        // Rule 1 (contains a 7)
[3]                                // Rule 3 (contains a 13)
[3]                                // Print is always done after rule 6
3                                  // Print sum as integer

1
我现在
只有

@cleblanc我看到你现在已经降到444了。我无法与Java竞争。:)(有趣的是,与其他所有答案相比,444几乎无法赢得胜利。)
Kevin Cruijssen

我知道,即使像05AB1E这样的高尔夫语言也差不多100个字节长。这个挑战是痛苦的。
cleblanc

您可以保留列表的通用名称List a = new ArrayList()吗?可能节省一些字节。不过,您可能需要在进行实际算术时添加类型转换。如果不是,Long则小于Integer

@Poke有了一个通用的List我必须使用(int)投五次,以及改变for-each循环,从intObject,并添加第六演员。至于Long:谢谢,可以节省1个字节:)(还是要改变的,从每一个intlong,并r.nextInt(20)+1r.nextInt(20)+1L)。
凯文·克鲁伊森

2

Ruby 2.4,260个字节

需要Ruby 2.4 Enumerable#sum

p a=(1..s=5+rand(5)).map{1+rand(19)}
a.map!{|i|i-1}if a.index 7
p a
a.map!{|i|i+1}if a.index 0
p a
a.pop s-(a.index(13)||s)
p a
a.reject! &:odd?if a.index 2
p a
a.index(20)?p(20*[1,s][(a[2]||1)%2]):((a.pop;p a)while a.sum>50
p m=a.sum;puts"%x"%m if a.index 16)

在线尝试!(repl.it和tio.run都不支持Ruby 2.4,因此此在线版本替换suminject(:+),具有相同的行为。)


1

R(3.3.1),325字节

非常幼稚的实现;我想我可以把它缩短一些。

s=sample(1:20,sample(5:10,1));print(s);if(7%in%s){s=s-1;print(s);if(0%in%s)s=s+1;print(s)};if(13%in%s){s=s[1:(which(s==13)-1)];print(s)};if(2%in%s){s=s[!(s%%2)];print(s)};if(20%in%s){if(s[3]%%2){20*length(s);print(s)}else{20;print(s)}};while(sum(s)>50){s=s[-length(s)];print(s)};if(16%in%s){print(as.hexmode(sum(s)))};sum(s)

1

PowerShell中525 413字节

filter a{"$a"};0..(4..9|random)|%{$a+=@(1..20|random)};a;if(7-in$a){$a=($a|%{$_-1});a;if(0-in$a){$a=($a|%{$_+1});a}}$b=$a;$a=@();foreach($z in $b){if($z-ne13){$a+=@($z)}else{a;break}}if(2-in$a){$a=($a|?{$_%2-eq0});a}if(20-in$a){if($a[2]%2){20*$a.count;exit}else{20;exit}}while(($h=$a-join'+'|iex)-gt50){$a=$a[0..($a.count-2)];a}if(16-in$a){$l=0..9+'a b c d e f'-split' ';$q=[math]::floor($h/16);"$q"+$l[$h%16]};$h

在线尝试!

我想尝试一下它,尽管我想我在这里还不能解决问题:PI一直在尝试降低它的速度,我敢肯定用更少的字节数是可能的。找到了一种更好的十六进制方法,但可能仍会改进。

不得不投 $a多次转换为字符串,最好为此创建一个过滤器...

我错过了很多简单的高尔夫运动,例如括号和空格。那里可能还会有一些吗?

稍微容易阅读的代码:

filter a{"$a"};0..(4..9|random)|%{$a+=@(1..20|random)};a;
if(7-in$a){$a=($a|%{$_-1});a;if(0-in$a){$a=($a|%{$_+1});a}}
$b=$a;$a=@();foreach($z in $b){if($z-ne13){$a+=@($z)}else{a;break}}
if(2-in$a){$a=($a|?{$_%2-eq0});a}
if(20-in$a){if($a[2]%2){20*$a.count;exit}else{20;exit}}
while(($h=$a-join'+'|iex)-gt50){$a=$a[0..($a.count-2)];a}
if(16-in$a){$l=0..9+'a b c d e f'-split' ';$q=[math]::floor($h/16);"$q"+$l[$h%16]};$h

0

MATLAB,275个字节

我最初计划的是单线八度音阶答案,但要求所有已应用规则的输出阻碍了我的计划。取而代之的是一个相当简单的MATLAB答案,其中包含一些有趣的优化,例如,使用规则6 cumsum代替显而易见while的变量。但是,if如果不应用规则,则会在s上浪费大量字节数以防止输出。

A=randi(20,1,randi(6)+4)
if any(A==7)
A=A-1
if any(~A)
A=A+1
end;end
q=find(A==13,1);if q
A=A(1:q-1)
end
if any(A==2)
A=A(2:2:end)
end
if any(A==20)
if mod(A(3),2)
20*length(A)
else
20
end;return;end
q=cumsum(A)<51;if any(~q)
A=A(q)
end
q=sum(A)
if(any(A==16))
dec2hex(q)
end

在线尝试!


0

Scala 587字节一个内衬

import scala.util.Random;object A{def main(args:Array[String])={val s=5+Random.nextInt(6);var a=new Array[Int](s);for(i<-1 to s){a(i-1)=1+Random.nextInt(20)};p(a);if(a.contains(7)&& !a.contains(1)){a.map(a=>a-1);p(a)};if(a.contains(13)){if(a(0)==13)a=new Array[Int](0)else a=a.slice(0,a.indexOf(13));p(a)};if(a.contains(2)){a=a.filter(pred=>pred%2==0);p(a)};if(a.contains(20)){if(a(2)%2==0)println(20)else println(20*a.length)}else{while(a.sum>50)a=a.dropRight(1);val u=a.sum;if(a.contains(16))println(Integer.toHexString(u));println(u)}};def p[T](a: Array[T])=println(a.mkString(","))}

Scala,原为763字节

import scala.util.Random
object TA {
  def main(args:Array[String])={
    val s=5+Random.nextInt(6)
    var a=new Array[Int](s)
    for (i<-1 to s)
      a(i-1)=1+Random.nextInt(20)
    p(a)
    if(a.contains(7) && !a.contains(1)){
      a.map(a=>a-1)
      p(a)
    }
    if(a.contains(13)){
      if (a(0)==13) a=new Array[Int](0) else a=a.slice(0,a.indexOf(13))
      p(a)
    }
   if(a.contains(2)){
      a=a.filter(pred=>pred%2== 0)
      p(a)
    }
    if(a.contains(20)){
      if (a(2)%2==0) println(20) else println(20*a.length)
    }else{
      while(a.sum>50)
        a=a.dropRight(1)
      val u =a.sum
      if (a.contains(16)) println(Integer.toHexString(u))
      println(u)
    }
  }
  def p[T](a: Array[T])={
    println(a.mkString(","))
  }
}

由于这是一个代码高尔夫球问题,因此我们的确要求您至少进行一些简单的练习,例如删除不必要的空格。
Ad Hoc Garf Hunter'Apr

我添加了一行低字节版本
Saideep Sambaraju '17

我不知道Scala,但是a: Array[T]需要空格吗?您在中没有空格args:Array[String],这就是导致我查询的原因。
扎卡里

不,我想我错过了。
Saideep Sambaraju

0

MATLAB,228个 241bytes

a=randi(20,1,randi(6)+4)
b=@any; 
a=a-b(a==7)
a=a+b(a==0)
a(find(a==13,1):end)=[]
a(and(mod(a,2),b(a==2)))=[]
if b(a==20)
a=[a 0 0 0];
s=20*(1+mod(a(3),1)*(numel(a)-4))
else
a(cumsum(a)>50)=[]
s=sum(a)
if b(a==16)
h=['0x' dec2hex(s)]
end
end

这将按顺序应用所有规则,并在每个步骤之后打印数组值。

如果结果的元素数量少于三个,则程序将在规则5上崩溃。当前没有什么可以说的,如果没有第三个元素,那该怎么办,所以我假设崩溃是可以接受的。如果少于3个元素并且一个或多个为20,则程序现在将打印20。

有趣的是,无论是否执行了步骤1,都可以应用步骤2。这是因为输入数组中永远不会有0,这意味着如果数组中有任何0,则它必须是由于发生了步骤1而导致的。

即使没有进行任何更改,所有规则也会依次应用,直到5个。结果,该数组将在开始时打印,然后在每个步骤之后直到5。在第5步之后,您将获得总和(如果已应用),或者直到第6步之后才输出。a可以在之后添加 else语句可确保在第5步之后以2字节为代价打印数组值。


我还想提一提,直到写完这篇文章,我才看其他答案。现在,我看到了另一个MATLAB答案,其中有一些相似之处-都是巧合。


0

Python 3, 3,297293289,278个字节

正如Arnauld所指出的,除非应用了规则1,否则您将不会获得0,该规则可以节省缩进。感谢所有也提出建议的人。

from random import*
p=print
a=b=sample(range(1,20),randint(5,10))
p(a)
if 7in a:a=[i-1for i in a];p(a)
if 0in a:a=b;p(a)
if 13in a:a=a[:a.index(13)];p(a)
if 2in a:a=[i for i in a if~i%2];p(a)
if 20in a and~a[2]%2:a=[20]
while sum(a)>50:a=a[:-1]
b=sum(a)
p(b)
if 16in a:p(hex(b))

在线尝试


我认为您在and和之间不需要空格~
扎卡里

我相信from random import* a=b=sample(range(1,20),randint(5,10))可以节省一些字节,因为您可以删除第2行
。– nocturama

0

Perl 6,246个字节

my&f={.grep($^a)};my&s=->{.say};$_=[(1..20).pick xx(5..10).pick];s;$_»--
if f 7;s;$_»++ if f 0;s;.splice(.first(13):k//+$_);s;$_=[f*%%2]if f 2;
s;say(20*(.[2]%%2||$_)),exit if $_>2&&f 20;s;.pop while
.sum>49;$/=f 16;$_=.sum;s;.base(16).say if $/

取消高尔夫:

my &f = { .grep($^a) };  # Helper function: search $_ for something
my &s = -> { .say };     # Helper function: print $_
$_ = [ (1..20).pick xx (5..10).pick ];  # Generate the initial array
s;  # Print the array
$_»-- if f 7;  # Decrement all elements if it contains a 7
s;  # Print the array
$_»++ if f 0;  # Increment all elements if a zero is found
s;  # Print the array
.splice(.first(13):k // +$_);  # Splice out everything from the first 13 onward
s;  # Print the array
$_ = [ f *%%2 ] if f 2;  # Remove all odd elements if a 2 is found
s;  # Print the array
say(20*(.[2] %% 2 || $_)), exit if $_ > 2 && f 20;  # Print and exit, maybe
s;  # Print the array
.pop while .sum > 49;  # Remove elements from the end until sum is below 50
$/ = f 16;  # Save in $/ whether $_ contains a 16
$_ = .sum;  # Set $_ to its own sum
s;  # Print the sum
.base(16).say if $/  # Print the sum in hexadecimal if the last array contained a 16

0

普通Lisp,490字节

在这里,该数组表示为Common Lisp列表。

(let((a(loop for i from 1 to(+ 5(random 5))collect(1+(random 19)))))(flet((p()(format t"~a~%"a))(m(x)(member x a))(h(x)(map-into a x a)))(p)(and(m 7)(h'1-))(p)(and(m 0)(h'1+))(p)(let((p(position 13 a)))(and p(setf a(butlast a (-(length a)p)))))(p)(and(m 2)(setf a(remove-if'oddp a)))(p)(or(and(m 20)(or(and(third a)(oddp(third a))(* 20(length a)))20))(p)(and(setf a(loop for x in a sum x into s while (<= s 50) collect x)) nil)(p)(let((s(reduce'+ a)))(print s)(and(m 16)(format t"~x"s))))))

像往常一样,大量使用andor作为控制结构。

(let ((a (loop for i from 1 to (+ 5 (random 5))  ; create initial list
            collect (1+ (random 19)))))
  (flet ((p () (format t "~a~%" a))     ; auxiliary functions: print list
         (m (x) (member x a))           ; check membership
         (h (x) (map-into a x a)))      ; operate on elements
    (p)
    (and (m 7) (h '1-))                 ; if 7 is present decrement all values
    (p)
    (and (m 0) (h '1+))                 ; if 0 is now present increment all values
    (p)
    (let ((p (position 13 a)))          ; remove from 13 (if exists)
      (and p (setf a (butlast a (- (length a) p)))))
    (p)
    (and (m 2) (setf a (remove-if 'oddp a)))   ; if 2 is present remove odd values
    (p)
    (or (and (m 20)                            ; if 20 is present
             (or (and (third a)                ;    when third is present
                      (oddp (third a))         ;         and it is odd
                      (* 20 (length a)))       ;         return 20 times the length
                 20))                          ;    otherwise return 20
        (p)                                    ; otherwise (20 is not present)
        (and (setf a (loop for x in a sum x into s ; compute sum of elements
                        while (<= s 50)            ; limited to 50
                        collect x))                ; and return those elements
             nil)                                  ; (to execute the rest of the code)
        (p)
        (let ((s (reduce '+ a)))                   ; compute the final sum
          (print s)                                ; print the result in decimal
          (and (m 16) (format t "~x" s))))))       ; if 16 is present print also in hexadecimal
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.