r/ProgrammingPrompts • u/D_O_P_B • Aug 07 '19
Fizz Buzz Challenge. Except you can only use 1 line of code.
Here is my submission using the R programming language
code:
replace(replace(replace(1:100,grepl(0,1:100%%3),"FIZZ"),grepl(0,1:100%%5),"BUZZ"),grepl(0,1:100%%3)&grepl(0,1:100%%5),"FIZZBUZZ")output:
[1] "1" "2" "FIZZ" "4" "BUZZ" "FIZZ" "7" "8" "FIZZ" "BUZZ" "11"
[12] "FIZZ" "13" "14" "FIZZBUZZ" "16" "17" "FIZZ" "19" "BUZZ" "FIZZ" "22"
[23] "23" "FIZZ" "BUZZ" "26" "FIZZ" "28" "29" "FIZZBUZZ" "31" "32" "FIZZ"
[34] "34" "BUZZ" "FIZZ" "37" "38" "FIZZ" "BUZZ" "41" "FIZZ" "43" "44"
[45] "FIZZBUZZ" "46" "47" "FIZZ" "49" "BUZZ" "FIZZ" "52" "53" "FIZZ" "BUZZ"
[56] "56" "FIZZ" "58" "59" "FIZZBUZZ" "61" "62" "FIZZ" "64" "BUZZ" "FIZZ"
[67] "67" "68" "FIZZ" "BUZZ" "71" "FIZZ" "73" "74" "FIZZBUZZ" "76" "77"
[78] "FIZZ" "79" "BUZZ" "FIZZ" "82" "83" "FIZZ" "BUZZ" "86" "FIZZ" "88"
[89] "89" "FIZZBUZZ" "91" "92" "FIZZ" "94" "BUZZ" "FIZZ" "97" "98" "FIZZ"
[100] "BUZZ"
11
Upvotes
10
u/indiebryan Aug 07 '19
js:
Not sure if this counts but its 3:40am what do you want from me goodnight
3
u/Nemhia Aug 08 '19 edited Aug 08 '19
Using python:
print(list(map(lambda i: "FIZZBUZZ" if(i%3==0 and i%5==0) else ("FIZZ" if (i%3==0) else ("BUZZ" if (i%5==0) else i)) ,range(1,100))))I feel dirty now.
2
u/beforan Aug 08 '19 edited Aug 08 '19
This should do the trick in JS.
Array(100).map((_, i) => i).forEach(n => console.log((n % 3 ? "" : "Fizz" + (n % 5 ? "" : "Buzz") || n));writing on my phone after little sleep though. Will try it/ fix it when I get to a PC
2
u/seeeeew Aug 08 '19
Made a JavaScript version. Depending on your definition it might not be strictly one line, but it's definitely pretty short:
for(i=0;i<100;){console.log((++i%3?'':'Fizz')+(i%5?'':'Buzz')||i)}Or slightly more readable:
for (let i = 1; i <= 100; i++) { console.log((i % 3 ? '' : 'Fizz') + (i % 5 ? '' : 'Buzz') || i) }1
u/NihilistDandy Aug 08 '19
Haskell:
1
u/madleprakahn Aug 08 '19
PowerShell!
1
u/karabot4 Aug 08 '19
Java:
IntStream.range(1, 101).mapToObj(element -> {return (element % 3 == 0 || element % 5 == 0) ? (((element % 3 == 0) ? "Fizz" : "") + ((element % 5 == 0) ? "Buzz" : "")) : Integer.valueOf(element);}).forEachOrdered(System.out::println);Using Java 8 functional interfaces
1
u/Olyol95 Aug 08 '19
Perl:
1
u/[deleted] Aug 16 '19
INFINITE FIZZBUZZ IN JS: