0

say something basic like:

def fizzBuzz(n: int) -> List[str]:
        l =[]
        for i in range(1,n+1):
            if i%3==0 and i%5==0:
                l.append("FizzBuzz")
            elif i%3==0:
                l.append("Fizz")
            elif i%5==0:
                l.append("Buzz")
            else:
                l.append(str(i))
        return l

where input: n=15.
output: [ "1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz" ]
I have started with something like:

["FizzBuzz" if x%3 ==0 and x%5==0 else str(x) for x in range(1, n+1)]
  • Does this answer your question? [Putting an if-elif-else statement on one line?](https://stackoverflow.com/questions/14029245/putting-an-if-elif-else-statement-on-one-line) – Andra Aug 30 '20 at 17:17
  • better answer. https://leetcode.com/problems/fizz-buzz/discuss/89928/Python-Golf – Equinox Aug 30 '20 at 17:18
  • 1
    `[("Fizz" * (not i % 3) + "Buzz" * (not i % 5)) or str(i) for i in range(1, n + 1)]` *If you will use parentheses instead of brackets, you will get a generator* – Olvin Roght Aug 30 '20 at 17:20
  • @venky__, funny that I've written almost same list comprehension with last example from your link without actually opening it :D – Olvin Roght Aug 30 '20 at 17:25
  • @OlvinRoght using `int` here is redundant, since booleans are integers in Python ;-) (Ah, it's edited now) – Lucas Moeskops Aug 30 '20 at 17:26
  • Also you can replace `[]` by `()` since he asked for a generator and you can remove the `()` around the `+` since it takes precedence over `or` ;-) – Lucas Moeskops Aug 30 '20 at 17:28
  • 1
    @LucasMoeskops, read the text after code example ;-) And I left parentheses for better readability. – Olvin Roght Aug 30 '20 at 17:28
  • while it's doable, then [zen of python](https://www.python.org/dev/peps/pep-0020/#the-zen-of-python) tell us it's a bad idea – Cyril Jouve Aug 30 '20 at 17:42

1 Answers1

2

Since you asked for a generator, here is your function converted to one, along with usage.

Notice that fizz_buzz(n) returns a NEW generator, so if you want multiple iterations, you would have to generate a new one every time.

n = 15


def fizz_buzz(n: int):
    for i in range(1, n + 1):
        if i % 3 == 0 and i % 5 == 0:
            yield "FizzBuzz"
        elif i % 3 == 0:
            yield "Fizz"
        elif i % 5 == 0:
            yield "Buzz"
        else:
            yield str(i)


a = fizz_buzz(n)
for f in a:
    print(f)

Output:

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz

However you may have wanted a list comprehension one liner, and didn't know what it is called and assumed the name was a "generator".

In that case,

b = ["FizzBuzz" if i%3==0 and i%5==0 else "Fizz" if i%3==0 else "Buzz" if i%5==0 else str(i) for i in range(n)]
print(b)

And, this can also be a one-liner-generator:

c = ("FizzBuzz" if i%3==0 and i%5==0 else "Fizz" if i%3==0 else "Buzz" if i%5==0 else str(i) for i in range(n))
for f in c:
    print(f)
Gulzar
  • 23,452
  • 27
  • 113
  • 201
  • 1
    note that `i%3==0 and i%5==0` is equivalent to `i%15==0` – Cyril Jouve Aug 30 '20 at 17:41
  • @CyrilJouve correct, but beats the purpose of modularity, readability and simplicity. The main question here is "What is 15?". what if the numbers stop being 3 and 5, but `m` and `n`? now "FizzBuzz" would check for `n*m`, which would be stored in an extra variable because it needs a name. I recommend this: https://www.youtube.com/watch?v=LueeMTTDePg – Gulzar Aug 30 '20 at 17:54
  • @CyrilJouve I meant this https://www.youtube.com/watch?v=ZsHMHukIlJY linked the wrong one last time – Gulzar Nov 16 '20 at 20:03