-1
\$\begingroup\$

Like the previous SmokeDetector challenge? This one is for you too ;)

Note: SmokeDetector is a bot that detects spam and offensive posts on the network and posts alerts to chat.

What to implement

Smokey's !!/coffee command uniformly at random picks one of these words (sorts of coffee):

  • Espresso
  • Macchiato
  • Ristretto
  • Americano
  • Latte
  • Cappuccino
  • Mocha
  • Affogato
  • jQuery

adds it to the message: "brews a cup of $COFFEE_SORT for $USERNAME"

Your task is to implement the function that takes a username as any reasonable input format, randomly (uniformly at random) picks $COFFEE_SORT, and prints to the screen:

brews a cup of $COFFEE_SORT for $USERNAME

Rules

Regular code-golf — the shortest answer in bytes wins ;)

Good luck!

\$\endgroup\$
13
  • 3
    \$\begingroup\$ Have you tried testing this question in the sandbox before? \$\endgroup\$ – Razetime Oct 25 '20 at 14:09
  • \$\begingroup\$ @Razetime nope, where's it? (and what is it???) ;) \$\endgroup\$ – Victor VosMottor Oct 25 '20 at 14:10
  • 1
    \$\begingroup\$ I don't understand what are we supposed to golf in these 2 challenges. \$\endgroup\$ – the default. Oct 25 '20 at 14:32
  • 1
    \$\begingroup\$ @VictorVosMottor It is unclear what interesting golfing can be done in them. Right now, the most obvious approach is to implement the function that gets a username as argument, randomly (uniformly at random) picks $COFFEE_SORT and prints to the screen, and it's really unlikely that someone will ever come up with a better one. \$\endgroup\$ – the default. Oct 25 '20 at 14:35
  • 12
    \$\begingroup\$ This seems to me like a borderline duplicate to the first challenge. They're basically the same, but this one has "print a string first" and "print the input" added on to it. It's basically a combination of the cat challenge and the first challenge \$\endgroup\$ – caird coinheringaahing Oct 25 '20 at 15:07

16 Answers 16

7
\$\begingroup\$

Jelly, 60 bytes

