17
\$\begingroup\$

In traditional FizzBuzz, you are asked to print the numbers from 1 to 100, but replacing every multiple of 3 with "Fizz", every multiple of 5 with "Buzz", and every multiple of both 3 and 5 (i.e. 15) with "FizzBuzz".

However, as an evil interviewer, I've invented my own twisted version of FizzBuzz, which I've decided to name BizzFuzz and give to you in a coding interview.

The rules of the game are:

  • Print each number from 1 to 100, except if the number meets one of the conditions below.

    • If the number is divisible by 4, print "Fizz".
    • If the number is divisible by 5, print "Buzz".

    • If the number is divisible by 4, but the number immediately after it is divisible by 5, print "FizzBuzz" instead of "Fizz", and print the next number regularly.

    • If the number is divisible by 5, but the number immediately after it is divisible by 4, print "BuzzFizz" instead of "Buzz", and print the next number regularly.

    • If the number is immediately before a number divisible by both 4 and 5, print "Bizz".

    • If the number is immediately after a number divisible by both 4 and 5, print "Fuzz".

    • If the number is divisible by both 4 and 5, print "BizzFuzz".

The shortest code to implement all these rules in any language wins.

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

GolfScript (83 80 chars)

(NB Howard's suggestion in the comments allows to reduce to 78 chars, but with trailing spaces on some lines).

This uses the character \0, so here it is in xxd format:

0000000: 3130 302c 7b29 2e32 3025 2742 6946 750a  100,{).20%'BiFu.
0000010: 0046 750a 0000 0046 6942 750a 0000 0000  .Fu....FiBu.....
0000020: 4669 0a00 0042 750a 0000 4669 0a00 0000  Fi...Bu...Fi....
0000030: 4275 4669 0a00 0000 0042 690a 2731 2c2f  BuFi.....Bi.'1,/
0000040: 3d32 2f27 7a7a 272a 5c6e 2b6f 727d 2f0a  =2/'zz'*\n+or}/.

and base64:

MTAwLHspLjIwJSdCaUZ1CgBGdQoAAABGaUJ1CgAAAABGaQoAAEJ1CgAARmkKAAAAQnVGaQoAAAAA
QmkKJzEsLz0yLyd6eicqXG4rb3J9Lwo=

Using ^ as a stand-in for \0, it's

100,{).20%'BiFu
^Fu
^^^FiBu
^^^^Fi
^^Bu
^^Fi
^^^BuFi
^^^^Bi
'1,/=2/'zz'*\n+or}/

Still not a particularly interesting problem.


An explanation was requested:

For values 0 to 99 inclusive:

100,{
...
}/

Increment the value (we want 1 to 100) and also find out what the incremented value is mod 20:

).20%

Split the magic string around \0 characters:

MAGIC_STRING 1,/

Take the (x mod 20)th element of that array, split it into 2-character chunks, and glue them back together with zz. Note: the string is either empty (in which case there are no chunks, so we end up with the empty string) or is a sequence of [BF][iu] prefixes followed by a newline.

=2/'zz'*

Take the other copy of the incremented number which we kept on the stack, and append a newline. Now whichever string we keep will end with a newline.

\n+

Apply a fallback operation. (This is similar to || in JavaScript or COALESCE in SQL).

or
\$\endgroup\$
  • \$\begingroup\$ @minitech, typo introduced by not copy-pasting because reindenting everything is a pain in the neck. Fixed. \$\endgroup\$ – Peter Taylor Aug 16 '13 at 20:26
  • 2
    \$\begingroup\$ reindenting everything = CTRL+K \$\endgroup\$ – John Dvorak Aug 16 '13 at 20:27
  • \$\begingroup\$ Can you explain this? \$\endgroup\$ – Johannes Kuhn Aug 16 '13 at 20:55
  • \$\begingroup\$ "Not a very interesting problem." Yeah, I had a hunch that was the case when I posted it. Still, it is a variation. \$\endgroup\$ – Joe Z. Aug 17 '13 at 0:10
  • \$\begingroup\$ It also gets more interesting as a parlour game with friends, rather than a programming challenge. Take turns trying to say the numbers in order. It's basically FizzBuzz Hardcore Edition if you don't memorize the sequence beforehand. \$\endgroup\$ – Joe Z. Aug 17 '13 at 0:26
13
\$\begingroup\$

Python, 114

a='Fizz'
b='Buzz'
c='Bizz'
d='Fuzz'
e=c+d
f=a+b
g=b+a
i=1
exec"print eval('ediifiiiaibiaiigiiic'[i%20]);i+=1;"*100

Original solution (131):

f='Fizz'
for i in range(1,101):x=i%20;print('Bizz'*(x%19<1)+'Fuzz'*(x<2)or(i%4<1)*f+'Buzz'*(i%5<1or x==4)+f*(x==15)or i,i)[x%11==5]
\$\endgroup\$
  • 3
    \$\begingroup\$ A truly beautiful abuse of eval \$\endgroup\$ – user8777 Aug 17 '13 at 6:13
3
\$\begingroup\$

Python 2, 131

