蓝鸭,红鸭,灰鸭


48

因此,上周我发布了一个挑战赛以玩Duck,Duck,Goose。这导致许多明尼苏达州人对其地区的“灰鸭”变异发表评论。

所以这是规则:

使用以下颜色列表:

Red
Orange
Yellow
Green
Blue
Indigo
Violet
Gray

编写程序以遵循以下规则:

  1. 选择这些颜色之一,然后将其添加到“ duck”一词之前,然后将结果打印到新行。
  2. 如果颜色不是“灰色”,请重复步骤1。
  3. 如果颜色是“灰色”,请结束程序。

必须遵循的规则:

  • 该程序不应始终打印相同的行数。
  • 它可以从“灰鸭”开始,但不应始终如一。
  • 每行上应该只有一只鸭子,并且不输出空行。
  • 在颜色和鸭子之间至少应有一个空间。
  • 重要输出前后的空白并不重要。
  • 输出的大小写无关紧要。
  • 颜色可以重复。
  • 输出不必每次都包含每种颜色,但是您的代码必须可以输出每种颜色。
  • 不能包含上述阵列以外的颜色。
  • 灰色或灰色都可以接受。
  • 颜色不应始终一致。
  • 争取最短的程序。最少的字节数获胜。

输出示例:

Green duck
Orange duck
Yellow duck
Indigo duck
Yellow duck 
Gray duck

感谢@Mike Hill首先提醒我这种变化。


关于结果分配有什么规定吗?因为我可以通过从非灰色颜色中随机选择几次来生成有效的输出,然后Grey再打印一次(因此我不必从所有颜色中都选择并检查是否选择了Grey)。
马丁·恩德

@MartinEnder很好。“颜色不应该始终保持相同的顺序。” 很重要,但是没有什么可以阻止您最后单独选择灰色。
AJFaraday

3
是否允许使用其他拼写“灰色”?
18Me21年

@ 12Me21很好 有技术原因吗?还是仅仅是一种美学?
AJFaraday

2
以我不太谦逊的观点,您缺少一种极为重要的鸭子颜色。蓝色接近,但不够精确。
cobaltduck

Answers:


6

05AB1E42 40字节

多亏了Erik the Outgolfer,节省了2个字节

[“ëßigo°¯†¾›ÈŠÛˆ¨‡—°Íolet“#7ÝΩ©è'Ðœðý,®#

在线尝试!


3
我注意到了许多不可读的高尔夫语言。是否有一些更易理解的形式的编译器?
AJFaraday

@AJFaraday:我不知道。我唯一知道的其中一种高尔夫语言是木炭。
Emigna '18

1
您可以进一步压缩您的字符串,indëß
暴民埃里克(Erik the Outgolfer)

1
Gs2也有一个。但是,这种语言现在很少使用了。
递归


46

LuaLaTeX,220点 211的字符

命令:

lualatex -interaction nonstopmode

不是最短的,而是最幻想的。基于@skillmon的解决方案

在此处输入图片说明

\RequirePackage{tikzducks}\newcount\b\let~\def~\0{red}~\1{orange}~\2{yellow}~\3{green}~\4{blue}~\5{cyan}~\6{violet}~\7{gray}\loop\b\uniformdeviate8\tikz\duck[body=\csname\the\b\endcsname]; \ifnum\b<7\repeat\stop

9
我的妈呀!这只是最好的事情!你让我开心,亚历克斯。
AJFaraday

@AJFaraday在这里使用确实是一个很棒的主意tikzducks:)
Skillmon

6
+1,我很抱歉选择了这么长的包裹名称!

@sam,您得到了赦免
AlexG

8
先生你好。在TeX.sx等其他技术社区中我绝对不认识的人。由于此答案涵盖了Anatidae家族中的一些鸟类,因此我不得不通过投票来表示同意。:)
Paulo Cereda,

26

6502机器代码(C64),124字节

00 C0 AD 12 D0 85 02 A2 17 8E 18 D0 A5 02 F0 03 0A 90 02 49 1D 85 02 A8 CA 10
02 A2 2F BD 42 C0 D0 F6 88 D0 F3 86 FB E8 BD 42 C0 F0 06 20 16 E7 E8 D0 F5 AA
BD 73 C0 F0 06 20 16 E7 E8 D0 F5 A6 FB D0 C9 60 00 C7 52 45 59 00 D2 45 44 00
CF 52 41 4E 47 45 00 D9 45 4C 4C 4F 57 00 C7 52 45 45 4E 00 C2 4C 55 45 00 C9
4E 44 49 47 4F 00 D6 49 4F 4C 45 54 00 20 44 55 43 4B 0D 00

