Skip to content

Instantly share code, notes, and snippets.

@kk6
Created September 16, 2011 13:14
Show Gist options
  • Save kk6/1222094 to your computer and use it in GitHub Desktop.
Save kk6/1222094 to your computer and use it in GitHub Desktop.
#-*- coding:utf8 -*-
# FizzBuzz遅延評価版
from itertools import repeat, starmap, count
try:
range = xrange
except NameError:
pass
else:
from itertools import imap as map
from itertools import izip as zip
def ifizzbuzz(n):
return (item or str(i) for i, item in zip(
count(1), map(''.join, starmap(lambda x,y:
(x[0] * x[1], y[0] * y[1]), zip(
zip(repeat("Fizz"),
map(lambda i: not i%3, range(1, n+1))),
zip(repeat("Buzz"),
map(lambda i: not i%5, range(1, n+1))))))))
if __name__ == '__main__':
from itertools import islice
end = 1000000
start = end - 50
result = ifizzbuzz(end)
print(' '.join(list(islice(result, start, end, None))))
@kk6
Copy link
Author

kk6 commented Sep 17, 2011

「((i%3==0)'Fizz' + (i%5==0)'Buzz' or str(i) for i in range(1, n+1))」の倍くらい遅いけど泣かない。

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