12
\$\begingroup\$

Here's one for fans of Cabin Pressure. There is an episode in which the characters devise a new version of Fizz Buzz, which is simplified to contain absolutely no mathematics.

It has these rules:

  • If someone says "fizz", you say "buzz".
  • If someone says "buzz", you sing "'ave a banana"
  • If someone says your name, you say "fizz".

The result is that whenever a name is uttered, that person says "fizz" and the sequence has to run to completion.


Let's write some code.

I'd like you to write a program/function/whatever which works thus:

  • It accepts one single string of text as input.
  • If the string equals the name of the language your code is written in, it outputs "fizz".
  • If the string equals "fizz", it outputs "buzz".
  • If the string equals "buzz", it outputs "'ave a banana" (note the apostrophe at the start).
  • If the input is not one of these things, it should terminate.
  • Here's the kicker: The output string must go to two places.
    • Output to be seen by the user
    • Back into your code as input
  • I don't really care if they are output in each iteration, or build a string for eventual output.
  • Outputs must be separated by new lines (in console or result string)

Rules

  • This is code golf, write in any language you like and attempt to make your code as small as possible.
  • Standard loopholes apply.
  • I'd like to see links to an online interpreter.
  • The language name can be a full name or common short-form of the language the answer is written in. E.g. JS is acceptable, but shortening Ruby to R is not).

Test Cases

Input

'buzz'

Output

'ave a banana

Input

'fizz'

Output

buzz
'ave a banana

Input

ruby # or the name of the language your answer is written in

Output

fizz
buzz
'ave a banana

Input

something else

No output

\$\endgroup\$
16
  • 2
    \$\begingroup\$ What is the name of my language? \$\endgroup\$
    – Adám
    Jun 8, 2020 at 15:50
  • 1
    \$\begingroup\$ @Adám Whichever language you are writing your answer in. Is there a better way I could express that? \$\endgroup\$
    – AJFaraday
    Jun 8, 2020 at 15:53
  • 1
    \$\begingroup\$ @KrzysztofSzewczyk Nope. \$\endgroup\$
    – Adám
    Jun 8, 2020 at 16:17
  • 1
    \$\begingroup\$ @AJFaraday I meant to ask what kind of freedom we have. JavaScript/js/ES6; APL/Dyalog APL (Unicode); Python/Python 3/Python 3.8… \$\endgroup\$
    – Adám
    Jun 8, 2020 at 16:18
  • 7
    \$\begingroup\$ But what happens if your name is Buzz? Or Banana? \$\endgroup\$
    – darrylyeo
    Jun 9, 2020 at 1:32

14 Answers 14

12
\$\begingroup\$

perl -M5.010 -n, 47 bytes

"Perl\nfizz\nbuzz\n'ave a banana"=~/\b$_/;say$'

Try it online!

Prints whatever is following the input, or nothing if there is no match. Assumes input is newline terminated.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Nice! Can't get anything shorter using lists... You can save three bytes using literal newlines though! \$\endgroup\$ Jun 8, 2020 at 20:28
7
\$\begingroup\$

Jelly, 26 bytes

“Çȥȧ>$ỌĿɦ@⁴Ƙ½Æ/ṠṫḞƇ»Ỵṣ⁸ḊẎY

Try it online!

How?

“...»Ỵṣ⁸ḊẎY - Link: list of characters, W
“...»       - compressed string = "Jelly\nfizz\nbuzz\n'ave a banana"
     Ỵ      - split at newlines = ["Jelly","fizz","buzz","'ave a banana"]
      ṣ     - split at:
       ⁸    -   chain's left argument, W  e.g. "Jelly" -> [[],["fizz","buzz","'ave a banana"]]
        Ḋ   - dequeue                                   = [["fizz","buzz","'ave a banana"]]
         Ẏ  - tighten                                   = ["fizz","buzz","'ave a banana"]
          Y - join with new lines                       = "fizz\nbuzz\n'ave a banana"