在线演示 -用法:SYS49152

屏幕截图


说明(注释拆卸):

         00 C0       .WORD $C000        ; load address
.C:c000  AD 12 D0    LDA $D012          ; current rasterline as seed
.C:c003  85 02       STA $02            ; to "random" value
.C:c005  A2 17       LDX #$17           ; cfg for upper/lower, also use as
.C:c007  8E 18 D0    STX $D018          ;    initial index into colors array
.C:c00a   .loop:
.C:c00a  A5 02       LDA $02            ; load current random val
.C:c00c  F0 03       BEQ .doEor         ; zero -> feedback
.C:c00e  0A          ASL A              ; shift left
.C:c00f  90 02       BCC .noEor         ; bit was shifted out -> no feedback
.C:c011   .doEor:
.C:c011  49 1D       EOR #$1D
.C:c013   .noEor:
.C:c013  85 02       STA $02            ; store new random val
.C:c015  A8          TAY                ; use as counter for next color string
.C:c016   .findloop:
.C:c016  CA          DEX                ; next char pos in colors (backwards)
.C:c017  10 02       BPL .xok           ; if negative ...
.C:c019  A2 2F       LDX #$2F           ;    load length of colors - 1
.C:c01b   .xok:
.C:c01b  BD 42 C0    LDA .colors,X      ; load character from colors
.C:c01e  D0 F6       BNE .findloop      ; not zero, try next character
.C:c020  88          DEY                ; decrement random counter
.C:c021  D0 F3       BNE .findloop      ; not zero, continue searching
.C:c023  86 FB       STX $FB            ; save character position
.C:c025  E8          INX                ; increment to start of color
.C:c026   .outloop:
.C:c026  BD 42 C0    LDA .colors,X      ; output loop for color string
.C:c029  F0 06       BEQ .duckout
.C:c02b  20 16 E7    JSR $E716
.C:c02e  E8          INX
.C:c02f  D0 F5       BNE .outloop
.C:c031   .duckout:
.C:c031  AA          TAX                ; A is now 0, use as char pos for duck
.C:c032   .duckoutloop:
.C:c032  BD 73 C0    LDA .duck,X        ; output loop for duck string
.C:c035  F0 06       BEQ .outdone
.C:c037  20 16 E7    JSR $E716
.C:c03a  E8          INX
.C:c03b  D0 F5       BNE .duckoutloop
.C:c03d   .outdone:
.C:c03d  A6 FB       LDX $FB            ; load saved character position
.C:c03f  D0 C9       BNE .loop          ; not zero -> continue main loop
.C:c041  60          RTS                ; zero was start of "Grey" -> done
.C:c042   .colors:
.C:c042  00 c7 52 45    .BYTE 0, "Gre"
.C:c046  59 00 d2 45    .BYTE "y", 0, "Re"
.C:c04a  44 00 cf 52    .BYTE "d", 0, "Or"
.C:c04e  41 4e 47 45    .BYTE "ange"
.C:c052  00 d9 45 4c    .BYTE 0, "Yel"
.C:c056  4c 4f 57 00    .BYTE "low", 0
.C:c05a  c7 52 45 45    .BYTE "Gree"
.C:c05e  4e 00 c2 4c    .BYTE "n", 0, "Bl"
.C:c062  55 45 00 c9    .BYTE "ue", 0, "I"
.C:c066  4e 44 49 47    .BYTE "ndig"
.C:c06a  4f 00 d6 49    .BYTE "o", 0, "Vi"
.C:c06e  4f 4c 45 54    .BYTE "olet"
.C:c072  00             .BYTE 0
.C:c073   .duck:
.C:c073  20 44 55 43    .BYTE " duc"
.C:c077  4b 0d 00       .BYTE "k", $d, 0

抱歉,您的演示似乎没有输出任何与鸭子相关的内容。
AJFaraday

1
@AJFaraday请注意“使用”部分...运行它的命令是sys 49152
菲利克斯·帕尔梅

