Tuesday, July 7, 2015

Project 5: One-Line Fizzbuzz

It's been a while since I last blogged; I figured I should write something up quickly.

Here's Fizzbuzz from the 100 projects -- in one line. Considering my usual Fizzbuzz solution takes around 8-10 lines, this is more concise. It's quite obtuse, though.

>>> def fizzbuzz(a, b, c): print "\n".join([(("Fizz"*(x%a == 0) + "Buzz"*(x%b == 0)) or str(x)) for x in range(1, c)])

>>> fizzbuzz(3, 2, 10)
1
Buzz
Fizz
Buzz
5
FizzBuzz
7
Buzz
Buzz
Fizz

>>> fizzbuzz(3, 1, 10)
Buzz
Buzz
FizzBuzz
Buzz
Buzz
FizzBuzz
Buzz
Buzz
FizzBuzz

I'm pretty proud of my solution, even if I did take the "\n".join approach from Michael Gilliland's blog entry on Fizzbuzz.

I hope to be publishing more. Thanks for reading.

No comments:

Post a Comment