FizzBuzz

The "Fizz-Buzz test" is an interview question designed to help filter out the 99.5% of programming job candidates who can't seem to program their way out of a wet paper bag. The text of the programming assignment is as follows:
"Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”."


is this a joke? I'm studying C for 2 weeks and I literally wrote the if statements in for this in 2 minutes

if( index%3 == 0 && index%5 != 0 ) printf("\nFizz");
if( index%5 == 0 && index%3 != 0 ) printf("\nBuzz");
if(index%3 == 0 && index%5 == 0) printf("\nFizzBuzz");
else if(index%3 != 0 && index%5 != 0) printf("\n%d", index);

Other urls found in this thread:

css-tricks.com/tales-of-a-non-unicorn-a-story-about-the-trouble-with-job-titles-and-descriptions/
joelgrus.com/2016/05/23/fizz-buzz-in-tensorflow/
github.com/EnterpriseQualityCoding/FizzBuzzEnterpriseEdition
twitter.com/AnonBabble

This is why
css-tricks.com/tales-of-a-non-unicorn-a-story-about-the-trouble-with-job-titles-and-descriptions/

>writing unnecessary conditions because he's too retarded to use else-ifs and else properly.

kys retard

wouldn't hire you with that shit code.
you are part of the 99.5%

literally this

Letting programmers name anything was a mistake.

these

give him a break he's a super beginner

is fizzbuzz real anyway? do they really ask you to do it?

OP here

you are right I forgot about

>Once an else if succeeds, none of the remaining else if's or else's will be tested.

how is this?

if(index%3 == 0 && index%5 == 0) printf("\nFizzBuzz");
else if(index%3==0) printf("\nFizz");
else if(index%5==0) printf("\nBuzz");
else printf("\n%d", index);

I have not had fizzbuzz in any of my application tests

it's real for shit tier cs jobs. I mean the quality of the work, not the pay. good cs jobs ask pretty interesting questions!

You don't need to give FizzBuzz its own line. That's a bad flag. At its roughest, it should be:

>Check if it's not a multiple. Print number if true.
>Print Fizz if applicable.
>Print Buzz if applicable.
>Print line break.

sub fizzbuzz ($n) {
given $n %% 3, $n %% 5 {
when ~(True, True ) { "fizzbuzz" }
when ~(True, False) { "fizz" }
when ~(False, True) { "buzz" }
$n
}
}


and some people find Perl6 hard.

Dumb meme. For perspective, I was asked to write a method to check if a binary tree is balanced for a sophomore summer CS internship. (among other coding questions)

