4 ms·
fizz = [nil,nil,'Fizz'].cycle buzz = [nil,nil,nil,nil,'Buzz'].cycle (1..100).zip(fizz,buzz) {|a| n,*fb=a.compact; puts fb.empty? ? n : fb.join } I can't cl
by JulianMorrison 13y ago
fizz = [nil,nil,'Fizz'].cycle
buzz = [nil,nil,nil,nil,'Buzz'].cycle
(1..100).zip(fizz,buzz) {|a| n,*fb=a.compact; puts fb.empty? ? n : fb.join }
I can't claim credit for this one. Very elegant, I thought when I saw it.
- eCa 13y agoOne line version in Perl: perl -e 'print $_ + 1 . " " . ((("") x 2, "Fizz") x 34)[$_] . ((("") x 4, "Buzz") x 20)[$_] . "\n" foreach (0..99)' Edit: unnecessary complexity removed.
- adamtj 13y agopython -c 'print "\n".join(w or str(i) for w, i in zip(10*[None, None, "Fizz", None, "Buzz", "Fizz", None, None, "Fizz", "Buzz", None, "Fizz", None, None, "FizzBuzz"], xrange(1, 101)))'
- pudquick 13y agoNot sure I appreciate a total skipping of actually doing modulus by pre-creating the repeating 15 values but oh well, I do love me some python. python -c 'print "\n".join(w or str(i+1) for i, w in enumerate(("..Fizz..Buzz.Fizz...Fizz.Buzz..Fizz...FizzBuzz."*7).split(".")[:100]))' If you actually want to do the math, you can make your python shorter: http://stackoverflow.com/a/6890045 http://stackoverflow.com/a/6890045
- deleted 13y ago[deleted]