“¦ƘÇ=X⁸ẓÇsŻY»⁶“¢p ɼƁỴŒ?Ọi^gċ{çØLƲ`ẆĿJ$ȮȷẈv&ẋȧ>ċ8½c»ḲX⁶“Ƙa»⁶³

Try it online!

If you write a nilad while there's an argument Jelly just dumps it into STDOUT, which is really nice for this challenge because you don't need to worry about maintaining the list of output characters but rather just dump the outputs piece by piece.

Explanation

“¦ƘÇ=X⁸ẓÇsŻY»⁶“...»ḲX⁶“Ƙa»⁶³  Main Link
“¦ƘÇ=X⁸ẓÇsŻY»                 "brews a cup of coffee for"
             ⁶                " " (dumps previous string to output)
              “...»           compressed string with space-separated coffee types
                   Ḳ          split on spaces
                    X         choose a random value
                     ⁶        " " (dumps the coffee type to output)
                      “Ƙa»    "for" (dumps the space to output)
                          ⁶   " " (dumps "for" to output)
                           ³  get the command line argument (implicit output)

It's the same length to compress " for" as to place a space and then compress "for". Unfortunately Jelly compressed strings don't seem to support trailing spaces, which unlike most situations actually matters here.

\$\endgroup\$
2
  • \$\begingroup\$ Could using Jelly's string separator help with the coffee types, or is it shorter to compress with spaces? \$\endgroup\$ – caird coinheringaahing Oct 25 '20 at 14:39
  • \$\begingroup\$ @cairdcoinheringaahing it's 8 bytes longer :( \$\endgroup\$ – HyperNeutrino Oct 25 '20 at 14:41
7
\$\begingroup\$

05AB1E, 73 72 bytes

Thanks to Kevin Cruijssen for -1 byte.

.•9‰dx$»w¯Ï»Å
0Ž4÷ßû‡Qβ%*mÏaQqîu<´Q™Õj•#™”„ƒoj¡¸”á9ô«Ω“brews a‚ ÿ€‡ ÿ

Try it online!

05AB1E does have a rather small dictionary, the only words we use here are American and Query.

.•9‰ ... Õj•q       # compressed alphabet string
                    # "espresso macchiato ristretto latte cappuccino mocha affogato"
#                   # split on spaces
 ™                  # title case each word
  ”„ƒoj¡¸”          # compressed dictionary string "Americanoj Query"
          á         # take the letters
           9ô       # split into groups of 9
             «      # concat both lists
              Ω     # choose a random element
“brews a‚ ÿ€‡ ÿ  # compressed alphabet string
                    # "brews a cup of ÿ for ÿ"
                    # where ÿ is implicitly replaced with
                    # the coffee type and the input

Try it with step-by-step output!

See Kevin Cruijssen's tip to understand how all these different string compression methods work.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ ðK can be á for -1. \$\endgroup\$ – Kevin Cruijssen Oct 26 '20 at 9:38
6
\$\begingroup\$

Charcoal, 80 bytes

”↶⌊<σ⪪|ς∨▶?ρ”‽⪪”>,,↶‽¿#Z\`P➙⮌\`ρL±5⭆À)⧴YQ>δAτ1¬▶⌈…⌊$h¿SÀüτt9W➙β⁰№LασUL⧴§⧴”B¦ for S

Try it online! Link is to verbose version of code. Explanation:

”↶⌊<σ⪪|ς∨▶?ρ”

Print compressed string brews a cup of .

‽⪪”>,,↶‽¿#Z\`P➙⮌\`ρL±5⭆À)⧴YQ>δAτ1¬▶⌈…⌊$h¿SÀüτt9W➙β⁰№LασUL⧴§⧴”B

Split compressed string EspressoBMacchiatoBRistrettoBAmericanoBLatteBCappuccinoBMochaBAffogatoBjQuery on B and print a random entry.

 for 

Print literal string for .

Print the input string.

\$\endgroup\$
1
4
\$\begingroup\$

Bash, 123 bytes

a=(Espresso Macchiato Ristretto Americano Latte Cappuccino Mocha Affogato jQuery)
echo brews a cup of ${a[RANDOM%9]} for $1

Try it online!

119 if $$ can be used instead of $RANDOM

\$\endgroup\$
4
\$\begingroup\$

Perl 5 (-p), 116 bytes

@a=(Espresso,Macchiato,Ristretto,Americano,Latte,Cappuccino,Mocha,Affogato,jQuery);s;;brews a cup of $a[9*rand] for 

Try it online!

114 if $$%9 can be used instead of 9*rand

\$\endgroup\$
4
\$\begingroup\$

Python 3, 159 156 153 bytes

-3 bytes thanks to @Neil

-3 bytes thanks to @ovs

from random import*
print("brews a cup of",choice("Espresso Macchiato Ristretto Americano Latte Cappuccino Mocha Affogato jQuery".split()),"for",input())

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ Because print takes a list, you can save 3 bytes by using ", instead of ` "+` etc. \$\endgroup\$ – Neil Oct 25 '20 at 14:57
  • \$\begingroup\$ "...".split splits on whitespace if given no argument, so replacing all commas by spaces saves 3 bytes. A lambda function is another 2 bytes shorter: tio.run/… \$\endgroup\$ – ovs Oct 25 '20 at 15:28
4
\$\begingroup\$

R, 139 137 bytes

cat("brews a cup of",sample(scan(,"",9),1),"for",scan(,""))
Espresso
Macchiato
Ristretto
Americano
Latte
Cappuccino
Mocha
Affogato
jQuery

Try it online!

-2 bytes by Dominic van Essen.

\$\endgroup\$
3
  • \$\begingroup\$ Nice. I always thought 'readlines' was kinda useless, but now I see that it's actually a pretty golfy way to input a character vector without all the ","s. \$\endgroup\$ – Dominic van Essen Oct 25 '20 at 16:05
  • \$\begingroup\$ ...although, not as golfy as scan, it seems: 137 bytes. \$\endgroup\$ – Dominic van Essen Oct 25 '20 at 16:08
  • \$\begingroup\$ Oh, indeed, scan also has a line limit argument, thanks! \$\endgroup\$ – Kirill L. Oct 25 '20 at 16:12
4
\$\begingroup\$

Scala, 150 bytes

s=>print("brews a cup of "+"Espresso#Macchiato#Ristretto#Americano#Latte#Cappuccino#Mocha#Affogato#jQuery".split("#")((math.random*9)toInt)+" for "+s)

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Pyth, 104 bytes

%." w#5Gœz,v7‡e´‚?×ÅvNžSÒ",Oc."	y3N.Yí&öPâèq}õÑ6É«’Õ%¯ä=Ķ/u8ƒµ;AF¢µ“€ä1•¥p.9sNšæãš<KU×Íí"\	z

Try it online!

Sorry for being rough on packed strings earlier, they help for shorter strings.

\$\endgroup\$
4
\$\begingroup\$

Retina, 111 110 bytes

K`EspressOMacchiatORistrettOAmericanOLatte¶CappuccinOMocha¶AffogatOjQuery
O
o¶
@L$`.+
brews a cup of $& for $+

Try it online! Explanation: The K` stage replaces the input with a list of types of coffee, except that a byte is saved by having the next stage expand O to o followed by a newline. The L`stage then randomly (@ is used here as it's a random match that gets selected) selects an element of the list, and $ tells it to output the phrase, substituting for the selected coffee and original input. Edit: I used @Leo's Retina Kolmogorov golfer to detect the duplication and save a byte.

\$\endgroup\$
3
\$\begingroup\$

Batch, 181 bytes

@set/ar=%random%%%9
@for %%a in (Espresso.0 Macchiato.1 Ristretto.2 Americano.3 Latte.4 Cappuccino.5 Mocha.6 Affogato.7 jQuery.8)do @if %%~xa==.%r% echo brews a cup of %%~na for %1

Explanation: Batch has no array type, so what I do is to loop over a list of strings and print the prefix if the suffix equals a random digit from 0-8. (Randomly choosing from 10 strings would have been slightly golfier.)

\$\endgroup\$
3
\$\begingroup\$

JavaScript, 163 148 bytes

-15 bytes thanks to @Arnauld

alert(`Brews a cup of ${'Espresso,Macchiato,Ristretto,Americano,Latte,Cappuccino,Mocha,Affogato,jQuery'.split`,`[Math.random()*9|0]} for `+prompt())

Fiddle

\$\endgroup\$
2
  • 2
    \$\begingroup\$ 148 bytes \$\endgroup\$ – Arnauld Oct 25 '20 at 16:43
  • \$\begingroup\$ @Arnauld creative, thanks :) \$\endgroup\$ – ΛRYΛN Oct 25 '20 at 19:40
2
\$\begingroup\$

C (gcc), 176 bytes

*s[]={"Espresso","Macchiato","Ristretto","Americano","Latte","Cappuccino","Mocha","Affogato","jQuery"};f(int*u){printf("brews a cup of %s for %s",s[rand(srand(time(0)))%9],u);}

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Python 3, 154 bytes

from random import*
print(f"brews a cup of {choice('Espresso Macchiato Ristretto Americano Latte Cappuccino Mocha Affogato jQuery'.split())} for",input())

This is basically @aidan0626's code, except I used f-strings to optimize the code further. They're so useful!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ python 3.7 and higher I must note \$\endgroup\$ – Victor VosMottor Oct 28 '20 at 11:42
2
\$\begingroup\$

05AB1E, 94 bytes

“¬àews€…‚ “’¡¾…›o î™Â¶€›o ¹´Ú•ˆÆ€„ „ƒo ¼È¶µ ¦§pÓ—Í«€¸ ›á£áa ÂÆÚƒ€›o ’™”j¡¸”á«ð¡Ω«ð«“€‡ “«s«

Try it online!

“¬àews€…‚ “                                                                   # push "brews a cup of "
              ’¡¾…›o î™Â¶€›o ¹´Ú•ˆÆ€„ „ƒo ¼È¶µ ¦§pÓ—Í«€¸ ›á£áa ÂÆÚƒ€›o ’™”j¡¸”á« # push "Espresso Macchiato Ristretto Americano Latte Cappuccino Mocha Affogato jQuery"
 ¡                                                                               # split
ð                                                                                # by spaces
   «                                                                             # concatenate top of stack with
  Ω                                                                              # random pick from list (this is $COFFEE_SORT)
         «                                                                       # concatenate top of stack with
    “€‡ “                                                                        # "for "
           «                                                                     # concatenate top of stack with
          s                                                                      # implicit input (this is $USERNAME)
                                                                                 # implicit output
\$\endgroup\$
1
\$\begingroup\$

PHP, 142 bytes

<?="brews a cup of ".explode(",","Espresso,Macchiato,Ristretto,Americano,Latte,Cappuccino,Mocha,Affogato,jQuery")[rand(0,8)]." for ".$argv[1];

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ You can save 3 bytes by using -F flag and $argn (and user passed as input instead of argument) \$\endgroup\$ – Kaddath Oct 27 '20 at 8:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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