for(int i = 0; i

if (i % 5 && i % 3 == 0)
user
you realize you wrote
if (i % 5 != 0 && i % 3 == 0)
you're part of the 99.5%

for ( n = 0; n < 100; n++){
if ( n % 3 == 0 ){
cout

You're part of the 99.5%.

Keep studying and don't be a pretentious asshole.

whoops :^)

This isn't what they do anymore. This is what they do now:

Step 1:
- Phone screen. Some contractor phones you and is typically as rude as possible/jackassery demanding you program over the fucking phone and asks you some questions. "What is the best way to sort X?" WRONG it's only X way".

Step 2:
- 'Founder' screen. If a startup some cofounder will phone you and ask you weird questions. 'What is a time you felt you were treated unfairly?". They will then shill all their bullshit culture and whatever in hopes of you joining the cult.

Step3:
You can invited for a week long interview (yes, a week usually longer). Basically you will have a huge series of 45min whiteboard interviews with various departments, all of them will by ivy league engineers trying to gotcha with stupid shit. All they really care at this part is you can explain your reasoning while you whiteboard program. They will continually ask you to optimize whatever you've done too. "balance this tree. How could you do it faster? Do it faster again blah blah".

Step4:
Monkey dancing continues with you typically doing a large work sample test that won't be like anything you will actually work on or applied for. Half way through you'll feel like you're working for free at this point and getting scammed.

Finally you get to negotiations where salary is discussed. This is where you discover after all that you'll only be offered a laughable salary. This is the current state of the art tech interview system unless you handpick a few startups that don't do this bullshit.

printf( (!(i&3) && !(i&5))?"FizzBuzz\n":!(i&3)?"Fizz\n":!(i&5)?"Buzz\n":"%d\n", i );

That's a step in the wrong direction.

Having a method print its result to stdout is already freshman tier bullshit. Now you're further writing yourself into that corner.

Ooops, used & instead of %. Oh well.

>mfw this is true if you interview
>mfw my good tier degree and FOSS contribs mean there are literally dozens of recruiters throwing salary quotes at me
The God tier way to get a job is to get recommendations from insider friends anyhow.

for(var i = 0; i

>Me: Ok, again to be honest, my JS knowledge is more regarding UI/UX based tasks. And I don't really understand the point of the question. Like, what's the use case? When would this come up in the role?

What the interviewer should have said was "We have a blog and we want to show ads on every 3rd post and every fifth one should be a sponsored story. Can you write "post, post, post with ad, post, sponsored story, ..."

Then she'd just say "oh,I see. I realize now that I'm a fraud. Sorry to waste your time." and maybe learn something instead of writing this blog post

lol

Same, that's how I got all my jobs before I just became a contractor. Still I had to whiteboard interview

import std.stdio;

void main() {
foreach (i ; 1 .. 101) {
if (i % 3 != 0 && i % 5 != 0)
writeln(i);
if (i % 3 == 0)
write("Fizz", (i % 5 != 0) ? "\n" : "");
if (i % 5 == 0)
writeln("Buzz");
}
}

Less retarded version:
import std.stdio;

void main() {
foreach (i ; 1 .. 101) {
if (i % 3 != 0 && i % 5 != 0)
write(i);
if (i % 3 == 0)
write("Fizz");
if (i % 5 == 0)
write("Buzz");
writeln();
}
}

OP here with another possible solution

if(index%3!=0 && index%5!=0) {printf("%d\n", index); continue;}
if(index%3==0) printf("Fizz");
if(index%5==0) printf("Buzz");
printf("\n");

I'm so glad I made this thread. I learned a lot.

and please dont tell me using continue is bad programming practice

you know you can still be part of the 99.5% even if you can do fizzbuzz right?

yes I was already exposed read the thread

even if you couldn't program this is a simple exercise in logic, the pseudo-code


if n is y and y fizz buzz
else if n = x fizz
else if n y buzz
print n

surely would have been possible with her "expertise" right?

#include
void main() {
const char *vars[] = { "%d ","Fizz ","Buzz ","FizzBuzz " };
for (int i = 1; i < 101; ++i)
printf(vars[(i % 3 == 0) + (i % 5 == 0)*2], i);
}

It's not a joke. I interview for a reasonably well-known tech firm; we use it as a weed-out/warmup question. It doesn't say if we *should* hire you, just if we *shouldn't*.

There's a significant number of applicants who get really tripped up on this kind of thing. Seriously; it's amazing.

can you sleep well at night?

Welcome to the 99.5%

I now use: "Count down from 700 to 200 in decrements of 13."

I'm actually retarded. It can be done with only this

if(index%3==0) printf("Fizz");
if(index%5==0) printf("Buzz");
if(index%3!=0 && index%5!=0) {printf("%d", index);}
printf("\n");

this is it. this must be the ultimate solution.

Can be done with two ifs.

var i = 700
while i >= 200 {
print(i)
i -= 13
}

Can you mess that up?(I somehow messed that up)

Fuck this I'm doing sales

how is that even hard?

Depending on the language, syntax might be off, but the logic is sound.

It is not hard.

It is like this: You are hiring a new pianist for your orchestra. An interviewee walks in, so you take them to a room with a piano, a drum, and a guitar.

"Point out the piano".

If the interviewee cannot point out the piano, what is the point of continuing the interview? So it goes with FizzBuzz, or "Count down from 700 to 200 in decrements of 13."

(map (fn fizzbuzz [n]
(cond
(and
(zero? (mod n 3))
(zero? (mod n 5))) (println "FizzBuzz")
(zero? (mod n 3)) (println "Fizz")
(zero? (mod n 5)) (println "Buzz")
:else
(println n)))
(range 1 (inc 100)))


That whiner got whats comming, just because she wants to programm UI doesn't mean she should't be able to handle data...

Yes that is right. You should be figuring out if the person really knows anything and possibly some problem solving. Most crap can be looked up but it takes years to learn some stuff. Also their personality. They can learn some skills on the job but being an asshole isn't something that can be fixed.

it's fucking amazing how people can't get their head around it being simple

I give up

can you please tell me how?

not him but maybe:
if(..)
if (..)
else

that doesnt work. I cant find such a solution on the internet either

D FUCK YES

...

I'm at a decent software company and we used to ask it to weed out fakers quickly.
Today we have an online test before the on-site interview that does more or less the same job.

Here's my solution written in JS. It only uses two if-statements.
It is meant to be as small as possible (there are smaller solutions).
for(i=1;i

>Why its a bad idea to code in one line/command

I dont understand this

what would it look like in C ?

You can't do that in C for at least 3 different reasons.

I just read this, and is this faggot seriously complaining about being tested on fucking FizzBuzz?
>pic related

You're doing it wrong kiddo.
I assume it's your first week coding, otherwise your wasting your time, go work at Burger King or something

cannot find symbol 'i'

its meant to be used in a for loop i'm guessing, cause i is always used in them

for (int i = 1; i

The ||-statement in your solution is an if-statement: if the value is "falthy" (empty or undefined) it gets set to i.

You're adding syntactic sugar, but it's still 3 if-statements.


There are many options:

1. Make a variable that gets set to fizz or buzz, check if it's empty --> if it's empty, print the number (the solution of the guy above)

2. if(i%15 == 0 ) --> "fizzbuzz", if(i%3 == 0 ) --> "fizz", if(i%5 == 0 ) --> "buzz"

Note that this solution does not work if you take numbers like 3 and 9 (each number which can be divided by 9 is already a "fizzbuzz", not each number which can be divided by 27).


3. Another optimization would be to do only two mod-operations for each number, since mod is quite expensive:
boolean_fizz = (i%3 == 0)
boolean_buzz = (i%3 == 0)

Checking your booleans afterwards costs almost nothing.


4. Higher optimization is about using "wheels":
You take two array with 100 booleans. For the first array you make a simple for-loop in steps of three and set every third value to "true". For the second array you repeat it with a for-loop in steps of 5. Step three is a simple for-loop that checks both boolean arrays.
You can completely avoid the modulo-operation, which is quite expensive.


5. A pretty mean hack is the "fizzbuzz of the christ", here in python, but you can also do it in C, since C looks at strings as an array of chars:
for i in range(1,101):
print("FizzBuzz"[i*i%3*4:8--i**4%5] or i)


Or with parentheses:

for i in range(1,101):
print("FizzBuzz"[((i**2)%3*4):(8-((-(i**4)))%5)] or i)


This is actually a pretty mathematical solution and works only because 5 and 3 are prime numbers. It uses a certain property of prime numbers:

"n^(n-1) mod n = 0", but "(n+k)^(n-1) mod n = 1" for all 0 < k < n

Check this out:
for i in range(1,101):
print((i**2)%3)


6. Using AI for "guessing" the numbers:
>joelgrus.com/2016/05/23/fizz-buzz-in-tensorflow/

for n in range(101):
if n%3==0:
print("fizz")
elif n%5==0:
print("butt")
else:
print(n)

Can I into code?

10 INT I
20 I = I + 1
30 IF I MOD 15
40 THEN PRINT "FIZZBUZZ"
50 GOTO 20
60 IF I MOD 3
70 THEN PRINT "FIZZ"
80 IF I MOD 5
90 THEN PRINT "BUZZ"
100 ELSE PRINT I
110 GOTO 20
120 END

>number coded basic
stop. That language ruined me forever.

Dat nostalgia..

using System;
using System.Text.RegularExpressions;
using static System.Console;
namespace fizzbuzz {
class Program {
static void Main(string[] args){
WriteLine(Regex.Replace(Regex.Replace("1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 ",
@"(\d+ \d+ )\d+",
"$1Fizz"),
@"([^ ]+ [^ ]+ [^ ]+ [^ ]+ )(\d+)?([^\d ]+)?",
"$1$3Buzz")
);
Console.ReadKey();
}
}
}

Holy shit, it actually works..

Heh, why did he delete his solution?

Oh I see, it's there:

> all those 4 division solutions posed as a good ones
No wonder that programming is going to shit nowdays.

Without its own line, a 3rd party reading the source may not realise that a multiple of 3 and 5 will print both. Therefore it is better to give it its own line at the expense of the extra computation time and code lines unless you are writing cycle-critical code.

>We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil

...

#include

int main(){
fizzbuzz();
}

int fizzbuzz()
{
int x;
for(x = 1; x

(defun fizzbuzz (n &aux (f (mod n 5))
(g (mod n 3)))
(cond ((= f g 0) 'fizzbuzz)
((= g 0) 'fizz)
((= f 0) 'buzz)
(t n)))

$fizzbuzz = array('fizzbuzz','1','2','fizz','4','buzz','fizz','7','8','fizz','buzz','11','fizz','13','14','fizzbuzz','16','17','fizz','19','buzz','fizz','22','23','fizz','buzz','26','fizz','28','29','fizzbuzz','31','32','fizz','34','buzz','fizz','37','38','fizz','buzz','41','fizz','43','44','fizzbuzz','46','47','fizz','49','buzz','fizz','52','53','fizz','buzz','56','fizz','58','59','fizzbuzz','61','62','fizz','64','buzz','fizz','67','68','fizz','buzz','71','fizz','73','74','fizzbuzz','76','77','fizz','79','buzz','fizz','82','83','fizz','buzz','86','fizz','88','89','fizzbuzz','91','92','fizz','94','buzz','fizz','97','98','fizz','buzz');
print_r($fizzbuzz);

++++++++++[>++++++++++>++++++++++>->>>>>>>>>>>>>>>>-->+++++++[->++
++++++++[->+>+>+>+>++++++++[-]++++
+[-]>>-->++++++[->+++++++++++[->+>+>+>+++++++>++++++++[-]++++++[-]>>-->
---+[-+>[-]++[-->++]-->+++[---++[-->-[+
+++[----]++[-->++]--++[--+[->[-]+++++[---->++++]-->[
->+>[.>]++[-->++]]-->+++]---+[->-[+>>>+[->>++++++++++-[>+>>]>[+[-]>+>>]+>>]>[+[-]>
+>>]

listen man, you are appending buzz. Thus, a mutiple of 3 and 5 would be fizz + buzz. If it's only multi of 5, than "" (empty str) + buzz, which is just buzz. Thus done in a line. Lot of the people in this thread are writing the code verbatim to solve the problem and not thinking about a good way to solve the problem.

For(int i=1, i

I want to give you reddit gold for this

>global import
>foreach and not map or iter
>not even using predswitch

What even is the point of the fizzbuzz challenge? To do it with the fewest possible comparisons? To just get it to work?

To give people a reason to learn Haskell

import fizzbuzz
fizzbuzz.run()


So easy, even a girl could do it.

That looks really aesthetic for some reason.

On a Popular Drinking Game.

Tybalt, the player of today's game.
Romeo, who keeps butting in.
Balthazar, a limiting factor.

Act I: Before the Game.

Scene I: An Uninvited Guest.
[Enter Balthazar and Tybalt]
Tybalt:
Listen to your heart.
Balthazar:
You plum.
[Exit Balthazar]
[Enter Romeo]
Scene II: Romeo's Question.
Romeo:
Is the remainder of the quotient between yourself and the sum
of a trustworthy fellow and a hero as good as nothing?
Tybalt:
If so, let us proceed to scene V.

Scene III: Romeo Continues.
Romeo:
Is the remainder of the quotient between yourself and the sum
of an angel and a beautiful blossoming flower as good as nothing?
Tybalt:
If so, let us proceed to scene VI.

Scene IV: The Easy Way Out.
Romeo:
Open your heart.
Tybalt:
Let us proceed to scene VII.

Scene V: Tybalt and the Art of Flattery.
Tybalt:

You mighty brave charming honest handsome noble fellow! You
are as bold as the sum of a big old hero and yourself. You are
as fair as the sum of a loving son and yourself! Speak your mind.

You are as rich as the sum of yourself and the quotient between
yourself and your large purse. Speak your mind.

You are as warm as the sum of yourself and a summer's day. You
are as cute as the sum of yourself and a pretty proud fine
little thing. Speak your mind. Speak your mind!

Is the remainder of the quotient between myself and the sum of
my thing and your lovely cute sister as good as nothing?

Romeo:
If not, let us proceed to scene VII.

Scene VI: Tybalt Starts to Run Out of Steam.
Tybalt:
You cunning fellow. You are as mighty as the difference
between yourself and a stupid vile half-witted damned dirty
lying coward. Speak your mind.

You are as large as the product of yourself and the good Lord.
You are as small as the sum of yourself and the difference
between a cat and a beautiful blossoming lovely red
rose. Speak your mind.

You are as big as the sum of yourself and a good old pony. You
are as big as the sum of yourself and a thing. Speak your
mind. Speak your mind!

Scene VII: The Next Round Begins.
Tybalt:
You cunning fellow. You are as mighty as the sum of yourself
and the cube of a little cat. Speak your mind!
Romeo:
You are as good as the sum of yourself and a King.
Tybalt:
Am I better than Balthazar?
Romeo:
If not, let us return to scene II.
[Exeunt]

First comment:

milar to many “interviews” I’ve been on. The whole fixbuzz thing is a huge joke. I have never used the or heard of PHP’s modulous operator before or since I failed the fizzbuzz test.

function fizzBuzz2()
for i=1,100 do
number = true;
str=" ";
if (i % 3 == 0) then
str = str.."fizz"
number = false;
end

if (i % 5 == 0) then
str = str .. "buzz";
number = false;
end

if (number) then
str = str .. i;
end

print(str);
end
end

C FizzBuzz
PROGRAM FZZBZZ
INTEGER I
DO 10, I=1,100
CALL COUNT(I)
10 CONTINUE
END

SUBROUTINE COUNT(I)
INTEGER I
IF (MOD(I, 3) .EQ. 0 .AND. MOD(I, 5) .EQ. 0) THEN
WRITE (*,*) 'FizzBuzz'
ELSE IF (MOD(I, 3) .EQ. 0) THEN
WRITE (*,*) 'Fizz'
ELSE IF (MOD(I, 5) .EQ. 0) THEN
WRITE (*,*) 'Buzz'
ELSE
WRITE (*,*) I
END IF
END

But I prefer this:
for i=1, 100 do
if(i % 15 == 0) then print('fizzbuzz')
elseif (i % 3 == 0) then print ('fizz');
elseif (i % 5 == 0) then print ('buzz');
else
print(i);
end
end

>I have never used the or heard of PHP’s modulous operator before or since I failed the fizzbuzz test

Every once in a great while I read something that encapsulates everything that's going wrong in the world.

That sentence is a great example.

How can anyone even pretend to call themselves a programmer if they have never encountered the % operator, or not know what it is?

The thing that's so galling about this is that everyone knows what the underlying concept is. Everyone has, at some point in their childhood, sat in a math classroom and seen the concept of "remainder after division". What is 7 divided by 3? It's 2, with a remainder of 1. Everyone has seen this. Everyone. And people who call themselves "programmers" damn well better have enough curiosity to care about the concept of integer division and what the remainder might be after performing it. This is *division* we're talking about here. One of the four fundamental arithmetic operators. Something they teach to third graders. THIS IS LITERALLY CHILD'S PLAY. And yet, we have a whole army of people out there who like to think of themselves as "programmers" who can't even use a programming language to express a concept that we teach to third graders.

But what's much, much worse -- is that they don't even have the curiosity to find out. This guy learns that he is utterly deficient in one of the most fundamental math and programming concepts in existence: integer division and the remainder that it produces. So what does he do? Does he use this opportunity to realize his deficiency and learn something new? No -- of course not. Why would he fucking ever do that? Why would he not just blame everyone else for his deficiency?

I just link to my repo:
github.com/EnterpriseQualityCoding/FizzBuzzEnterpriseEdition

>But what's much, much worse -- is that they don't even have the curiosity to find out.

I don't know how someone gets here. There probably isn't a single beginner programming course or book that omits modulus

now do it in assembly

which architecture?

>people skip a lot of shit