Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

No one is quite certain what the emoticon >:U is intended to represent, but many scholars believe it looks like an angry duck. Let's assume that's the case.

Task

Given an integer n between 0 and 3 inclusive, print or return

quack

if n = 0,

>:U

if n = 1,

     U   U
>  : U   U
 >   U   U
>  : U   U
      UUU

if n = 2, or

                  >:U         >:U
>:U               >:U         >:U
   >:U       >:U  >:U         >:U
      >:U         >:U         >:U
         >:U      >:U         >:U
      >:U         >:U         >:U
   >:U       >:U  >:U         >:U
>:U               >:U         >:U
                     >:U>:U>:U

if n = 3.

You can assume the input will always be valid. There should be no leading space in the output, but any amount of trailing space is fine. Ducks (with the possible exception of @cobaltduck) have no tolerance for loopholes. Shortest code in bytes wins.

share|improve this question
4  
...huh. The number tag seems like it should be on a lot of challenges. "using,...accepting as input... numerical values." – VTCAKAVSMoACE Feb 25 at 22:30
73  
First I was thinking "huh, what is a duck?". Luckily you have provided a link to the wikipedia page. – Adnan Feb 25 at 22:52
6  
"Ducks have no tolerance for loopholes." But you're a bird not a duck, does that mean we can use loopholes? :P – Upgoat Feb 25 at 22:57
6  
@Downgoat Nope. Also that edit was entirely unnecessary so I rolled it back. – Alex A. Feb 25 at 22:57
6  
@Downgoat s/:P/>:U/ – ETHproductions Feb 25 at 22:59

10 Answers 10

up vote 18 down vote accepted

CJam, 108 90 85 bytes

"quack"">:U":D"scT¦{$ì"{269b22bSf*D}:F~[ZYB].*s.+s5/"ÿ3nÜïS{JÐø¦yûn"F*33/z]ri=zN*

Note that some characters are unprintable. Try it online!

Background

The first two outputs are incompressible in CJam.

Compressing the last output is straightforward. After stripping all linefeeds, we can split the resulting string at occurrences of >:U, and compute the length of each resulting string of spaces.

That results in the array

[18 9 0 15 9 3 7 2 9 6 9 9 9 6 9 6 9 9 3 7 2 9 0 15 9 21 0 0 0]

which we can store efficiently by converting it from base 22 to base 269, yielding the digits

[255 12 51 110 220 239 83 123 74 208 248 166 121 251 110 17]

Since each digit is less than 256, we can store it as a single byte.

Finally, compressing the third output becomes easier if we transpose lines and columns:

 > > 
  >  

 : : 

UUUU 
    U
    U
    U
UUUU

Counting once more the spaces between non-whitespace characters, we get the array

[1 1 3 8 1 6 0 0 0 5 4 4 0 0 0 0 0]

which becomes

[115 159 99 84 166 123 36 236 6]

when transcoded from base 22 to base 269.

How it works

"quack"   e# Push the first output.

">:U":D   e# Push the second output, and save it in D.

e# Push [115 159 99 84 166 123 36 236 6] as bytes.

"scT¦{$ì"

{         e# Define a code block:
  269b22b e#   Transcode from base 269 to base 22.
  Sf*     e#   Replace each digit with a string of that many spaces.
  D       e#   Push D.
}         e#
:F~       e# Save the block in F. Execute it.
[ZYB]     e# Push [3 2 11].
.*        e# Vectorized repeat; push [">>>" "::" "UUUUUUUUUUU"].
s         e# Flatten the array of strings.
.+        e# Append the nth character to the nth string of spaces.
s5/       e# Flatten and split into chunks of length 5.

e# Push [255 12 51 110 220 239 83 123 74 208 248 166 121 251 110 17] as bytes.

"ÿ3nÜïS{JÐø¦yûn"

