#if a number is a multiple of 3 and 5 you say fizzbuzz
Explore tagged Tumblr posts
httpsdsdotcom · 2 years ago
Text
just remembered about fizzbuzz. what a wave of nostalgia
0 notes
jeremy-ken-anderson · 3 months ago
Text
Ode to FizzBuzz
FizzBuzz is a kind of old programming problem - one I was really familiar with and primed to excel at due to some unexpected basics of number theory cropping up in Dungeons and Dragons 3rd Edition.
Basically someone gives you a positive integer. Then you start counting, but if you hit a multiple of 3 you say "Fizz" instead of the number you were going to say, if you hit a multiple of 5 you say "Buzz" instead of that number, and if you hit a multiple of 15 you say "FizzBuzz" instead of whatever else.
In D&D 3rd Ed you had some levels that felt really blank and empty, because sometimes devs felt like giving out different sets of bonuses evenly - You'd get a Fighter feat every 2 levels and a regular feat every 3 levels so at level 5 Fighter would feel kinda bored and at level 6 Fighter would get two feats AND a second attack per round. AND all their saves would go up by 1 at 6 while none improved at 5. Bananas.
Anyway.
Most of handling FizzBuzz as a problem is just a matter of
Can you parse what's really being asked of you? and
Can you translate that using the vocabulary and grammar of the language you want to code in?
FizzBuzz, for instance, has a couple things to note:
You need to do something to account for 15s. If you just account for 3s and 5s, you'll end up with this kind of thing:
... 13 14 Fizz 16 ...
or maybe this, if you do back-to-back ifs instead of else-ifs (called "elifs" for short, in Python):
... 14 Fizz Buzz 16 ...
You probably want to include all three non-number outputs as explicit options - with 15 as the first option so it overrides others:
if n % 15 == 0: print("FizzBuzz") elif n % 3 == 0: print("Fizz") elif n % 5 == 0: print("Buzz") else: print(n)
If you try to make it part of the formatting of a single line, you still have to include a line of code that accounts for "what if %3 is also %15?" and you also have to code oddly so you won't run into issues with the if statements not progressing properly:
if n % 3 == 0: print("Fizz", end "") -> This makes it so "Fizz" won't automatically write a new line. if n % 3 != 5: print() -> It'll only write a new line IF it's not also "Buzz" if n % 5 == 0: print("Buzz") elif n % 3 != 0: -> This is the "weird workaround" bit, where you can't just say "else" because you had to use a second if statement to make it possible for both Fizz and Buzz to fire on the same number. print(n)
This also tests how well you understand the twisty ways code gets you thinking about stuff. Because there's a natural temptation to write it in the order the question asks about it: "Fizz," then "Buzz," then "FizzBuzz."
But if you write it "if n % 3" then "else if n % 5" then "else if n % 15," the 15 (the "FizzBuzz") part will never happen. "Else If" only happens if the first case doesn't happen, and "is divisible by 3" will always happen if "is divisible by 15" can happen. It's a reminder to put your rarer and more permissive if-statements earlier if you want everything to occur in its own time.
But wow, though. I'd really forgotten how bare-bones level 5 used to be on 3rd Ed Fighter.
1 note · View note