Strongly Emergent

What comes from combining humans, computers, and narrative

Fizz and Buzz

Jeff Atwood made a Twitter post recently that looped back to his three-years-ago post about “The FizzBuzz Problem.” I read the post, and I experienced a distinct need to self-check. I spent 10-20 minutes and came up with this:

!/usr/bin/python

import sys
ournumbers = range(1, 101)
def fizzer (x):
        if (x % 3):
                return None
        else:
                return "fizz, "
def buzzer (x):
        if (x % 5):
                return None
        else:
                return "buzz, "
for x in ournumbers:
        if fizzer(x) and buzzer(x):
                sys.stdout.write("fizzbuzz, ")
        else:
                if fizzer(x):
                        sys.stdout.write(fizzer(x))
                        continue
                if buzzer(x):
                        sys.stdout.write(buzzer(x))
                        continue
                sys.stdout.write("%d, " % x)

Now, as Atwood’s immediate follow-up post points out, this is nothing to be particularly proud of. It’s a minimal test of competency. Still, I’m heartened by the fact that I straightaway knew how to do it, I just had to look up a couple of things (I confused next for continue, forgot the %-for-modulo syntax, and used print instead of sys.stdout.write) to make it behave properly. I have a very long way to go before I’m as good at this stuff as I want to be. But I’m confident that I can get there. A quick exercise like this also helps me work on my “real” projects, which are not so huge, but feel intimidating to me because they’re bigger than I’m currently competent for. The goal of those projects, of course, is to grow into that competency, so I have to keep going.