好的,这让我很开心:)
AJFaraday '18

1
这太棒了。自从我编写c64汇编已经有一段时间了。
lsd

2
@lsd:在这里一样!我的第一个程序是在C64上进行6502组装,因为我刚得到它,而一位朋友给我写了6502书,任务是“编写游戏!” 让我写点东西!(很棒的方法!不仅是阅读,而且实际上是写东西,这是一个很大的动力)。编写了一个192字节(iirc)的“ Snake”程序(带有一个imo聪明的双索引,指向蛇头的头部和尾部)...第一次尝试:它放大了底部边缘,“进食”到其余的公羊,生长在任何“ @”上,因为我忘了筑墙^^
Olivier Dulac


10

出租车,1995字节

Go to Heisenberg's:w 1 r 3 r 1 l.[a]Pickup a passenger going to Divide and Conquer.8 is waiting at Starchild Numerology.8 is waiting at Starchild Numerology.Go to Starchild Numerology:n 1 l 3 l 1 l 3 l.Pickup a passenger going to Divide and Conquer.Pickup a passenger going to Multiplication Station.Go to Divide and Conquer:w 1 r 3 r 1 r 2 r 1 r.Pickup a passenger going to Cyclone.Go to Cyclone:e 1 l 1 l 2 l.Pickup a passenger going to What's The Difference.Pickup a passenger going to Trunkers.Go to Zoom Zoom:n.Go to Trunkers:w 3 l.Pickup a passenger going to What's The Difference.Go to What's The Difference:w 2 r 1 l.Pickup a passenger going to Multiplication Station.1 is waiting at Starchild Numerology.Go to Starchild Numerology:e 1 r 1 l 3 l.Pickup a passenger going to Addition Alley.Go to Multiplication Station:w 1 r 2 r 1 r 4 l.Pickup a passenger going to Addition Alley.Go to Addition Alley:n 2 l 1 r 3 l 1 l.Pickup a passenger going to The Underground.'Red duck\n' is waiting at Writer's Depot.'Orange duck\n' is waiting at Writer's Depot.'Yellow duck\n' is waiting at Writer's Depot.'Green duck\n' is waiting at Writer's Depot.'Blue duck\n' is waiting at Writer's Depot.'Indigo duck\n' is waiting at Writer's Depot.'Violet duck\n' is waiting at Writer's Depot.'Grey duck' is waiting at Writer's Depot.Go to Writer's Depot:n 1 l 1 l.[b]Pickup a passenger going to Narrow Path Park.Go to Narrow Path Park:n 3 r 1 l 1 r.Go to The Underground:e 1 r.Switch to plan "c" if no one is waiting.Pickup a passenger going to The Underground.Go to Writer's Depot:s 2 r 1 l 2 l.Switch to plan "b".[c]Go to Narrow Path Park:n 4 l.Pickup a passenger going to Post Office.Go to Post Office:e 1 r 4 r 1 l.Go to Writer's Depot:s 1 r 1 l 2 l.Switch to plan "a" if no one is waiting.[d]Pickup a passenger going to Sunny Skies Park.Go to Sunny Skies Park:n 2 r.Go to Writer's Depot:n 1 l.Switch to plan "e" if no one is waiting.Switch to plan "d".[e]Go to Heisenberg's:n 3 r 3 r.Switch to plan "a".

在线尝试!

我认为值得一提的是,这段代码中有47%只是从1到8中选择一个随机整数。
而且,Taxi非常冗长,以至于duck\n在每种颜色之后硬编码而不是在以后进行级联的方式要短得多。
这是非高尔夫版本:

Go to Heisenberg's: west 1st right 3rd right 1st left.

[Pick up a random INT 1-8 going to The Underground]
[a]
Pickup a passenger going to Divide and Conquer.
8 is waiting at Starchild Numerology.
8 is waiting at Starchild Numerology.
Go to Starchild Numerology: north 1st left 3rd left 1st left 3rd left.
Pickup a passenger going to Divide and Conquer.
Pickup a passenger going to Multiplication Station.
Go to Divide and Conquer: west 1st right 3rd right 1st right 2nd right 1st right.
Pickup a passenger going to Cyclone.
Go to Cyclone: east 1st left 1st left 2nd left.
Pickup a passenger going to What's The Difference.
Pickup a passenger going to Trunkers.
Go to Zoom Zoom: north.
Go to Trunkers: west 3rd left.
Pickup a passenger going to What's The Difference.
Go to What's The Difference: west 2nd right 1st left.
Pickup a passenger going to Multiplication Station.
1 is waiting at Starchild Numerology.
Go to Starchild Numerology: east 1st right 1st left 3rd left.
Pickup a passenger going to Addition Alley.
Go to Multiplication Station: west 1st right 2nd right 1st right 4th left.
Pickup a passenger going to Addition Alley.
Go to Addition Alley: north 2nd left 1st right 3rd left 1st left.
Pickup a passenger going to The Underground.

