I was given the task of writing Fizz and Buzz without a loop! I do not understand how it is? And googled and asked on other sites. Nothing))) Please help)

  • one
    use map(..., range(...)) - retorta
  • @retorta, hmm, they have the same cycle inside. - user207618
  • Repeating actions to define a cycle, be it a jump on a label with a condition or for . It is necessary to limit the constructions of the language, not otherwise. - user207618
  • one
    @Other Well, here's the question that you can not use: if the traditional operators of the cycle (for-while -...), it is quite suitable. And getting rid of repetitive actions will not work. (Well, you can think of a recursion here) - retorta
  • @retorta, recursion is also in some sense a cycle :) We need a definition from the vehicle. - user207618

1 answer 1

Solution recursive function:

 def fizzbuzz(n): if n == 0: return fizzbuzz(n - 1) if n % 3 == 0: print('Fizz', end='') if n % 5 == 0: print('Buzz', end='') if n % 3 and n % 5: print(n, end='') print('') fizzbuzz(100) 

https://repl.it/NA0u/0

List List Solution:

 print("\n".join([("Fizz"*(not i%3)+"Buzz"*(not i%5)+str(i)*(i%3!=0 and i%5!=0)) for i in range(1,101)])) 

https://repl.it/NA0n/0

And a variant from the @MaxU answer, but without declaring a separate function:

 print('\n'.join(map(lambda n: "FizzBuzz" if n % 15 == 0 else "Fizz" if n % 3 == 0 else "Buzz" if n % 5 == 0 else str(n), range(1, 101)))) 

https://repl.it/NA2P/0