I really liked this Javscript FizzBuzz can it be as nice in Python?

Peter Otten __peter__ at web.de
Fri Apr 5 13:40:00 EDT 2019


Sayth Renshaw wrote:

> I saw this fizzbuzz in Eloquent Javascript and thought its really nice.
> Not all the usual if else version, just if.
> 
> for (let n = 1; n <= 100; n++) {
>   let output = "";
>   if (n % 3 == 0) output += "Fizz";
>   if (n % 5 == 0) output += "Buzz";
>   console.log(output || n);
> }

> Any unique mindlowing style FizzBuzz I would never have considered and can
> learn off?

Here's some itertools gymnastics ;)

from itertools import *

def make(s, n):
    return cycle(chain(repeat("", n-1), (s,)))

fizzbuzz = map("".join, zip(make("Fizz", 3), make("Buzz", 5)))

for i, fb in enumerate(islice(fizzbuzz, 100), 1):
    print(fb or i)





More information about the Python-list mailing list