4 ms·
I recently challenged people to codegolf fizzbuzz (http://swizec.com/blog/fizzbuzz-without-ifs-in-90-char-i-will-buy-you-a-beer-if-you-can-do-better/swizec/5276
by Swizec 14y ago
I recently challenged people to codegolf fizzbuzz (http://swizec.com/blog/fizzbuzz-without-ifs-in-90-char-i-will-buy-you-a-beer-if-you-can-do-better/swizec/5276 http://swizec.com/blog/fizzbuzz-without-ifs-in-90-char-i-wil...)
The Haskell solution was really cool:
[max(show x)(concat[n|(f,n)<-[(3,"Fizz"),(5,"Buzz")],mod x f==0])|x<-[1..100]]
This is much simpler and it looks easier to extend as well.
- Gazler 14y agoMy goto ruby solution looks something like: (1..100).map{|i|(f=[["Fizz"][i%3],["Buzz"][i%5]].join).empty? ? i:f}
- GeZe 14y ago60 character solution in LiveScript[0], without using 'if's, also easily extensible! [1 to 100]map(->[s for s,n of{Fizz:3,Buzz:5}|it%n<1]*''||it) [0]http://gkz.github.com/LiveScript/ http://gkz.github.com/LiveScript/
- andreasvc 14y agoUnfortunately not below 90 characters, but without loops or ifs: l=range(101) l[3::3]=100/3*['Fizz'] l[5::5]=100/5*['Buzz'] l[15::15]=100/15*['FizzBuzz'] print l[1:] This one is 86 characters by using constants: l=range(101) l[::3]=34*['Fizz'] l[::5]=21*['Buzz'] l[::15]=7*['FizzBuzz'] print l[1:]
- buu700 14y agoI wrote a JS one-liner solution the other day, though JS isn't quite as concise as Haskell: function fizzbuzz (n) { return new Array(n + 1).join().split(',').map(function (j, i) { return (i % 3 ? '' : ' fizz') + (i % 5 ? '' : ' buzz') || ' ' + i; }).slice(1).join().slice(1); } 185 characters Edit: If I move from a general solution to only 1-100 and remove spaces and semicolons: new Array(101).join().split(',').map(function(j, i){return (i%3?'':' fizz')+(i%5?'':' buzz')||' '+i}).slice(1).join().slice(1) Down to 126, but a lot of that is related to precision with the whitespace and handling a JS quirk with map on undefined values; without the extra joins/split/slices, it goes down to 83 characters (but also doesn't work).
- kevincennis 14y agoIn JS: i=0;while(i++<100)console.log(((i%3?'':'Fizz')+(i%5?'':'Buzz'))||i) Pardon the global.
- koblas 14y agoPython: under 90 characters and no explicits if statements for i in range(100):print ''.join([s*(i%m==0)for m,s in[(3,"Fizz"),(5,"Buzz")]])or i
- osener 14y ago66 characters: " ".join(i%3/2*"Fizz"+i%5/4*"Buzz"or str(i+1) for i in range(100))
- prakashk 14y agoPerl: # 97 characters for my $i (1..100) { say join("", map { {3=>'fizz',5=>'buzz'}->{$_} unless $i % $_ } 3,5) || $i } # 81, if those pesky spaces are removed for my$i(1..100){say join("",map{{3=>'fizz',5=>'buzz'}->{$_}unless$i%$_}3,5)||$i}
- nandemo 14y agoThat max looks suspicious. It (ab)uses the fact that the strings "Fizz", "Buzz" compare higher than any printed integer. If you try to extend it to (7, " Bazz") it fails, because " " comes before digits.