[a / b / c / d / e / f / g / gif / h / hr / k / m / o / p / r / s / t / u / v / vg / vm / vmg / vr / vrpg / vst / w / wg] [i / ic] [r9k / s4s / vip / qa] [cm / hm / lgbt / y] [3 / aco / adv / an / bant / biz / cgl / ck / co / diy / fa / fit / gd / hc / his / int / jp / lit / mlp / mu / n / news / out / po / pol / pw / qst / sci / soc / sp / tg / toy / trv / tv / vp / vt / wsg / wsr / x / xs] [Settings] [Search] [Mobile] [Home]
Board
Settings Mobile Home
/g/ - Technology

[Advertise on 4chan]


Thread archived.
You cannot reply anymore.


[Advertise on 4chan]


File: fizzbuzz.png (20 KB, 1612x596)
20 KB
20 KB PNG
fizzbuzz me this, fizzbuzz me that.
Hit me with your best fizzbuzz.
>>
font?
>>
>>83383481
Proggy
>>
Honestly whats so bad about using 3 if statements?
>>
>>83383630
>He uses 3 if's for a single function
ngmi
>>
>>83383630
>>83383651
>>83383481

Just show me your fizzbuzz man.
>>
>>83383688
No.
>88
Nazi.
>>
import std.stdio;
import std.conv;

string fizzBuzz(int i, int n)
{
string s;
string e = to!string(i);
e ~= ", ";
if (i%3==0) s~="Fizz";
if (i%5==0) s~="Buzz";
if (i<n) s~=", ";
else s~=".";
if(s!=", ")return s;
else return e;
}
void main()
{
int n=100;
for(int i=1;i<=n;i++){
writef(fizzBuzz(i,n));
}
}
>>
>>83383699
>99
gay
>>
File: 1631669147877.png (239 KB, 634x650)
239 KB
239 KB PNG
>>83383707
>FOUR if statements
>>
>>83383651
Its readable and it’s not like it’s any less performant.
>>
>>83383469
anon, your fizzbuzz is unreadable. we're not here to play code golf. you're not going to win a prize for having the biggest brain solution. the real winner is the person with the most readable code, and everyone else is a schizo or baiting you. that being said, here is my simple haskell solution

n `isDivisible` m = n `mod` m == 0
fizzbuzz n
| n `isDivisible` (3 * 5) = "FizzBuzz"
| n `isDivisible` 3 = "Fizz"
| n `isDivisible` 5 = "Buzz"
| otherwise = show n

main = mapM (putStrLn . fizzbuzz) [1, 2 .. 100]

nothing fancy at all, drop dead simple and readable.
>>
Why doesn’t this work for python? It throws TypeError: not all arguments converted during string formatting

fizzbuzz = ["%d", "FIZZ", "BUZZ", "FIZZBUZZ"]
for i in range (0,100):
print(fizzbuzz[(i%3==0) + 2 * (i%5==0)] % i)

>>
#include <stdio.h>

int main ( void )
{
for ( int i = 0; i < 100 ; i++) printf( "%d%s\n", i, i%3?"":"Fizz", i%5?"":"Buzz" );
return 0;
}
>>
char s[] = "FizzFuzz";
char * c;
for(int i = 0; i ++< 100; printf(*c ? "%s\n" : "%d\n", *c ? (int)c : i))
{
*(c = s + 4) = (i % 5 == 0) * 'B';
c -= 4 * (i % 3 == 0);
}
>>
>>83384138
I guess that formatting type is not really used anymore. Guess it really has been a while since ive used python (or done much programming in general). No wonder i didn’t get passed the third programming interview the other week.

fizzbuzz = ["{0}", "FIZZ", "BUZZ", "FIZZBUZZ"]
for i in range (0,101):
print(fizzbuzz[(i%3==0) + 2 * (i%5==0)].format(i))
>>
>>83383469
(1..100).map{|n| {1 => n, 6 => "Fizz", 10 => "Buzz", 0 => "FizzBuzz"}[n**4%15] }
>>
File: cat.jpg (27 KB, 583x346)
27 KB
27 KB JPG
>>83384399
Now that's what this thread is about.

Well done!
>>
What other common exercices like this are there?
for i in range(30):
line = ""
if i % 3 == 0:
line += "Fizz"
if i % 5 == 0:
line += "Buzz"
if line == "":
line = i
print(line)
exit()
>>
>>83384595
Theres stuff like make a function that takes a number k and an array of numbers and find all pairs from that array that add together to get k.
>>
>>83384399
This errors out for me
>>
>>83383707
>D
Why this language? I’m not sure ive seen this used before.
>>
>>83384786
works with
mingw32-g++.exe -Wall -std=c++14 -fexceptions -O2
>>
void fizzbuzz(int i, int j)//i = start, j = stop
{
while((((i%3 == 0)&&(std::cout << "fizz"))|((i%5 == 0)&&(std::cout << "buzz"))||((std::cout << i)))&&(1&&(std::cout << std::endl))&&(i++ < j));
}
>>
>>83384822
Interesting, i was just trying in online compilers but all of them threw stuff like

