3 ms·
Nobody ever seems to go for the general solution: words = ( (3, 'Fizz'), (5, 'Buzz') ) def fizzbuzz(num): for value, word in words: if i % value ==
by bluepnume 13y ago
Nobody ever seems to go for the general solution:
words = (
(3, 'Fizz'),
(5, 'Buzz')
)
def fizzbuzz(num):
for value, word in words:
if i % value == 0:
yield word
for i in range(1, 101):
print ''.join(fizzbuzz(i)) or i
- cantcopy 13y agoIt works even though it should be: words = ((3, 'Fizz'), (5, 'Buzz')) def fizzbuzz(num): for value, word in words: if num % value == 0: yield word for i in range(1, 101): print ''.join(fizzbuzz(i)) or i
- bluepnume 13y agoAh. Thanks for pointing that out! Interesting quirk with shared global scope!
- epidemian 13y agoI think a list comprehension reads better than a generator (and works the same) in this case: def fizzbuzz(num): return (word for value, word in words if num % value == 0) And even dropping the fizzbuzz function altogether reads quite nicely IMHO, though it starts to look a bit golfed: words = ((3, 'Fizz'), (5, 'Buzz')) for i in range(1, 101): print ''.join(s for n, s in words if i % n == 0) or i