>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.
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)))
Ayden Nelson
what language?
Wyatt Edwards
brainlet and codelet here, what does the % represent again?
Ian Wilson
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).
Hunter Taylor
Look like rust
Modulo, basically the remainder after division
Chase Hall
Thank you user.
Daniel Taylor
Can someone write clean ruby for this solution?
Readable and clean
Kayden Brooks
I don't like your way of doing it, just letting you know :)
Lincoln Torres
# >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()
Nicholas Martinez
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;
Evan Edwards
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")
Caleb White
Haven't wrote python in awhile, those else if should be elif or something.
Jaxson James
Sup Forums doesn't support my language of choice's character set, so I had to screencap it.
Hunter Russell
>abstract algebra OMG
Ethan Watson
>Using a language where foreach is faster than for
#!/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)
Lucas Foster
My disgusting what?
Joshua Richardson
(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)))))
Jackson Hall
What fucking lamguage has so much parentheses
Nathan King
For you, user 100.times{|i|i+=1;puts i%15==0?"fizzbuzz":i%5==0?"buzz":i%3==0?"fizz":i.to_s}
Owen Ortiz
hebrew
Charles Clark
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.
Thomas Price
anus
Michael Lopez
I suppose it could use a good wax and bleach.
Joshua Brown
These are shit.
Nathan Ramirez
FizzBuzz in C #include
int main() { int i;
for (i = 1; i
Ayden Watson
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.
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
Gabriel Russell
do any companies actually ask interviewees to do fizzbuzz anymore?
Asher Price
I started learning Go today, this was the first thing I wrote
package main import "fmt" func main(){ for i := 1; i
Caleb Young
Work on modularity nigger This would be an asspain to maintain
Lincoln Moore
Looks like I forgot how fizzbuzzes are supposed to work, but you get the idea.
Owen Murphy
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
Caleb Rodriguez
Even if you did this concurrently I'm pretty sure it'd be slower than just using one for loop
Isaac Moore
You're our new CEO, user.
Ryan Walker
what does .= do
Nathaniel Smith
#include
int main (void) {
for (int i=1;i
Dylan Hernandez
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
Brody Bailey
All these brainlets using mod 15 when you already have 3 and 5 lmao
Luke Butler
Nice
Jack Cook
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.
Noah Johnson
(let [f "fizz" b "buzz" fb "fizzbuzz"] (map #(nth (conj (cycle [% % f % b f % % % b % f % % fb]) %) %) (range 1 101)))
Noah Wright
> brainlet doesn't understand that the mod 15 answer is the most efficient
Xavier Hernandez
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
Logan Garcia
It's the >you don't need math to program xd math flunkie skiddie posse running Sup Forums.
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.
Camden Davis
>import Failed.
Julian Peterson
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))); } }
Gabriel Adams
Nobody said imports weren't allowed.
Dominic Garcia
Nobody said it, but it shows how shit you are.
Tyler Walker
you get points for actually properly incrementing the for counter, but lose them all for i < 101
The JavaScript versions would look almost exactly like the Python versions in this thread including the generator version you were sperging out about.
Joseph Lewis
>inb4 "lol secondlife"
Ethan Baker
>inafter forgot image. rip
Easton Young
Ignore him. Modulo on small integers is incredibly cheap. The string concatenation is significantly more expensive and can be eliminated easily.
Juan James
#include using namespace std;
int main() { cout
Brayden Young
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?
Xavier Perez
modern programmers have no use for the mod operation. You only learn that math brainteaser stuff in CS
Kevin Lee
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; }
Colton Jackson
...
Julian Young
wouldn't there be an optimal way of doing it by now that everyone could just use?
Ian Price
Welcome to the Big 4
Noah Fisher
posting a thread on Sup Forums and using one of the results posted
Grayson Gomez
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); } } }
Samuel Johnson
>he actually believes this
Christopher Murphy
yes, see
Christopher Carter
Made in Go package main
import ( "fmt" )
func main() { fmt.Println("FizzBuzz challenge!") for i := 1; i
Jordan White
See, this is why nobody will ever take you seriously as a programmer.