F         e# Execute F.
*         e# Join the resulting array of strings of spaces, separating by ">:U".
33/       e# Split into chunks of length 33.
z         e# Zip; transpose rows with columns.

]         e# Wrap the entire stack in an array.
ri        e# Read a token from STDIN and interpret it as an integer.
=         e# Retrieve the element at the corresponding index.
z         e# Zip; transpose rows with columns or map "string" to ["string"].
N*        e# Join, separating by linefeeds.
share|improve this answer
2  
I like the extra :D between the two strings. – Zgarb Feb 26 at 0:13
6  
@Zgarb He has appeased the great duck overlord. The duck now smiles. – Alex A. Feb 26 at 0:17
6  
I went through the page thinking, "172... 162... 182... I can beat all those. Wait, 90? Oh, it's Dennis the Unbeatable..." – ETHproductions Feb 26 at 0:47

Java, 303 286 bytes

Saved 17 bytes thanks to @VoteToClose!

Not actually meant to be the shortest, I just thought it would be fun to try in Java.

Creates a list of string variables representing common strings, then creates an array of all the outputs, then prints the right one.

String a(int y){String n="\n",d=">:U",A=" ",B=A+A,C=B+B,D=C+C,a="U"+B+" U"+n,G=D+A,H=C+B,c=d+G+d+n,E=B+A,F=C+E;String[]z={"quack",d,C+A+a+">"+B+": "+a+" >"+E+a+">"+B+": "+a+C+B+"UUU",D+D+B+c+d+D+F+c+B+A+d+F+d+B+c+H+d+G+c+G+d+H+c+H+d+G+c+E+d+F+d+B+c+d+D+F+c+D+D+C+A+d+d+d};return z[y];}

Ungolfed:

String a(int y) {
    String n = "\n", d = ">:U", A = " ", B = A + A, C = B + B, D = C + C,
            a = "U" + B + " U" + n, G = D + A, H = C + B, c = d + G + d + n,
            E = B + A, F = C + E;
    String[] z = { "quack", d, C + A + a + ">" + B + ": " + a + " >" + E + a + ">" + B + ": " + a + C + B + "UUU", D + D + B + c + d + D + F + c + B + A + d + F + d + B + c + H + d + G + c + G + d + H + c + H + d + G + c + E + d + F + d + B + c + d + D + F + c + D + D + C + A + d + d + d };
    return z[y];
}

This is my first answer on this site, so please tell me if I have done something wrong.

share|improve this answer
2  
It looks fine! Nice first post, and welcome to PPCG! – Cᴏɴᴏʀ O'Bʀɪᴇɴ Feb 26 at 1:22
    
@AlexA. Thanks for letting me know, I fixed it. – FlyingPiMonster Feb 26 at 1:40
2  
@kittycat3141 Looks great. Nice solution and thanks for participating in my challenge! :) – Alex A. Feb 26 at 1:41
    
It appears that D+A and C+B appear frequently enough to be golfed further into 2 new variables. I also get the slightest feeling that using an array with a for loop might help, but I haven't figured out how yet... – VTCAKAVSMoACE Feb 26 at 23:13

05AB1E, 162 159 157 bytes

Code

Dammit, way too long, but it's at least something:

">:U"VI3Qið16×7166b1ð:0Y:DUJ,Yð13×JD?X,3838b1ð:0Y:D?X,16255b1ð:0Y:D?X,16367b1ð:0Y:4F?X,}ð21×Y3×J,}¹2Qið4×" U   U"©J,">  :"®JD," >  "?®,,ð6×'U3×J,}¹iY,}"quack

Try it online!


Explanation

The first part of the code exists of ">:U"V, which sets Y to that string. After that, we just check if the input is equal to 3. That is done at the I3Qi part. If equal, we print the giant magpie:

N = 3

It first starts with ð16× which just pushes 16 space characters. After that, there is a number 7166b. This covers the ">:U " part, with a little help from Retina :). I used this script to convert the string to a binary number. After that, we get to the 1ð:0Y: part, which replaces every 1 with a space character and every 0 with Y, which has been set to >:U. After that, we Duplicate this string, store it in X using U and Join the stack. We pop this using ,, which prints the full string with a newline. All the others after that are all based on the same principe. The if statement ends at the second }.

