Skip to content

Instantly share code, notes, and snippets.

@kk6
Created September 16, 2011 10:40
Show Gist options
  • Save kk6/1221822 to your computer and use it in GitHub Desktop.
Save kk6/1221822 to your computer and use it in GitHub Desktop.
無駄にitertoolsを駆使したFizzBuzz(Python3では動きません)
#-*- coding:utf8 -*-
# 無駄にitertoolsを駆使したFizzBuzz
from itertools import repeat, ifilterfalse, starmap, count
N = 31
LEN = range(1, N)
FIZZ, BUZZ = map(repeat, ("Fizz", "Buzz"))
fizz, buzz = (
map(list,
starmap(ifilterfalse,
[(lambda i: i%3, LEN), (lambda i: i%5, LEN)])))
fizz, buzz = starmap(map,
[(lambda i: i in fizz, LEN), (lambda i: i in buzz, LEN)])
fizz, buzz = starmap(zip,
[(fizz, FIZZ), (buzz, BUZZ)])
fizz, buzz = starmap(starmap,
[(lambda x, y: x * y, fizz), (lambda x, y: x * y, buzz)])
fizzbuzz = starmap(lambda x, y: x + y, zip(fizz, buzz))
print(' '.join([(item or str(i)) for i, item in zip(count(1), fizzbuzz)]))
@kk6
Copy link
Author

kk6 commented Sep 16, 2011

Pythonならホントはワンライナーで書けるけど敢えてitertools縛りで。

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