2 ms·
Since we're all sharing our fizzbuzzes, here's mine (from my blog http://seriously.dontusethiscode.com/2013/04/29/bad-interview-answers.html http://seriously.do
by jamesdutc 13y ago
Since we're all sharing our fizzbuzzes, here's mine (from my blog http://seriously.dontusethiscode.com/2013/04/29/bad-interview-answers.html http://seriously.dontusethiscode.com/2013/04/29/bad-intervie...)
Note that this solution is generalised for any divisors and generates an infinite sequence.
from itertools import chain, combinations, count
from operator import mul, add
fizzbuzz = lambda terms: (lambda terms: ({x%d:w for d,w in terms}.get(0,str(x)) for x in count(1)))(tuple((lambda (d,w):(reduce(mul,d),reduce(add,w)))(zip(*x)) for x in chain.from_iterable(combinations(sorted(terms.iteritems()),s) for s in xrange(1,len(terms)+1))))
terms = {3:'fizz', 5:'buzz', 7:'baz'}
from itertools import islice
print list(islice(fizzbuzz(terms),None,25))
- iopq 13y agoMuch better, you didn't have to do manage 15, 35, 21, 105, etc.