Created
September 16, 2011 13:14
-
-
Save kk6/1222094 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#-*- 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)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
「((i%3==0)'Fizz' + (i%5==0)'Buzz' or str(i) for i in range(1, n+1))」の倍くらい遅いけど泣かない。