The full conversion can be found here.

N = 2

We now check if the input is equal to 2. This is done at the ¹2Qi part. After that, if equal, we push the space character 4 times using ð4×. After that, we push the " U U" string and store it using © (idea blatantly stolen from Jelly :p). We again Join the stack and print this with a newline. After that, we push the "> :" string, retrieve the " U U" using ®, Join the stack and Duplicate this string and print these both on the same line.

Short quiz, what will this do: " > "?®,?

After printing the above string, we get the duplicate of the second line of the face, and print this (since it's the same as the 2nd line).

The last part covered by this case is:

ð6×'U3×J,

ð6×        # Push the space character × 6
   'U3×    # Push the "U" character three times
       J,  # Join and print pop with a newline

N = 1

This one is easier to explain:

¹1QiY

¹1Qi   # Check if the input is equal to 1
    Y  # Push the magpie face
       # This is then implicitly printed

N = 0

¹0Qi"quack

¹0Qi        # Check if the input is equal to 0
    "quack  # Weird sound what magpies make
            # Implicitly printed
share|improve this answer
    
Explanation plz? :P – VTCAKAVSMoACE Feb 25 at 23:25
    
@VoteToClose Done :) – Adnan Feb 25 at 23:37
    
"way too long"? See my answer :-P – Luis Mendo Feb 25 at 23:41
1  
TIL that magpies quack. Apparently. – Alex A. Feb 25 at 23:55

JavaScript (ES6), 163 bytes

var solution =

n=>["quack",d=">:U",`5U3U
>2:1U3U
1>3U3U
>2:1U3U
6UUU`,`99090
096090
30702090
609090
906090
609090
30702090
096090
993000`][n].replace(/\d/g,c=>+c?" ".repeat(c):d)
<input type="number" oninput="R.textContent=solution(+this.value)"><pre id="R"></pre>

Explanation

Uses my go-to compression with JavaScript: run-length encoding. The digits 1 to 9 map to that many spaces, 0 maps to the angry duck face, and any other character stays the same.

share|improve this answer
1  
Very impressive for a non-golfing language, but the rows right above and below the center for n = 3 seem to be missing. – ETHproductions Feb 26 at 17:06
    
@ETHproductions Oops, not sure what happened there. Fixed now. – user81655 Feb 26 at 20:38

Japt, 116 105 102 99 96 bytes

