9
DavZZar
7y

Writing FizzBuzz in 3 lines via python list comprehentions.

Comments
  • 4
    If you havent seen it already. You should search Github for FizzBuzz Enterprise. Its hilarious.
  • 0
    @CrashOverride INumberGeneratorFactoryFactory etc etc.
  • 0
    Sorry, but one liners are cooler ζ:
  • 0
    fizzbuzz = lambda num_range: print('\n'.join(str(x) if x%5 and x%3 else (('fizz' if not x%3 else '') + ('buzz' if not x%5 else '')) for x in range(1, num_range+1)))
  • 2
    def fizzbuzz(limit)
    1.upto(limit+1) do |num|
    printf "fizz" if num%3==0
    printf "buzz" if num%5==0
    printf "\n"
    end
    end

    fizzbuzz(92)

    7+1 lines, so it's a little long, but very easy to read. Would be slightly faster (and more easily extensible) if I used string concatenation and a single printf, but that would add another line.
  • 2
    def fizzbuzz(limit)
    (1..limit+1).to_a.collect{|num|{1:"", 3:"fizz", 5:"buzz"}.map{|k,v| v if num%k.to_i==0}.join+"\n"}
    end
    printf fizzbuzz(92)

    More extensible golfy one-liner 😊
Add Comment