News for hackers. Stuff that matters.
Re:Fizzbuzz, Interviews, And Overthinking by js2 on Friday October 05, 2012 @11:11AM (#4617197)
Halfway through the article, I'd write the Python as:

    for i in xrange(1, 101):
        s = ""
        if not i % 3:
            s += "Fizz"
        if not i % 5:
            s += "Buzz"
        if not i % 7:
            s += "Bazz"
        print s or i
Which can be shortened to:

    for i in xrange(1, 101):
        s = ""
        s += "Fizz" if i % 3 == 0 else ""
        s += "Buzz" if i % 5 == 0 else ""
        s += "Bazz" if i % 7 == 0 else ""
        print s or i
Finishing the article inspired:

    print "\n".join(
        "".join(filter(None, ("Fizz" if i % 3 == 0 else None,
                              "Buzz" if i % 5 == 0 else None,
                              "Bazz" if i % 7 == 0 else None)))
        or str(i) for i in xrange(1, 101))

There are no comments on this comment yet.