["quack""c)`+«öÂ[@=^Gñ`1]o2"mc r'4#¿+R "4z>2:z >2z>2:z6UUU"rz" U3U
" '1]®r'1">:U" r"%d"_SpZ}ÃgU

Contains an unprintable. Test it online!

How it works

The first two strings are not compressed at all. The third is compressed simply by replacing each runs of spaces with its length, then " U3U" with "z". The last one is more complex:

  1. Replace each run of 2-9 spaces with its length.
  2. Replace >:U with 1.
  3. Replace 191\n with 4. (4 isn't used anywhere else in the string.)
  4. Map each valid byte code (10-255, or 0A-FF) to the character with that char code.

The resulting string is only 21 19 bytes long, but the decompression takes up another 31 29.

After decompressing the strings, we simply take the item at position U, the input. (The array is arranged [0,3,2,1] because there's a bug which makes it count from the end of the array instead of the beginning.)

[                      // Create an array of the following:
  "quack"              //  "quack".

  "c)`+«öÂ[@=^Gñ`1]o2" //  Take this string (contains an unprintable).
                       //  Map each char to its char code.
                       //  This produces "994196431712461949164619431712419649931112".
  r'4#¿+R              //  Replace each "4" with the char code of "¿" (191) + a newline.

  "4z>2:z >2z>2:z6UUU" //  Take this string.
  rz" U3U\n"           //  Replace each "z" with " U3U\n".

  '1                   //  "1".
]
®              Ã       // Map each item by this function:
r'1">:U"               //  Replace each "1" with ">:U".
r"%d"_SpZ}             //  Replace each remaining digit Z with Z spaces.

gU                     // Get the item at index -U, wrapping. 
                       // (This should just be U, but there's a bug which negates it.)
share|improve this answer

Vitsy, 172 171 159 bytes

Oh, geez. If I wanted something to show off the power of methods, I got it.

' 'V1+m
'kcauq'Z
'U:>'Z
58m5m6m'   > 'Z5m6m'UUU'68m
f3+bm9mamcm98m2m6bmcmam9mf6+8m3\[2m]
a'U   U'Z
' :  >'Z5m
Z2m98ma2m
\VZ
2mfbm
VVVZ2m78m2mVV7m
8m7m
68m2m9bm

Try it Online!

How this works is by calling various methods throughout itself. The explanation is below:

' 'V1+m
' 'V      Save character literal ' ' as a permanent variable.
    1+    Add one to the top item of the stack (input + 1)
      m   Go to that index of code.

'kcauq'Z
'kcauq'   Push 'quack' to the stack.
       Z  Output everything in the stack as a char.

'U:>'Z
'U:>'Z    Ouput ">:U" with the same method as the previous line.

Now on to some more... interesting lines.

58m5m6m'   > 'Z5m6m'UUU'68m
5              Push space, push 4
 8m            Call the 8th line index.
               As we will soon see, the 8th line index duplicates the space
               the number of times specified by the number just before the call 
               (4 in this case)
   5m          Call the 5th line index.
               The 5th line index outputs the 'U   U' and a newline.
     6m        Call the 6th line index.
               The 6th line index outputs '>  : U   U' and a newline.
'   > 'Z       Output ' >   '.
        5m6m   Same method calls as before.
'UUU'          Push 'UUU'.
     68m       Push 6, then call the 8th line index. This gives us the correct padding.

f3+bm9mamcm98m2m6bmcmam9mf6+8m3\[2m]
f3+              Push 18.
   bm            Call the 11th line index.
                 The 11th line index calls the 8th line index (which we've already seen
                 in action) and then the 7th line index, which you can find and explanation
                 for below (it does a lot)
     9m          Call the 9th line index.
                 The 9th line index outputs '>:U               >:U         >:U' (explanation lower)
       am        Call the 10th line index.
                 ...I'm gonna stop explaining these and just tell you to go to the lines now. :P
         cm      Call the 12th line index.
9                Push space, push 9.
 8m              Call the 8th line index (explained below and before).
   2m            Call the 2nd line index.
     6           Push 6.
      bm         Call the 11th line index. We've finished up to '>:U      >:U         >:U' now.
cm               You guessed it! Call the 12th line index. (explanation below)
  am             Call the 10th line index. (explanation below)
    9m           Call the 9th line index. (explanation below)
f6+              Push space, push 19 21.
   8m            Call the 8th line index. (explanation below)
     3\[2m]      Call the 2nd line index thrice.

All of the rest of these methods are supporting methods now.

a'U   U'Z       Output 'U   U' followed by a newline.

' :  >'Z5m      Output '>  : U   U' followed by a newline.

Z2m98ma2m
Z               Output everything currently in the stack.
 2m             Call the 2nd line index.
   9            Push space, push 8.
    8m          Call the 8th line index. (explained below)
      a         Push a newline to the stack.
       2m       Call the 2nd line index.
                This handles the biggest angry duck face's faces showing the eyebrows and eyes.

\VZ
\V    Push space as many times as the top item specifies.
  Z   Output everything in the stack.

2mfbm
2m      Call the 2nd line index.
  f     Push space, push 14.
   bm   Go to the 11th line index.
        This handles the mouth and some parts of the eyebrows of the biggest duck face.

VVVZ2m78m2mVV7m
VVVZ              Output 3 spaces (and whatever was pushed before it)
    2m            Call the 2nd line index.
      7           Push space, push 6.
       8m         Call the 8th line index. (explained... above)
         2m       Call the 2nd line index.
           VV     Push 2 spaces.
             7m   Call the 7th line index.

8m7m     This is pretty damn self-explanatory if you've read this far.

68m2m9bm
6            Push space, push 5.
 8m          Call the 8th line index.
   2m        Call the 2nd line index.
     9       Push space, push 9.
      bm     Call the 11th line index.

This code is ridiculous. It's verbose form is:

toggle single quote;
 ;
toggle single quote;
save top as permanent variable;
push 1;
add top two;
goto top method;
:toggle single quote;
k;
push 12;
push 10;
flatten top two stacks;
q;
toggle single quote;
output stack as chars;
:toggle single quote;
U;
clone current stack;
go forward;
toggle single quote;
output stack as chars;
:push 5;
push 8;
goto top method;
push 5;
goto top method;
push 6;
goto top method;
toggle single quote;
 ;
 ;
 ;
go forward;
 ;
toggle single quote;
output stack as chars;
push 5;
goto top method;
push 6;
goto top method;
toggle single quote;
U;
U;
U;
toggle single quote;
push 6;
push 8;
goto top method;
:push 15;
push 3;
add top two;
push 11;
goto top method;
push 9;
goto top method;
push 10;
goto top method;
push 12;
goto top method;
push 9;
push 8;
goto top method;
push 2;
goto top method;
push 6;
push 11;
goto top method;
push 12;
goto top method;
push 10;
goto top method;
push 9;
goto top method;
push 15;
push 6;
add top two;
push 8;
goto top method;
push 3;
repeat next instruction set top times;
begin recursive area;
push 2;
goto top method;
end recursive area;
:push 10;
toggle single quote;
U;
 ;
 ;
 ;
U;
toggle single quote;
output stack as chars;
:toggle single quote;
 ;
clone current stack;
 ;
 ;
go forward;
toggle single quote;
output stack as chars;
push 5;
goto top method;
:output stack as chars;
push 2;
goto top method;
push 9;
push 8;
goto top method;
push 10;
push 2;
goto top method;
:repeat next instruction set top times;
save top as permanent variable;
output stack as chars;
:push 2;
goto top method;
push 15;
push 11;
goto top method;
:save top as permanent variable;
save top as permanent variable;
save top as permanent variable;
output stack as chars;
push 2;
goto top method;
push 7;
push 8;
goto top method;
push 2;
goto top method;
save top as permanent variable;
save top as permanent variable;
push 7;
goto top method;
:push 8;
goto top method;
push 7;
goto top method;
:push 6;
push 8;
goto top method;
push 2;
goto top method;
push 9;
push 11;
goto top method;
share|improve this answer
    
@Adnan I linked in the wrong link and copied the wrong code. ಠ_ಠ Fixed. – VTCAKAVSMoACE Feb 26 at 0:30
    
It works now. Very nice! :) – Adnan Feb 26 at 0:31

JavaScript ES6, 232 223 203 bytes

Saved 29 bytes thanks to ETHproductions!

n=>[`quack`,r=`>:U`,`     U   U
>  : U   U
 >   U   U
>  : U   U
      UUU`,`0000002
1000002
0100 1  2
0010002
0001002
0010002
0100 1  2
1000002
0000000111`.replace(/\d/g,N=>[g=`   `,r,r+g+g+g+r][N])][n]

Test it out!

A=n=>[`quack`,r=`>:U`,`     U   U
>  : U   U
 >   U   U
>  : U   U
      UUU`,`0000002
1000002
0100 1  2
0010002
0001002
0010002
0100 1  2
1000002
0000000111`.replace(/\d/g,N=>[g=`   `,r,r+g+g+g+r][N])][n]

(a.onchange=a.onkeydown=a.onkeyup=function(){
 o.innerHTML = A(+a.value);
})();
*{font-family:"Consolas",monospace;font-size:20px;white-space:pre-wrap}input{left:50%;width:150px;position:absolute;text-align:center;margin-left:-75px;}#o{margin:15px;border:1px solid #00ACDC;padding:15px;}
<input id=a type=number value=0 min=0 max=3><br><div id=o></div>

share|improve this answer
    
surely it can be atob'd... if not, could the 1 and 0s be made into a trinary number? – Upgoat Feb 26 at 0:50
    
@Downgoat Maybe! I'll try that. – Cᴏɴᴏʀ O'Bʀɪᴇɴ Feb 26 at 0:53
1  
@Downgoat btoaing and trinary both are longer. – Cᴏɴᴏʀ O'Bʀɪᴇɴ Feb 26 at 0:57
    
@ETHproductions... that might work! – Cᴏɴᴏʀ O'Bʀɪᴇɴ Feb 26 at 1:16
    
@ETHproductions It did work! :D – Cᴏɴᴏʀ O'Bʀɪᴇɴ Feb 26 at 1:19

MATL, 283 182 bytes

101 bytes saved thanks to @Adnan!

This uses decimal numbers to encode indices 0...3 to the four characters for cases 2 and 3. The decimal numbers could perhaps be compressed using base-64 encoding, but quack!

For the case 3 a very nice trick suggested by @Adnan is used: define binary sequences to encode each row, where 0 and 1 correspond to space and >:U respectively.

~?'quack'}G1=?'>:U'}G2=?' :>U'12336 8466480h2109488h8466480h4032h4YA47-)}268697600 67174401h16795656h67174464h67174912h67174464h16795656h67174401h14680064h"@BP48+]Xh49'>:U'YX48 32YXc

