Preferences

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.

They 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...
the 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.

It’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.
add another bool that stores the information to write a newline whenever either fizz or buzz happen and you don't need the third branch
I 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

    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.
Nice. C looks like a safe language with the casual using an int as a bool.
Depends 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

I can't tell if you're being serious or making a joke.
That's an extra system call for printing the newline.
Well, duh, yeah, if you call setbuf(stdio, NULL) first.
Uh, setbuf(stdout, NULL)

I'll call my own Uber, thanks

I think this person wants a space between Fizz and Buzz.
Exactly, 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.
And 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?
I 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.

and people say haskell is hard to get

This item has no comments currently.

Keyboard Shortcuts

Story Lists

j
Next story
k
Previous story
Shift+j
Last story
Shift+k
First story
o Enter
Go to story URL
c
Go to comments
u
Go to author

Navigation

Shift+t
Go to top stories
Shift+n
Go to new stories
Shift+b
Go to best stories
Shift+a
Go to Ask HN
Shift+s
Go to Show HN

Miscellaneous

?
Show this modal