The FizzBuzz test

>list all natural numbers from 1 to 100
>if a number is a multiple of 3, print Fizz instead
>if a number is a multiple of 5, print Buzz instead
>if a number is both a multiple of 3 and 5, print FizzBuzz

This is a standardized test all programmers must pass in order to get hired and there are lots of programmers out there who can't pass it (or make huge messes). I don't understand how that is possible.

Other urls found in this thread:

theregister.co.uk/2016/03/23/npm_left_pad_chaos/
twitter.com/AnonBabble

This is my attempt, took me around 30 seconds and just one failed test before it worked (had to move line 2 to the top of the loop)

Boggles the fucking mind how somebody with 3 years of computer science experience can fail at this

#include

int main(void) {

for(int i = 1; i < 101; i++)
{
if((i%3 != 0) && (i%5 != 0)) printf("%d",i);
if(i%3 == 0) printf("fizz");
if(i%5 == 0) printf("buzz");
printf("\r\n");
}
return 0;
}

def fizzbuzz(v)
word = ""
word += "fizz" if value % 3 == 0
word += "buzz" if value % 5 == 0
word += value.to_s if word.empty?
word
end

fn main() {
for i in 1..101 {
match (i % 3, i % 5) {
(0, 0) => println!("FizzBuzz"),
(0, _) => println!("Fizz"),
(_, 0) => println!("Buzz"),
_ => println!("{}", i)
}
}

Statistician coming through.
sapply(1:100, function(x) ifelse(x%%15==0, "fizzbuzz", ifelse(x%%5==0, "buzz", ifelse(x%%3==0, "fizz", x))))

[1] "1" "2" "fizz" "4" "buzz" "fizz" "7" "8" "fizz" "buzz"
[11] "11" "fizz" "13" "14" "fizzbuzz" "16" "17" "fizz" "19" "buzz"
[21] "fizz" "22" "23" "fizz" "buzz" "26" "fizz" "28" "29" "fizzbuzz"
[31] "31" "32" "fizz" "34" "buzz" "fizz" "37" "38" "fizz" "buzz"
[41] "41" "fizz" "43" "44" "fizzbuzz" "46" "47" "fizz" "49" "buzz"
[51] "fizz" "52" "53" "fizz" "buzz" "56" "fizz" "58" "59" "fizzbuzz"
[61] "61" "62" "fizz" "64" "buzz" "fizz" "67" "68" "fizz" "buzz"
[71] "71" "fizz" "73" "74" "fizzbuzz" "76" "77" "fizz" "79" "buzz"
[81] "fizz" "82" "83" "fizz" "buzz" "86" "fizz" "88" "89" "fizzbuzz"
[91] "91" "92" "fizz" "94" "buzz" "fizz" "97" "98" "fizz" "buzz"

P Y T H O N I C

list(map(lambda x: "fizzbuzz" if x%15==0 else "buzz" if x%5==0 else "fizz" if x%3==0 else x, range(1, 101)))

what language?

brainlet and codelet here, what does the % represent again?

God damn stupid formatting REEEEE. Should be good this time.

div5(X) :- 0 is X mod 5.
div3(X) :- 0 is X mod 3.

fizzbuzz(X, Max) :- (div3(X), div5(X), writeln('FizzBuzz');
div5(X), writeln('Buzz');
div3(X), writeln('Fizz');
writeln(X)), !,
X < Max,
X1 is X+1,
fizzbuzz(X1, Max).

fizzbuzz :- fizzbuzz(1, 100).

Look like rust

Modulo, basically the remainder after division

Thank you user.

Can someone write clean ruby for this solution?

Readable and clean

I don't like your way of doing it, just letting you know :)

# >list all natural numbers from 1 to 100
# >if a number is a multiple of 3, print Fizz instead
# >if a number is a multiple of 5, print Buzz instead
# >if a number is both a multiple of 3 and 5, print FizzBuzz
from __future__ import print_function


def check_number(num):
if num % 3 == 0 and num % 5 == 0:
print("FizzBuzz")
elif num % 3 == 0:
print("Fizz")
elif num % 5 == 0:
print("Buzz")
else:
print(num)

def main():
for i in range(1, 100):
check_number(i)

if __name__ == "__main__":
main()

without division or modulo, pseudo code
results = [];

for i = 3; i < 100; i+= 3;
results[i] = 'fizz';

for i = 5; i < 100; i+= 5;
if isset(results[i]))
results[i] .= 'buzz';
else
results[i] = 'buzz';

for i = 1; i < 100; i++;
if isset(results[i]))
print results[i];
else
print i;

Not doing your homework pajeet I have real programmings to do, but start off like this. If you are really good you can write some code to print this out for 1 to 100, then copy and paste that into your program.