Try it online!

share|improve this answer
3  
How about for the last case, using this, replacing the 1 with spaces and the 0 with the >:U string. I don't know if that would help. – Adnan Feb 25 at 23:52
    
@Adnan That's a good idea! – Luis Mendo Feb 25 at 23:53
5  
@Adnan 101 bytes saved :-O – Luis Mendo Feb 26 at 0:21
    
That is amazing! Very nice answer! :) – Adnan Feb 26 at 0:24

Cinnamon Gum, 76 bytes

0000000: 6c33 502b 2c4d 4cce b636 54b3 b30a b536  l3P+,ML..6T....6
0000010: 5253 0081 5010 e6b2 5350 b082 3215 ecb0  RS..P...SP..2...
0000020: 8a42 1487 865a 1bab 2960 00a0 79c8 6c2e  .B...Z..)`..y.l.
0000030: 2026 a002 4221 0430 55c0 5938 cd40 9720   &..B!.0U.Y8.@. 
0000040: 6c06 6177 90e9 17ac 4102 4100            l.aw....A.A.

Non-competing since Cinnamon Gum was created after this challenge.

Try it online.

Explanation

The first byte l specifies the mode: in this case it tells Cinnamon Gum to enter lookup table mode. Cinnamon Gum then decompresses the rest of the string (which has been compressed with zopfli --deflate) into this:

0&quack;1&>:U;2&     U   U
>  : U   U
 >   U   U
>  : U   U
      UUU;3&                  >:U         >:U
>:U               >:U         >:U
   >:U       >:U  >:U         >:U
      >:U         >:U         >:U
         >:U      >:U         >:U
      >:U         >:U         >:U
   >:U       >:U  >:U         >:U
>:U               >:U         >:U
                     >:U>:U>:U

It then splits by ;, puts each key-value pair (with & as delimiter) in a dictionary, and outputs dictionary[input].

share|improve this answer

GML, 265 bytes

A port of @kittycat3141's excellent answer, with the suggestion to golf it further with two new variables (which I named G and H) by @VoteToClose. I also managed to shorten it further with GML's relatively loose syntax.

d=">:U"A=" "B=A+A;C=B+B;D=C+C;G=D+A;H=C+B;a="U"+B+" U"+"#"c=d+G+d+"#"E=B+A;F=C+E;x[0]="quack"x[1]=d;x[2]=C+A+a+">"+B+": "+a+" >"+E+a+">"+B+": "+a+H+"UUU"x[3]=D+D+B+c+d+D+F+c+B+A+d+F+d+B+c+H+d+G+c+G+d+H+c+H+d+G+c+E+d+F+d+B+c+d+D+F+c+D+D+C+A+d+d+d;return x[argument0]
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.