460259303/source.cpp:9:66: error: cast from ‘char*’ to ‘int’ loses precision [-fpermissive]
for(int i = 0; i ++< 100; printf(*c ? "%s\n" : "%d\n", *c ? (int)c : i))
^
1460259303/source.cpp:9:71: warning: format ‘%s’ expects argument of type ‘char*’, but argument 2 has type ‘int’ [-Wformat=]
for(int i = 0; i ++< 100; printf(*c ? "%s\n" : "%d\n", *c ? (int)c : i))
>>
>>83384895
well yeah its an obscured fizzbuzz, its going to have some warnings
>>
File: file.png (83 KB, 687x718)
83 KB
83 KB PNG
>>83384786
works fine here, regular gcc
probably wouldn't even give a warning on a 32bit system (4 byte pointers) but i might be wrong about that
>>
You're all taking the long way around the problem
>>
>>83384946
Weird that all of the online compilers throw the error: cast from ‘char*’ to ‘int’ loses precision [-fpermissive] instead of just the warning like you got.
>>
>>83384043
>n `isDivisible` (3 * 5) = "FizzBuzz"
>>83384464
>0 => "FizzBuzz"
If you need an extra case to check for "FIzzBuzz", when you already checked, or will check "Fizz" and "Buzz" separately, you have failed.
>>
有數一。名之曰「戊」。恆為是。

除「戊」以三。所餘幾何。變其。名之曰「三餘」。
除「戊」以五。所餘幾何。變其。名之曰「五餘」。

夫「三餘」「五餘」中有陽乎。變其。名之曰「寂」。
若「寂」者。吾有一言。曰「戊」。書之。
若非。
吾有一言。名之曰「聲」。
若「三餘」者。加「聲」以「「黃河流水鳴濺濺。」」。昔之「聲」者。今其是矣。云云。
若「五餘」者。加「聲」以「「燕山胡騎鳴啾啾。」」。昔之「聲」者。今其是矣。云云。
吾有一言。曰「聲」。書之。
云云。

若「戊」等於一百者乃止也。
加一以「戊」。昔之「戊」者。今其是矣云云。

r8 & h8
>>
>>83383481
that font looks like shit though
>>
>>83383469
stop premanure optimization you dont know if branchless fizzbuzz generates better code i trust compiler does it better and that makes it hard to read could you please turn that into a virtual template
>>
>>83385155
That’s what i was thinking too, not sure what kind of lunatic would want to use that.
>>
>>83385211
Oldschool aesthetics are cool. Sorry about your bad case of shit taste man
>>
File: MyFizzBuzz.png (133 KB, 1155x962)
133 KB
133 KB PNG
Hey Guys!
Rate my SOLID FizzBuzz
>>
Only the first 20 numbers because 4chan doesn't like posts too long
#include <stdio.h>

int main()
{
int i=1;

loop:if (i == 1) {
printf("1\n");
}
if (i == 2) {
printf("2\n");
}
if (i == 3) {
printf("Fizz\n");
}
if (i == 4) {
printf("4\n");
}
if (i == 5) {
printf("Buzz\n");
}
if (i == 6) {
printf("Fizz\n");
}
if (i == 7) {
printf("7\n");
}
if (i == 8) {
printf("8\n");
}
if (i == 9) {
printf("Fizz\n");
}
if (i == 10) {
printf("Buzz\n");
}
if (i == 11) {
printf("11\n");
}
if (i == 12) {
printf("Fizz\n");
}
if (i == 13) {
printf("13\n");
}
if (i == 14) {
printf("14\n");
}
if (i == 15) {
printf("FizzBuzz\n");
}
if (i == 16) {
printf("16\n");
}
if (i == 17) {
printf("17\n");
}
if (i == 18) {
printf("Fizz\n");
}
if (i == 19) {
printf("19\n");
}
if (i == 20) {
printf("Buzz\n");
}
if (i <= 20) {
i++;
goto loop;
}

return 0;
}
>>
>>83385385
holy based
>>
>>83385385
the full fizzbuzz is here https://pastebin.com/RTNy5LmJ
>>
>>83385142
holy shit
>>
File: FizzInC.png (65 KB, 1279x349)
65 KB
65 KB PNG
I did one more.
>>
>>83383469
>>83385155
it looks comfy and nostalgic af
v comy
>>
What books would be good for programming and solving math problems/using math to solve programming problems?
>>
My 'zbuzz. In GHCI (the 'skell interpreter) enter "zbuzz n" where n is how high you want to go
module Zbuzz where
import Control.Monad
import Data.Bool


zbuzz = mapM_ (putStrLn . liftM3 bool
(liftM3 bool (const "Fizzbuzz") (const "Buzz") ((>0).(`mod`3)))
(liftM3 bool (const "Fizz") show ((>0).(`mod`3)))
((>0).(`mod`5))) . enumFromTo 1
>>
>>83385837
Here's a 'zbuzz where you enter both the lower and upper bound:
module Zbuzz where
import Control.Monad
import Data.Bool

zbuzz = (mapM_ (putStrLn . liftM3 bool
(liftM3 bool (const "Fizzbuzz") (const "Buzz") ((>0).(`mod`3)))
(liftM3 bool (const "Fizz") show ((>0).(`mod`3)))
((>0).(`mod`5))).) . enumFromTo
>>
>>83385810
make sonic the hedgehog go around the loop
That takes math
>>
>/g/ - Technology
>>
#include <stdio.h>

int main() {
int i;
for (i = 0; i < 100; i++) {
if (i % 15 == 0)
printf("fizzbuzz\n");
else if (i % 3 == 0)
printf("fizz\n");
else if (i % 5 == 0)
printf("buzz\n");
else printf("%d\n", i);
}
return 0;
}

>>
>>83385061
c++



Delete Post: [File Only] Style:
[Disable Mobile View / Use Desktop Site]

[Enable Mobile View / Use Mobile Site]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.