[Use the random INT to select a color]
'Red duck\n' is waiting at Writer's Depot.
'Orange duck\n' is waiting at Writer's Depot.
'Yellow duck\n' is waiting at Writer's Depot.
'Green duck\n' is waiting at Writer's Depot.
'Blue duck\n' is waiting at Writer's Depot.
'Indigo duck\n' is waiting at Writer's Depot.
'Violet duck\n' is waiting at Writer's Depot.
'Grey duck' is waiting at Writer's Depot.
Go to Writer's Depot: north 1st left 1st left.
[b]
Pickup a passenger going to Narrow Path Park.
Go to Narrow Path Park: north 3rd right 1st left 1st right.
Go to The Underground: east 1st right.
Switch to plan "c" if no one is waiting.
Pickup a passenger going to The Underground.
Go to Writer's Depot: south 2nd right 1st left 2nd left.
Switch to plan "b".

[Output the selected color]
[c]
Go to Narrow Path Park: north 4th left.
Pickup a passenger going to Post Office.
Go to Post Office: east 1st right 4th right 1st left.

[If the color was grey, exit by error]
Go to Writer's Depot: south 1st right 1st left 2nd left.
Switch to plan "a" if no one is waiting.

[Get rid of the rest of the colors]
[You could throw them off a bridge but you won't get paid]
[d]
Pickup a passenger going to Sunny Skies Park.
Go to Sunny Skies Park: north 2nd right.
Go to Writer's Depot: north 1st left.
Switch to plan "e" if no one is waiting.
Switch to plan "d".

[Start over from the beginning]
[e]
Go to Heisenberg's: north 3rd right 3rd right.
Switch to plan "a".

读起来很有趣。
Makotosan

天哪,这太神秘了!我无法理解它是如何工作的。
祝您旅途

这似乎与Fetlang
Skillmon's

8

Java(OpenJDK 9),133字节

v->{for(int x=9;x>0;)System.out.println("Grey,Red,Orange,Yellow,Green,Blue,Indigo,Violet".split(",")[x+=Math.random()*8-x]+" duck");}

在线尝试!

说明

v->{                              // Void-accepting void lambda function
  for(int x=9;x>0;)               //  Loop until x is zero
    System.out.println(           //   Print...
        "Grey,Red,Orange,         //       colors, "Grey" first
         Yellow,Green,Blue,       //          more colors
         Indigo,Violet"           //          more colors
        .split(",")               //       as an array
          [x+=Math.random()*8-x]  //       pick one randomly, use implicit double to int cast with "x+=<double>-x" trick
        +" duck");                //      Oh, and append " duck" to the color.
}

8

Ruby93 91 90 89 87 86 85字节

感谢Dom Hastings保存2个字节,Kirill L. 1个字节和Asone Tuhid 1个字节!

puts %w(Red Orange Yellow Green Blue Indigo Violet Grey)[$.=rand(8)]+" duck"while$.<7

在线尝试!


您可以删除()周围的代码,如果你使用$.的,而不是s存储索引,可以避免创建s完全(因为$.是预先初始化行号!)你需要移动Grey到列表的结束和检查$.<7,而不是虽然。希望有帮助!
Dom Hastings '18

@DomHastings谢谢!我一直在寻找摆脱的方法,s=1而且$.很完美!
克里斯蒂安·卢帕斯库

我认为您也可以在一段时间后删除空间,Ruby似乎对此并不抱怨。
Kirill L.

@KirillL。是的,谢谢!我以前有,while s...而且需要空间。更改s为后没有看到这个机会$.
克里斯蒂安·卢帕斯库


7

操作Flashpoint脚本语言,133字节

f={s="";v=s;while{v!="grey"}do{v=["Red","Orange","Yellow","Green","Blue","Indigo","Violet","Grey"]select random 7;s=s+v+" duck\n"};s}

致电:

hint call f

输出示例:

最初,我以某种方式误解了挑战,好像目标只是输出不同数量的行,而不一定以“灰鸭”行结尾。之后,错误的解释产生了一段稍微有趣的代码:

f={s="";c=[1];c set[random 9,0];{s=s+(["Red","Orange","Yellow","Green","Blue","Indigo","Violet","Grey"]select random 7)+" duck\n"}count c;s}

7

pdfTeX的,231个 220 219 209 207字节

\newcount\b\let~\def~\0{Red}~\1{Orange}~\2{Yellow}~\3{Green}~\4{Blue}~\5{Indigo}~\6{Violet}~\7{Gray}~\9{ }\newlinechar`z\loop\b\pdfuniformdeviate8\message{z\csname\the\b\endcsname\9duck}\ifnum\b<7\repeat\bye

LuaTEX等程序,216个 206 204字节

\newcount\b\let~\def~\0{Red}~\1{Orange}~\2{Yellow}~\3{Green}~\4{Blue}~\5{Indigo}~\6{Violet}~\7{Gray}~\9{ }\newlinechar`z\loop\b\uniformdeviate8\message{z\csname\the\b\endcsname\9duck}\ifnum\b<7\repeat\bye



5

PHP,89字节

do echo[Grey,Red,Orange,Yellow,Green,Blue,Indigo,Violet][$i=rand()%8]," Duck
";while($i);

运行-nr在线尝试


4

> <>,107字节

x<>" duck"a>
x<^"deR"
x<^"egnarO"
x<^"wolleY"
x<^"neerG"
x<^"eulB"
x<^"ogidnI"
x<^"teloiV"
x"Grey duck"r>o|

在线尝试!


4

八度114112字节

do disp([strsplit('Red Orange Yellow Green Blue Indigo Violet'){i=randi(7)},' duck'])until i>6
disp('Grey duck')

在线尝试!

有很多不同的选项,它们的长度都在112到118字节之间。有些选项在开始处初始化索引,并为每个循环将其递减一个随机数,并一直等到它为0为止。其他人使用printf而不是disp避免使用一些括号等。


小问题:八度音阶中是否有一个符号与Excel中的&相同,因为我认为那时可以进一步缩短代码。
Michthan

1
不幸的是,没有...字符串必须串联在方括号内(或使用诸如cathorzcat。的功能,尽管感谢:)
Stewie Griffin

4

PHP133个 125 111 108 97 92字节

<?for(;$b=[Red,Orange,Yellow,Green,Blue,Indigo,Violet][rand(0,7)];)echo"$b duck
"?>Grey duck

在线尝试!

-8个字节,感谢@OlivierGrégoire

-3个字节,感谢@manatwork

-11个字节,感谢@Dom Hastings


2
在颜色字符串和测试中,将其更改GreyX,因为未使用它。您将获得6个字节。
奥利维尔·格雷戈尔

@OlivierGrégoire哦,是的。感谢您的:)
Davіd