if 1 % 3 == 0 and i % 5 == 0:
print("FizzBuzz")
else if 1 % 3 == 0:
print("Fizz")
else if i % 5 == 0:
print("Buzz")
else if 2 % 3 == 0 and 2 % 5 == 0:
print("FizzBuzz")
else if 2 % 3 == 0:
print("Fizz")
else if 2 % 5 == 0:
print("Buzz")
else if 3 % 3 == 0 and 3 % 5 == 0:
print("FizzBuzz")
else if 3 % 3 == 0:
print("Fizz")
else if 3 % 5 == 0:
print("Buzz")

Haven't wrote python in awhile, those else if should be elif or something.

Sup Forums doesn't support my language of choice's character set, so I had to screencap it.

>abstract algebra
OMG

>Using a language where foreach is faster than for

(define (foldbuzz n)
(for-each (lambda (k)
(let ((normal-number? (foldl (lambda (acc x)
(if (= 0 (modulo k (car x)))
(begin (display (cdr x))
#f)
acc))
#t
(list (cons 3 'fizz) (cons 5 'buzz)))))
(if normal-number?
(display k))
(newline)))
(iota n 1)))

Disgusting indentation.

YOUR disgusting

#!/usr/bin/env node --stack-size=8192
with(JSON){f=a=>((a=((f,s=stringify)=>(g=>g(g))(x=>f((...v)=>(k=>k in x?x[k]:x[k]=x(x)(...v))(s(v)))))(s=>(a,b)=>(b)?s(a^b,(a&b)g(g))(x=>f((...v)=>(k=>k in x?x[k]:x[k]=x(x)(...v))(s(v)))))(d=>(a,b)=>(a==b)?0:(b(g=>g(g))(x=>f((...v)=>(k=>k in x?x[k]:x[k]=x(x)(...v))(s(v)))))(s=>(a,b)=>(b)?s(a^b,(a&b)g(g))(x=>f((...v)=>(k=>k in x?x[k]:x[k]=x(x)(...v))(s(v)))))(s=>(a,b)=>(b)?s(a^b,(a&b)g(g))(x=>f((...v)=>(k=>k in x?x[k]:x[k]=x(x)(...v))(s(v)))))(d=>(a,b)=>(a==b)?0:(b(g=>g(g))(x=>f((...v)=>(k=>k in x?x[k]:x[k]=x(x)(...v))(s(v)))))(s=>(a,b)=>(b)?s(a^b,(a&b)g(g))(x=>f((...v)=>(k=>k in x?x[k]:x[k]=x(x)(...v))(s(v)))))(s=>(a,b)=>(b)?s(a^b,(a&b)

My disgusting what?

(display (filter non-empty-string?
(map (λ (x) (local [(define s "")]
(begin
(cond [(eq? (modulo x 3) 0)
(set! s (~a s "Fizz"))])
(cond [(eq? (modulo x 5) 0)
(set! s(~a s "Buzz"))])
s)))
(stream->list (in-range 1 100)))))

What fucking lamguage has so much parentheses

For you, user
100.times{|i|i+=1;puts i%15==0?"fizzbuzz":i%5==0?"buzz":i%3==0?"fizz":i.to_s}

hebrew

You don't need the conditional for fizzbuzz. An if for each one and you'll get fizzbuzz where they overlap. In other words, instead of an if/elseif, just do two consecutive ifs.

anus

I suppose it could use a good wax and bleach.

These are shit.

FizzBuzz in C
#include

int main()
{
int i;

for (i = 1; i

FizzBuzz in Python
2 ez
for i in range (1, 101):
if (i % 3 == 0 and i % 5 == 0):
print("FizzBuzz")
elif i % 3 == 0:
print("Fizz")
elif i % 5 == 0:
print("Buzz")
else:
print(i)

Its unbelievable to me that people fail this test.

#include
int main()
{
for (int i=0;++i

if 0 % 15 == 0:
print("FizzBuzz")
elif 0 % 3 == 0:
print("Fizz")
elif 0 % 5 == 0:
print("Buzz")
if 1 % 15 == 0:
print("FizzBuzz")
elif 1 % 3 == 0:
print("Fizz")
elif 1 % 5 == 0:
print("Buzz")
if 2 % 15 == 0:
print("FizzBuzz")
elif 2 % 3 == 0:
print("Fizz")
elif 2 % 5 == 0:
print("Buzz")
if 3 % 15 == 0:
print("FizzBuzz")
elif 3 % 3 == 0:
print("Fizz")
elif 3 % 5 == 0:
print("Buzz")
if 4 % 15 == 0:
print("FizzBuzz")
elif 4 % 3 == 0:
print("Fizz")
elif 4 % 5 == 0:
print("Buzz")
if 5 % 15 == 0:
print("FizzBuzz")
elif 5 % 3 == 0:
print("Fizz")
elif 5 % 5 == 0:
print("Buzz")
if 6 % 15 == 0:
print("FizzBuzz")
elif 6 % 3 == 0:
print("Fizz")
elif 6 % 5 == 0:
print("Buzz")
if 7 % 15 == 0:
print("FizzBuzz")
elif 7 % 3 == 0:
print("Fizz")
elif 7 % 5 == 0:
print("Buzz")
if 8 % 15 == 0:
print("FizzBuzz")
elif 8 % 3 == 0:
print("Fizz")
elif 8 % 5 == 0:
print("Buzz")
if 9 % 15 == 0:
print("FizzBuzz")
elif 9 % 3 == 0:
print("Fizz")
elif 9 % 5 == 0:
print("Buzz")
if 10 % 15 == 0:
print("FizzBuzz")
elif 10 % 3 == 0:
print("Fizz")
elif 10 % 5 == 0:
print("Buzz")
if 11 % 15 == 0:
print("FizzBuzz")
elif 11 % 3 == 0:
print("Fizz")
elif 11 % 5 == 0:
print("Buzz")
if 12 % 15 == 0:
print("FizzBuzz")
elif 12 % 3 == 0:
print("Fizz")
elif 12 % 5 == 0:
...

don't call us
we'll call you

...

...

local p = {[0] = "%d", "fizz", "buzz", "fizzbuzz"}
local a = function (b) if b then return 1 end return 0 end
local f = function (i) return string.format(p[a(i%3 == 0)+a(i%5==0)*2], i) end
for i=0,100 do print(f(i)) end

do any companies actually ask interviewees to do fizzbuzz anymore?

I started learning Go today, this was the first thing I wrote

package main
import "fmt"
func main(){
for i := 1; i

Work on modularity nigger
This would be an asspain to maintain

Looks like I forgot how fizzbuzzes are supposed to work, but you get the idea.

Easy.
Print("1\n2\nFizz\n4\nBuzz\nFizz\n7\n8\nFizz\nBuzz\n11\nFizz\n13\n14\nFizzBuzz\n16\n17\nFizz\n19\nBuzz\nFizz\n22\n23\nFizz\nBuzz\n26\nFizz\n28\n29\nFizzBuzz\n31\n32\nFizz\n34\nBuzz\nFizz\n37\n38\nFizz\nBuzz\n41\nFizz\n43\n44\nFizzBuzz\n46\n47\nFizz\n49\nBuzz\nFizz\n52\n53\nFizz\nBuzz\n56\nFizz\n58\n59\nFizzBuzz\n61\n62\nFizz\n64\nBuzz\nFizz\n67\n68\nFizz\nBuzz\n71\nFizz\n73\n74\nFizzBuzz\n76\n77\nFizz\n79\nBuzz\nFizz\n82\n83\nFizz\nBuzz\n86\nFizz\n88\n89\nFizzBuzz\n91\n92\nFizz\n94\nBuzz\nFizz\n97\n98\nFizz\nBuzz")
Sent from my iPhone

Even if you did this concurrently I'm pretty sure it'd be slower than just using one for loop

You're our new CEO, user.

what does .= do

#include

int main (void)
{

for (int i=1;i

for(int i = 1; i 0)
temp -= 3;
if(temp == 0){
printf("fizz");
fb = true;

temp = i;
while(temp > 0)
temp -= 5;

if(temp == 0){
printf("buzz");
fb = true;
}

if(!fb)
printf(i);

printf("\n");
}

Who needs % or /?
Hire me pls

All these brainlets using mod 15 when you already have 3 and 5 lmao

Nice

shorthand for
var = 'po';
var = var . 'pcorn';

yeah, you could mash all the logic into one loop and I think it'd actually be faster than the classic modulo operation approach. Try it, especially for bigger inputs. Anyway, I broke it out into three loops so beginners can understand exactly what's going on, to help it 'click' so to speak.

(let [f "fizz"
b "buzz"
fb "fizzbuzz"]
(map #(nth (conj (cycle [% % f % b f % % % b % f % % fb]) %) %)
(range 1 101)))

> brainlet doesn't understand that the mod 15 answer is the most efficient

You save so little time that you can run it without probably a billion times in the time it takes to type out the 15 comparison

It's the
>you don't need math to program xd
math flunkie skiddie posse running Sup Forums.

why the calls to int()?

1[[Fizz]n1sw]sF[[Buzz]n1sw]sB[dn]sN[dd0sw3%0=F5%0=Blw0=N[
]n1+d100!

from itertools import islice

def fizzbuzz(i):
while True:
if i % 15 == 0:
yield "FizzBuzz"
elif i % 3 == 0:
yield "Fizz"
elif i % 5 == 0:
yield "Buzz"
else:
yield str(i)
i += 1

print('\n'.join(islice(fizzbuzz(1), 100)))


Whenever I have to generate sequences I like to just make an infinite generator and then just take what I need from that.

>import
Failed.

elegant

public class FizzBuzz
{
public static void main(String[] args)
{
for (int i = 1; i < 101; ++i)
System.out.println(i%15==0?"FizzBuzz": (i%5==0?"Buzz":(i%3==0?"Fizz":i)));
}
}

Nobody said imports weren't allowed.

Nobody said it, but it shows how shit you are.

you get points for actually properly incrementing the for counter, but lose them all for i < 101

I bet brainlet.

>python mongoloid
>can't manage fizzbuzz without importing shit
>calls anyone brainlet
pottery

different from C includes how?

Haskell is just objectively better.

fizzbuzz n | n `mod` 15 == 0 = "fizzbuzz"
| n `mod` 5 == 0 = "fizz"
| n `mod` 3 == 0 = "buzz"
| otherwise = show n
main = mapM_ (print . fizzbuzz) [1..100]

you write what we call on the /tech/ college graduate code, so how the fuck can you even shit talk anyone else?

You're as bad as a cuck.js morons on github

do you use

stay mad pajeet

fizzbuzz n = case (rem n 3, rem n 5) of
(0,0) -> "fizzbuzz"
(0,_) -> "fizz"
(_,0) -> "buzz"
_ -> show x

map fizzbuzz [1..100]

can you get more efficient than this with python?

This is actually the state of Node.JS programming right now.

// of course you should use a package to make the code easier, simpler, and faster :^)

var fizzbuzz = import("node-fizzbuzz")

var fb = fizzbuzz.createInstance({min=0,max=100});

fb.loop(function(n, err) {
if(err) {
console.log(err);
process.exit();
}
fb.if(n.fizz, function(err) {
if(err) {
console.log(err);
process.exit();
}
console.log(fb.fizz);
}
fb.if(n.buzz, function(err) {
if(err) {
console.log(err);
process.exit();
}
console.log(fb.buzz);
}
fb.if(n.fizzbuzz, function(err) {
if(err) {
console.log(err);
process.exit();
}
console.log(fb.fizzbuzz);
}
fb.if(n.number, function(err) {
if(err) {
console.log(err);
process.exit();
}
console.log(n.value);
}
});

fb.endInstance();

// see? use packages! they make everything better! don't reinvent the wheel! :^)

yeah, don't use modulo, instead use counters. Try it for 10^19 -> 10^20 and youll see what I mean

Nobody would write it like that.

>instead use counters
u wot?

Node users can't even write their own fucking string padder
theregister.co.uk/2016/03/23/npm_left_pad_chaos/

The JavaScript versions would look almost exactly like the Python versions in this thread including the generator version you were sperging out about.

>inb4 "lol secondlife"

>inafter forgot image.
rip

Ignore him. Modulo on small integers is incredibly cheap. The string concatenation is significantly more expensive and can be eliminated easily.

#include
using namespace std;

int main() {
cout

I think he was talking about starting from zero and incrementing the value by the divisor until it was no longer less than the dividend, and then checking if they have the same value, if true then the modulus is zero.

Isn't that how the modulus function works anyways?

modern programmers have no use for the mod operation. You only learn that math brainteaser stuff in CS

Perl
say "Fizz"x!($_%3)."Buzz"x!($_%5)||$_ for 1..100

C
#include "stdio.h"
int main(void)
{
for (int i = 1; i < 101; ++i) {
printf("%d\r", i);
if (i % 3 == 0) { printf("Fizz");
if (i % 5 == 0) { printf("Buzz");
printf("\n");
}
return 0;
}

...

wouldn't there be an optimal way of doing it by now that everyone could just use?

Welcome to the Big 4

posting a thread on Sup Forums and using one of the results posted

Haxe
class Main {
static public function main():Void {
for (i in 1...101) {
var line:String = "";
if (i % 3 == 0)
line += "Fizz";
if (i % 5 == 0)
line += "Buzz";
if (line == "")
line += i;
trace(line);
}
}
}

>he actually believes this

yes, see

Made in Go
package main

import (
"fmt"
)

func main() {
fmt.Println("FizzBuzz challenge!")
for i := 1; i

See, this is why nobody will ever take you seriously as a programmer.

What?

It's 2 lines in python.