News for hackers. Stuff that matters.
Re:Fizzbuzz, Interviews, And Overthinking by sowhatquestion on Thursday October 04, 2012 @10:07PM (#4615363)
This can be edited down to six lines with a simple trick: a Python string multiplied by True will return itself, and multiplied by False will return the empty string.

  for i in range(110):
      pr = ""
      pr += "Fizz" * (i%3 == 0)
      pr += "Buzz" * (i%5 == 0)
      pr += "Bazz" * (i%7 == 0)
      print (pr if pr else i)
I iterated over range(110) to show that it handles the "FizzBuzzBazz" case correctly.

EDIT: Or we could use lambdas as OP's Ruby code did; this might be more maintainable...

  cases = [
      lambda n: "Fizz" * (n%3 == 0),
      lambda n: "Buzz" * (n%5 == 0),
      lambda n: "Bazz" * (n%7 == 0) ]

  for i in range(110):
      pr = ""
      for case in cases:
          pr += case(i)
      print (pr if pr else i)
That's nine nonblank lines.
Re:Fizzbuzz, Interviews, And Overthinking by koblas on Thursday October 04, 2012 @11:07PM (#4615508)
Just for fun, another variant - separating logic from data and trying to be pretty stock python.

    cases = [(3, "Fizz"), (5, "Buzz"), (7, "Bazz")]

    for i in range(110):
        pr = ''.join([v[1] * (i % v[0] == 0) for v in cases])
        print pr or i
Re:Fizzbuzz, Interviews, And Overthinking by deadmansshoes on Friday October 05, 2012 @03:04AM (#4615942)
It should be possible in a single line of Python..

  pip install fizzbuzz
And then:

  import fizzbuzz
  print fizzbuzz.fizzbuzz()
Or for the second solution:

  print fizzbuzz.fizzbuzzbazz()
Re:Fizzbuzz, Interviews, And Overthinking by jonp on Friday October 05, 2012 @08:38AM (#4616593)
A tweetable (140 character) single line of Python:

print '\n'.join((lambda x:(''.join(x) if x else str(i)))([w+'zz' for (n,w) in ((3,'Fi'),(5,'Bu'),(7,'Ba')) if i%n==0]) for i in range(1,100))