1
颠倒for不需要括号的条件:x!=$b=$a[array_rand($a)]。顺便说一句,?>终止一条语句,不需要;在它前面。
manatwork'Mar 19'18

@manatwork哦,太好了!我将立即更新答案!
Davіd

2
您可以通过根本不包含更多字节,x并将其$b=$a..用作条件并使用rand(0,7)代替来节省更多字节array_rand。您也可以在?>和之间删除换行符Grey duck。同样,您的TIO链接仍然具有完整的标签,您可以添加-d short_open_tag=on到标记中以使其正常工作!:)
Dom Hastings


4

bash,96个字节

a=(Grey Red Orange Yellow Green Blue Indigo Violet);for((i=1;i;));{ echo ${a[i=RANDOM%8]} duck;}

感谢@DigitalTrauma。


高尔夫的机会很多-查看bash高尔夫技巧
Digital Trauma

@DigitalTrauma我很着急,但是我想要一个没有GNU coreutils的纯Bash解决方案。我可以将其从112个字节缩小到105个字节。
rexkogitans

是的,我也喜欢纯粹的答案。 这还有10个字节的折扣
Digital

我脱衣舞了>0,但是我离开了,${#a}而不是8
rexkogitans

为什么需要保留${#a}而不是8?这是代码高尔夫球-无需为其他颜色的颜色修改代码的通用性。您所需要做的就是以最小的字节数满足规范。
Digital Trauma


3

AWK,114字节

{srand();for(split("Red9Orange9Yellow9Green9Blue9Indigo9Violet9Grey",A,9);r<8;print A[r]" duck")r=int(8*rand()+1)}

在线尝试!

说明

{srand();                  # Seed rand to obtain different sequence each run
for(
     split("Red9Orange9Yellow9Green9Blue9Indigo9Violet9Grey",
            A,9);          # Split on 9 to avoid using '"'s
     r<8;
     print A[r]" duck")    # Print the colored ducks
     r=int(8*rand()+1)     # AWK uses 1-indexing not 0-indexing when splitting strings into arrays
}

请注意,这需要“某些”输入。输入可以为空。为了避免输入的需要,在第一行之前加上BEGIN


3

PowerShell,94字节

for(;$r-ne'Grey'){$r=-split"Red
Orange
Yellow
Green
Blue
Indigo
Violet
Grey"|Random;"$r Duck"}

在线尝试!

循环直到$r等于Grey。在循环内部,-split在换行符上输入文字字符串,选择Random其中一个,然后打印出加号Duck(从技术上讲,它留在管道上,并且在下一个循环迭代中进行管道清理会导致a Write-Output发生)。请注意,从理论上讲Grey,永远不会选择循环,并且循环会无限期地继续下去,但这几乎永远不会发生(在概率意义上)。


3

R,101字节

cat(paste(c(sample(scan(,""),rexp(1),T),"gray"),"duck\n"))
Red
Orange
Yellow
Green
Blue
Indigo
Violet

在线尝试!

受到@ user2390246对相关挑战的回答的启发。我们需要两个随机性来源:更改颜色顺序和对非灰色鸭子颜色进行采样。在sample将通过用速率参数的指数分布给定随机大小的随机样本1,截断为整数。不幸的是,使用指数扰动意味着 至少有exp(-8)或大约0.0003354有样本的可能性8,因此我们必须使用进行抽样replace=T


您可以将其替换\n为1的实际换行符
MickyT

也可以节省很多,colors()[c(26,254,498,552,640,652)]以代替scan(..)等等。应该下降到大约83
MickyT

@MickyT .........我使用R的图形不够用,无法记住所有漂亮的内置图形colors()!那些是不错的高尔夫,我认为您应该将其发布为自己的答案,因为这是获得颜色的不太优雅的方法。
朱塞佩

好的,我将发布一些小改动
MickyT

3

Python 2中138个 133 120 117 116字节

import os
while id:id=ord(os.urandom(1))%8;print"Grey Red Orange Yellow Green Blue Indigo Violet".split()[id],'duck'

在线尝试!

@EriktheOutgolfer的一些想法要好得多。谢谢!

-3多亏@ovs

-1,感谢@Rod学习了一个很酷的新技巧:-)


3

视网膜69 68字节

感谢Leo节省了1个字节。

.^/y/{K`Red¶Orange¶Yellow¶Green¶Blue¶Indigo¶Violet¶Grey
" duck¶">?G`

在线尝试!

说明

./y/^{K`Red¶Orange¶Yellow¶Green¶Blue¶Indigo¶Violet¶Grey

.在程序结束时禁止隐式输出(否则,我们将得到两只灰色的鸭子)。/y/^{只要工作字符串不包含,就将整个程序包装在一个循环中,该循环将继续y。该行的其余部分将工作字符串设置为所有颜色的换行分隔的列表。

" duck¶">G?`

我们从工作字符串中随机抽取一条线(因此随机抽取颜色)。然后,我们在结果的末尾 duck加上换行符。


3

MATL68 64字节

`'DYCIXMSQ(qm#Q$4{#is,Gh1(=lAjUSId;&'F2Y232hZaYb8YrX)' duck'h7Mq

在线尝试!

说明

`                         % Do...while
  'DYCI···Id;&'           %   Push this string (to be decompressed by base conversion)
  F                       %   Push false
  2Y2                     %   Push string 'abc...xyz'
  32                      %   Push 32 (ASCII for space)
  h                       %   Concatenate horizontally. Gives 'abc...xyz '
  Za                      %   Base-convert from alphabet of all printable ASCII
                          %   characters except single quote (represented by input
                          %   false) to alphabet 'abc...xyz '. Produces the string
                          %   'grey red ··· violet'
  Yb                      %   Split on space. Gives a cell array of strings
  8Yr                     %   Random integer from 1 to 8, say k
  X)                      %   Get the content of the k-th cell
  ' duck'                 %   Push this string
  h                       %   Concatenate horizontally
  7M                      %   Push k again
  q                       %   Subtract 1
                          % Implicit end. Run a new iteration if top of the stack
                          % is non-zero
                          % Implicit display

3

Python 3中,130128127126,125个字节

from random import*
d,c=1,'Grey Red Orange Yellow Green Blue Indigo Violet'.split()
while d!=c[0]:d=choice(c);print(d,'duck')

-1来自@ElPedro!
-1由我
-1由@Bubbler!

在线尝试!


您可以将Gray移到最前面并必须d!=c[0]保存一个字节吗?
ElPedro '18

print(d,'duck')保存一个字节。默认的分隔符是空格。
Bubbler

3

Java(JDK 10),287字节

Random r=new Random();int i;String c;do{i=r.nextInt(8);switch(i){case 0:c="Red";break;case 1:c="Orange";break;case 2:c="Yellow";break;case 3:c="Green";break;case 4:c="Blue";break;case 5:c="Indigo";break;case 6:c="Violet";break;default:c="Gray";}System.out.println(c+" duck");}while(i!=7)

在线尝试!

我的第一个代码高尔夫!显然没有竞争力,只是高兴地学习了足够的Java(目前在CS200中)可以参与。


2

Kotlin,122字节

while({x:Any->println("$x duck");x!="Grey"}("Red,Orange,Yellow,Green,Blue,Indigo,Violet,Grey".split(",").shuffled()[0])){}

在线尝试!


1
可能会对那里的某个人有所帮助,以便在Kotlin中获得随机整数值,可以使用以下代码:(0..7).shuffled()[0]短于:(Math.random()* 8).toInt ()
Makotosan

2

MS-SQL,158个字节

DECLARE @ VARCHAR(6)a:SELECT @=value FROM STRING_SPLIT('Red,Orange,Yellow,Green,Blue,Indigo,Violet,Grey',',')ORDER BY NEWID()PRINT @+' duck'IF @<>'Grey'GOTO a

很大程度上基于Razvan的出色回答,但使用的STRING_SPLIT是MS-SQL 2016及更高版本特有的功能。也使用GOTO而不是WHILE循环。

格式:

DECLARE @ VARCHAR(6)
a:
    SELECT @=value FROM 
        STRING_SPLIT('Red,Orange,Yellow,Green,Blue,Indigo,Violet,Grey',',')
        ORDER BY NEWID()
    PRINT @+' duck'
IF @<>'Grey'GOTO a

2

T-SQL195个 185 184字节

DECLARE @ VARCHAR(9)=''WHILE @<>'Grey'BEGIN SELECT @=c FROM(VALUES('Red'),('Orange'),('Yellow'),('Green'),('Blue'),('Indigo'),('Violet'),('Grey'))v(c)ORDER BY NEWID()PRINT @+' Duck'END

1
不错的变化。您可以在之前删除空格BEGIN
BradC

2

Python 2,98个字节

看起来没有进口!

v=0
while 1:x=id(v)%97%8;print"GVIYORGBrinererleodladeuyliln ee egog n  towe"[x::8],"duck";v=1/x,v

(在颜色之间以及duck问题允许的位置打印多余的空格)

在线尝试!

一个相当差劲的伪随机数生成器,其对象ID为0(但似乎符合规范),它x[0,7]中反复产生一个整数,该整数用于从该索引中切出一个字符列表。步骤8可获得与duck元组一起打印的颜色名称,并在两者之间加一个空格。当x变为零时,Grey将打印下一个输入,并对基于模的随机数生成器错误的下一个输入进行求值,尝试除以零(v=1/x,v尝试使用第一个元素1/x= 创建新的元组1/0


同样的方法在Python 3中是100

v=0
while 1:x=id(v)%17%8;print("GVIYORGBrinererleodladeuyliln ee egog n  towe"[x::8],"duck");v=1/x,v
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.