5 ms·
As a C programmer, that's the worst FizzBuzz implementation ever. You're not supposed to special-case % 15, just bool m3 = !(i % 3); bool m5 = !(i % 5
by CamperBob2 1y ago
As a C programmer, that's the worst FizzBuzz implementation ever. You're not supposed to special-case % 15, just
bool m3 = !(i % 3);
bool m5 = !(i % 5);
if (m3) printf("Fizz");
if (m5) printf("Buzz");
if (m3 || m5) printf("\n");
You can turn in your visitor badge at the front desk, and they'll call you an Uber.
- chpatrick 1y agoThat's an extra system call for printing the newline.
- CamperBob2 1y agoWell, duh, yeah, if you call setbuf(stdio, NULL) first.
- CamperBob2 1y agoUh, setbuf(stdout, NULL) I'll call my own Uber, thanks
- YZF 1y agoAnd yet it's funny how many times you see the supposed "correct" solution missing that 3x5=15. I wonder how AI will answer fizzbuzz, is that part of any standard benchmark?
- CamperBob2 1y agoI mean, all trolling aside, that's kind of the idea behind FizzBuzz. If you don't notice that 15 is divisible by 3 and 5 and take advantage of that somehow in your logic, or at least acknowledge it, you really cannot be said to have aced the problem, even if your program's output is technically correct. Phrasing the question in a way that doesn't leave room for that insight is also a pretty big goof. As for AI, yes, FizzBuzz is trivial for any model because it's so well-represented in the training data. The common benchmarks involve things like "Render a physically-correct bouncing ball inside a rotating hexagon," or something else that is too complex to simply regurgitate.
- vostok 1y agoI think this person wants a space between Fizz and Buzz.
- CamperBob2 1y agoExactly, which means the interviewer didn't even state the problem correctly. The train had already jumped the rails by the time the candidate started writing. Hopefully HR will agree that they deserve each other.
- coolcase 1y agoNice. C looks like a safe language with the casual using an int as a bool.
- Joel_Mckay 1y agoDepends which of the hundreds of C compilers you used, as some "bool" are cast as uint8_t, others unsigned char, and others bit-packed with an optimizer. With C, any claim one makes about repeatability is always wrong at some point depending on the version compliance. I like C, but Haskell is a happy optimistic syntax... Julia is probably the language I'd wager becoming more relevant as Moore's laws corpse begins to stink. =3
- unsnap_biceps 1y agoThey have n `mod` 3 == 0 && n `mod` 5 == 0 And you have if (m3 || m5) I really don't see what point you're trying to make here...
- tayo42 1y agothe article has a special string that says "fizz buzz", they saying its unnecessary because if you go in order you first print "fizz", then print "buzz" which will always print "fizz buzz" for the equivalent of " mod 15" you don't need a special string that like. the "if (m3 || m5)" is just printing a newline because under that condition you printed something earlier.
- hnlmorg 1y agoIt’s still an extra condition. So it really doesn’t matter if you’re printing “Fizz Buzz” string or not, it’s the same amount of branching.
- airstrike 1y agoadd another bool that stores the information to write a newline whenever either fizz or buzz happen and you don't need the third branch
- kqr 1y agoI agree. But you're also not supposed to have separate booleans for each special print, because when you have many special prints it gets annoying to extend. I've always liked this solutiin, which avoids that: https://archive.is/KJ39B https://archive.is/KJ39B fizzbuzz i = fromMaybe (show i) . mconcat $ [ "fizz" <$ guard (i `rem` 3 == 0) , "buzz" <$ guard (i `rem` 5 == 0) ] main = for_ [1..100] $ putStrLn . fizzbuzz This allows you to add special prints by adding just the one line of code, changing nothing else.
- Glyptodon 1y agoI can't tell if you're being serious or making a joke.
- internet_points 1y agoand people say haskell is hard to get