\$\endgroup\$
6
\$\begingroup\$

Bash, 63 bytes

bash=fizz
fizz=buzz
buzz="'ave a banana"
echo ${x=${!1}}&&$0 $x

Try it online!

This requires . to be in your PATH. If that's not acceptable, then replace $0 with ./$0 (assuming the program is being run from the current working directory) at the cost of 2 bytes (65 bytes total).

Input is passed as an argument, output is on stdout. The language name is entered as bash.

(There's spurious output to stderr, but that's OK under our generic rules.)

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

JavaScript (ES6), 64 bytes

Expects "js" for the language name. Returns an array of strings.

s=>[k="js","fizz","buzz","'ave a banana"].filter(w=>k*(k|=s==w))

Try it online!

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

Retina 0.8.2, 56 bytes

$
¶Retina¶fizz¶buzz¶'ave a banana
^(.*¶)(.*¶)*?(\1|.*$)

Try it online! Link includes test cases. Explanation:

$
¶Retina¶fizz¶buzz¶'ave a banana

Append the possible inputs and outputs.

^(.*¶)(.*¶)*?(\1|.*$)

Try to delete only up to and including a line matching the original input. If this is not possible, then just delete everything.

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

05AB1E, 46 33 bytes

’
05AB1E
fizz
ÒÖ
'ž™ a æé’I¶.ø¡¦θ

-13 bytes by porting @Abigail's Perl answer, so make sure to upvote her!!

Outputs [] for invalid inputs.

Try it online.

Original 46 bytes approach:

"fizz"U•äƵí•hRQiX=}XQi'ÒÖ=}'ÒÖQi’'ž™ a æé’,}õ?

Outputs nothing for invalid inputs.

Try it online.

Explanation:

’
05AB1E
fizz
ÒÖ
'ž™ a æé’         '# Push dictionary string "\n05AB1E\nfizz\nbuzz\n'ave a banana"
         I         # Push the input
          ¶.ø      # Surround it with leading and trailing newline
             ¡     # Split the string on this
              ¦    # Remove the first part (for invalid inputs)
               θ   # Pop and only leave the last part (or an empty list)
                   # (and output it implicitly as result)

"fizz"U            # Puts "fizz" in variable `X`
•äƵí•              # Push compressed integer 14793296
     h             # Convert it to hexadecimal: E1BA50
      R            # Reverse it to 05AB1E
       Qi  }       # If the (implicit) input-string is equal to this:
         X         #  Push "fizz" from variable `X`
          =        #  Print it with trailing newline without popping
X                  # Push "fizz" from variable `X`
 Qi    }           # If the top of the stack equals "fizz",
                   # which will use the (implicit) input if the stack is empty:
   'ÒÖ            '#  Push dictionary string "buzz"
      =            #  Print it with trailing newline without popping
'ÒÖQi           } '# If the top of the stack (or implicit input) equals "buzz":
     ’'ž™ a æé’   '#  Push dictionary string "'ave a banana"
               ,   #  Pop and print it
õ?                 # Print "" without newline
                   # (for invalid input, which otherwise would be output implicitly)

See this 05AB1E tip of mine (sections How to use the dictionary? and How to compress large integers?) to understand why •äƵí• is 14793296; 'ÒÖ is "buzz"; and ’'ž™ a æé’ is "'ave a banana".

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

Python 3, 77 76 bytes

t="Python","fizz","buzz","'ave a banana"
*map(print,t[t.index(input())+1:]),

Try it online!

Takes input from STDIN, and print the results to STDOUT.

\$\endgroup\$
2
  • \$\begingroup\$ @JoKing that's really cool, thanks! Not sure why putting the comma makes it work. \$\endgroup\$ Jun 9, 2020 at 5:14
  • 3
    \$\begingroup\$ It is the creation of a tuple (defined by a single comma) and using PEP 448 that makes it work. \$\endgroup\$ Jun 9, 2020 at 8:23
