Skip to content

Instantly share code, notes, and snippets.

@theleoborges
Last active December 17, 2015 20:28
Show Gist options
  • Save theleoborges/5667463 to your computer and use it in GitHub Desktop.
Save theleoborges/5667463 to your computer and use it in GitHub Desktop.
Functional Fizz Buzz in Ruby 2.0
threes = ["", "", "fizz"].cycle.lazy
fives = ["", "", "", "", "buzz"].cycle.lazy
(1..Float::INFINITY).lazy.zip(threes, fives).map{|n,f,b|
if f.empty? && b.empty?
return n
elsif f.empty?
return b
else
return [f,b].join
end
}.take(30).to_a
# [1, 2, "fizz", 4, "buzz", "fizz", 7, 8, "fizz", "buzz", 11, "fizz", 13, 14, "fizzbuzz",
# 16, 17, "fizz", 19, "buzz", "fizz", 22, 23, "fizz", "buzz", 26, "fizz", 28, 29, "fizzbuzz"]
@pranavraja
Copy link

from itertools import *                                                                                                                       

threes = cycle(chain(repeat('', 2), ['Fizz']))
fives = cycle(chain(repeat('', 4), ['Buzz']))

print [(fizz+buzz) or (i+1) for i, fizz, buzz in islice(izip(count(), threes, fives), 0, 30)]

winning

@rprieto
Copy link

rprieto commented May 29, 2013

from fizzbuzz import *
nailed_it()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment