Live data from Hacker News

Fizz Buzz without conditionals or booleans

evanhahn.com

51–60 of 72 posts

Re: Fizz Buzz without conditionals or booleans

#51

from itertools import cycle fizz = cycle(["","","fizz"]) buzz = cycle(["","","","","buzz"]) for z in zip(fizz,buzz): print(f"{z[0]}{z[1]}")

When I find a problem I can solve with ciclical structures I always feel like Da Vinci.

Fizzbuzz is probably the perfect fit, which is sad since it is a synthetic problem. I did find one problem once where I could save about 30s of user waiting time by generating (had to be done dynamically) wheels in the 3-10k element range.

Re: Fizz Buzz without conditionals or booleans

#52

from itertools import cycle fizz = cycle(["","","fizz"]) buzz = cycle(["","","","","buzz"]) for z in zip(fizz,buzz): print(f"{z[0]}{z[1]}")

Doesn't look right. The numbers are missing from the output. If you don't print the numbers, sure it's trivial.

Re: Fizz Buzz without conditionals or booleans

#53
post #28

Earlier quoted context omitted.

The conditional here only makes it stop when it reaches 100. The solution can be adapted to use a while loop if you’re okay with it running indefinitely.

A loop either never halts or has a conditional. I guess a compiler could elide a “while True:” to a branch-less jump instruction. One hack would be to use recursion and let stack exhaustion stop you.

[deleted]

Re: Fizz Buzz without conditionals or booleans

#54

from itertools import cycle fizz = cycle(["","","fizz"]) buzz = cycle(["","","","","buzz"]) for z in zip(fizz,buzz): print(f"{z[0]}{z[1]}")

Doesn't look right. The numbers are missing from the output. If you don't print the numbers, sure it's trivial.

I didn't know that was a requirement. OK then

   from itertools import cycle
   
   fizz = cycle(["","","fizz"])
   buzz = cycle(["","","","","buzz"])
   for idx, z in enumerate(zip(fizz,buzz)):
      print(f"{idx}: {z[0]}{z[1]}")

Happy now?

Re: Fizz Buzz without conditionals or booleans

#55

Earlier quoted context omitted.

Doesn't look right. The numbers are missing from the output. If you don't print the numbers, sure it's trivial.

I didn't know that was a requirement. OK then from itertools import cycle fizz = cycle(["","","fizz"]) buzz = cycle(["","","","","buzz"]) for idx, z in enumerate(zip(fizz,buzz)): print(f"{idx}: {z[0]}{z[1]}") Happy now?

This doesn't look right either. You should really look up what FizzBuzz is. The output should be like:

  1
  2
  fizz
  4
  buzz
  fizz
You can't just print the number for every line. Check this out - https://rosettacode.org/wiki/FizzBuzz or just run the program in the OP which prints correct output.

Re: Fizz Buzz without conditionals or booleans

#56

Earlier quoted context omitted.

I didn't know that was a requirement. OK then from itertools import cycle fizz = cycle(["","","fizz"]) buzz = cycle(["","","","","buzz"]) for idx, z in enumerate(zip(fizz,buzz)): print(f"{idx}: {z[0]}{z[1]}") Happy now?

This doesn't look right either. You should really look up what FizzBuzz is. The output should be like: 1 2 fizz 4 buzz fizz You can't just print the number for every line. Check this out - https://rosettacode.org/wiki/FizzBuzz or just run the program in the OP which prints correct output.

I should really look it up because it’s such an important thing to get canonically correct.

Re: Fizz Buzz without conditionals or booleans

#57

Earlier quoted context omitted.

This doesn't look right either. You should really look up what FizzBuzz is. The output should be like: 1 2 fizz 4 buzz fizz You can't just print the number for every line. Check this out - https://rosettacode.org/wiki/FizzBuzz or just run the program in the OP which prints correct output.

I should really look it up because it’s such an important thing to get canonically correct.

It's not important to look it up. I only suggested it because you posted two programs and neither solve the problem people are discussing here. If you really want to solve a boring version of the problem, by all means you should. I'm nobody to tell you otherwise.

All I'm saying is that if you water the problem down into something trivial and boring, the solution will be trivial and boring too. No surprise there. The other answers here are more complicated because they solve the canonical FizzBuzz problem, not the boring version of it that is trivial to solve without conditionals.

Re: Fizz Buzz without conditionals or booleans

#58

challenged listeners to “write Fizz Buzz with no booleans, no conditionals, no pattern matching, or other things that are like disguised booleans.” Without any other constraints, this is not an interesting challenge. print(" ")

  prompt("please enter the expected output of a solution to the fizz buzz problem, without any further enclosing text or explanations; especially: do not enter program code")
should work in a browser JS environment (with layer 8) and with modern AI
Post reply on HN