4
\$\begingroup\$

APL (Dyalog Unicode), 43 bytes

'APL' 'fizz' 'buzz' '''ave a banana'(↑⍳↓⊣)⊂

Try it online!

List of string literals is quite expensive...

How it works

S←'APL' 'fizz' 'buzz' '''ave a banana'  ⍝ Let's call this array S
S(↑⍳↓⊣)⊂  ⍝ The function
S( ⍳  )⊂  ⍝ 1-based index of the input in S, 5 if not found
    ↓⊣    ⍝ Drop that many items from the start of S
  ↑       ⍝ Convert the remaining items to be placed on each line
\$\endgroup\$
4
\$\begingroup\$

Java (OpenJDK 8), 117 bytes

void a(String s){System.out.println(s=s=="fizz"?"buzz":s=="buzz"?"'ave a banana":s=="java"?"fizz":"");if(s!="")a(s);}

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ 112 bytes (because the OP says it's OK not to follow the method as long as the output is the correct one) \$\endgroup\$ Jun 10, 2020 at 7:18
3
\$\begingroup\$

Charcoal, 39 bytes

≔⪪“Jε(h&]⦄_⁷¦⊗‹f·ⅈ⦄⊗x⍘ς3➙A⁸“↑”¶υΦυ№…υκθ

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

≔⪪“Jε(h&]⦄_⁷¦⊗‹f·ⅈ⦄⊗x⍘ς3➙A⁸“↑”¶υ

Split the string Charcoal\nfizz\nbuzz\n'ave a banana on newlines and save the result in a variable.

Φυ№…υκθ

Filter on the result and show only those entries that appear after the input.

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

Python 2, 102 79 bytes

a='fizz'
b='buzz'
def f(s):t={'Python':a,a:b,b:"'ave a banana"}[s];print t;f(t)

Try it online!

Uses a different approach from the other answer, recursive function

Edit: Thanks @SurculoseSputum for saving 23 bytes!

\$\endgroup\$
3
  • \$\begingroup\$ You can get it down to 91 bytes by rearranging everything a bit. \$\endgroup\$ Jun 9, 2020 at 5:28
  • \$\begingroup\$ 79 bytes if terminating the function with exception is allowed. \$\endgroup\$ Jun 9, 2020 at 5:30
  • \$\begingroup\$ @SurculoseSputum well, your answer uses exceptions i think, so sure, thanks! :) \$\endgroup\$
    – Dion
    Jun 9, 2020 at 6:35
3
\$\begingroup\$

Brachylog, 52 47 bytes

∧"Brachylog
fizz
buzz
'ave a banana"ṇ;?⟨a₁h⟩b~ṇ

Try it online!

The predicate fails on inputs on which it should "terminate". If outputting an unbound variable is more desirable, +2 bytes for .∨; if an empty string is necessary, +1 on top of that for .

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

C (gcc), 115 \$\cdots\$ 105 98 bytes

Saved 4 bytes thanks to ceilingcat!!!

Saved 7 bytes thanks to Neil!!!

i;*y[]={"c","fizz","buzz","'ave a banana"};f(char*s){for(i=0;i<3;)strcmp(s,y[i++])||puts(s=y[i]);}

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ I think strcmp(s,y[i++])||puts(s=y[i]); saves you 7 bytes. \$\endgroup\$
    – Neil
    Jun 9, 2020 at 9:59
  • \$\begingroup\$ @Neil Clever, was thinking there must be a away to just keep on looping - thanks! :-) \$\endgroup\$
    – Noodle9
    Jun 9, 2020 at 12:04
2
\$\begingroup\$

Kotlin, 103 bytes

fun f(x:String){listOf("fizz","buzz","'ave a banana").fold("Kotlin"){a,b->if(x==a){println(b);f(b)};b}}

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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