F,B,Z,I='Fizz','Buzz','Fuzz','Bizz'
for i in range(1,101):print{5:Z,19:I,i%4:B,i%5*4:F,3:B+F,16:F+B,0:I+Z,1:i,4:i}.get(i%4+i%5*4,i)
\$\endgroup\$
  • 1
    \$\begingroup\$ You can golf this down to 154 by using a) logic short circuiting on the expression to print, b) reversing the meaning of r to calculate it in fewer characters, c) putting everything on one line: r,F,B,Z,I=1,'Fizz','Buzz','Fuzz','Bizz'\nfor i in range(1,101):a,b=i%4,i%5*4;print~-r and i or{5:Z,19:I,a:B,b:F,3:B+F,16:F+B,0:I+Z}.get(a+b,i);r=3!=a+b!=16 \$\endgroup\$ – Reinstate Monica Aug 16 '13 at 22:38
  • \$\begingroup\$ @WolframH: Thanks! I applied those with some changes to make 143. \$\endgroup\$ – Ry- Aug 17 '13 at 2:22
  • \$\begingroup\$ Ok this has me beat, impressive work. I love print 1!=a+b!=4 that is diabolical! \$\endgroup\$ – user8777 Aug 17 '13 at 5:53
  • 1
    \$\begingroup\$ @LegoStormtroopr: Nah. The exec/eval combo is diabolical ;) \$\endgroup\$ – Ry- Aug 17 '13 at 5:58
  • \$\begingroup\$ Yeah, that's gonna be hard to beat. I was able to squeeze and extra few form yours though by changing the print section to print{5:Z,19:I,a:B,b:F,3:B+F,16:F+B,0:I+Z,1:i,4:i}.get(a+b,i) \$\endgroup\$ – user8777 Aug 17 '13 at 6:09
1
\$\begingroup\$

An ungolfed reference implementation in Python that implements every rule literally (420 chars):

n = 1
while(n <= 100):
    if(n % 20 == 0):
        print "BizzFuzz"
    elif((n - 1) % 20 == 0):
        print "Fuzz"
    elif((n + 1) % 20 == 0):
        print "Bizz"
    elif(n % 5 == 0 and (n + 1) % 4 == 0):
        print "BuzzFizz"
        print n + 1
        n += 1
    elif(n % 4 == 0 and (n + 1) % 5 == 0):
        print "FizzBuzz"
        print n + 1
        n += 1
    elif(n % 4 == 0):
        print "Fizz"
    elif(n % 5 == 0):
        print "Buzz"
    else:
        print n
    n += 1
\$\endgroup\$
  • 3
    \$\begingroup\$ Hey. You got parentheses in my Python. \$\endgroup\$ – Ry- Aug 16 '13 at 20:03
  • \$\begingroup\$ I think the IDE I was using inserted them by default. I was on a Windows machine, and trying to code using a text editor on Windows is brutal. \$\endgroup\$ – Joe Z. Aug 17 '13 at 4:44
1
\$\begingroup\$

Python, 150

This is derivative of minitechs (earlier) answer, but I've squeezed enough out of it to make my own:

F,B,Z,I='Fizz','Buzz','Fuzz','Bizz'
for i in range(1,101):a,b=i%4,i%5*4;print i if a+b in [1,4] else {5:Z,19:I,a:B,b:F,3:B+F,16:F+B,0:I+Z}.get(a+b,i)

The degolfed version isn't much more readable, but the r that minitech was using is only triggered if on the next iteration the sum if a,b was either 1,0 or 0,4, which is equivalent to i%4 or i%5 == 0so it was only going to appear in those circumstance. So it was possible to remove the assignment and calculation of r and derive it from the current value of i using a and b:

F,B,Z,I='Fizz','Buzz','Fuzz','Bizz'
for i in range(1,101):
 a,b=i%4,i%5*4;
 print i if a+b in [1,4] else {5:Z,19:I,a:B,b:F,3:B+F,16:F+B,0:I+Z}.get(a+b,i)

It also includes @WolframH's suggestions.

\$\endgroup\$
  • \$\begingroup\$ Er, it’s not really different. Anyways, take away the spaces around [1,4] (and use (1,4) or {1,4} besides) and before {. \$\endgroup\$ – Ry- Aug 17 '13 at 2:20
  • \$\begingroup\$ Not different no, just changed enough that I thought it was worth an entry. \$\endgroup\$ – user8777 Aug 17 '13 at 5:52
  • \$\begingroup\$ Okay, but you should golf it for whitespace. \$\endgroup\$ – Ry- Aug 17 '13 at 5:52
  • \$\begingroup\$ @minitech no worries. Cheers for the tip on the [] spacing. I was wondering why you used {} in your original answer. \$\endgroup\$ – user8777 Aug 17 '13 at 5:56
1
\$\begingroup\$

R: 170 characters

a=b=1:100
o=!a%%4
i=!a%%5
w=o&i
a[o]="Fizz"
a[i]="Buzz"
a[c(i,F)&c(F,o)]="FizzBuzz"
a[c(F,i)&c(o,F)]="BuzzFizz"
a[w[-1]]="Bizz"
a[c(F,w)]="Fuzz"
a[w]="BizzFuzz"
cat(a[b])
\$\endgroup\$
0
\$\begingroup\$

Tcl, 185 chars

while {[incr i]<101} {puts [expr {$i%4?$i%5?($i-1)%4|($i-1)%5?($i+1)%4|($i+1)%5?$i:"Fuzz":"Bizz":($i-1)%4?($i+1)%4?"Buzz":"BuzzFizz":$i:($i-1)%5?($i+1)%5?$i%5?"Fizz":"FizzBuzz":$i:$i}]}
\$\endgroup\$
  • \$\begingroup\$ Sometimes while loops can be replaced by time constructs to save bytes \$\endgroup\$ – sergiol Nov 9 '17 at 23:42

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.