3 lines of python, no imports. fizzbuzz = [None, None, "Fizz", None, "Buzz", "Fizz", None, None, "Fizz", "Buzz", None, "Fizz", None, None, "FizzBuzz"] for i in range(1,100): print(fizzbuzz[i % 15] or i) Edit: I see I was a few hours late, and someone posted nearly the exact same solution. :(
Fizz Buzz without conditionals or booleans
61–70 of 72 posts
Re: Fizz Buzz without conditionals or booleans
#62Earlier quoted context omitted.
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…
Re: Fizz Buzz without conditionals or booleans
#63Earlier 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.
Re: Fizz Buzz without conditionals or booleans
#643 lines of python, no imports. fizzbuzz = [None, None, "Fizz", None, "Buzz", "Fizz", None, None, "Fizz", "Buzz", None, "Fizz", None, None, "FizzBuzz"] for i in range(1,100): print(fizzbuzz[i % 15] or i) Edit: I see I was a few hours late, and someone posted nearly the exact same solution. :(
does not "or" imply a boolean
fizzbuzz = ["", "", "Fizz", "", "Buzz", "Fizz", "", "", "Fizz", "Buzz", "", "Fizz", "", "", "FizzBuzz"]
for i in range(1,101):
print(max((fizzbuzz[(i-1)% 15],str(i),)))
Probably still bools under the hood.Re: Fizz Buzz without conditionals or booleans
#65Earlier quoted context omitted.
does not "or" imply a boolean
It does.Will you accept max? fizzbuzz = ["", "", "Fizz", "", "Buzz", "Fizz", "", "", "Fizz", "Buzz", "", "Fizz", "", "", "FizzBuzz"] for i in range(1,101): print(max((fizzbuzz[(i-1)% 15],str(i),))) Probably still bools under the hood.
Re: Fizz Buzz without conditionals or booleans
#66Earlier 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.
Re: Fizz Buzz without conditionals or booleans
#67Obviously FizzBuzz is a property of integers Integer extend [ fizzbuzz [ (self \\ 15 = 0) ifTrue: ['fizzbuzz' printNl] ifFalse: [ (self \\ 3 = 0) ifTrue: ['fizz' printNl] ifFalse: [ (self \\ 5 = 0) ifTrue: ['buzz' printNl] ifFalse: [self printNl] ] ] ] ] 1 to: 100 by: 1 do: [:i | i fizzbuzz]
How is this without conditionals?
Re: Fizz Buzz without conditionals or booleans
#68Earlier quoted context omitted.
It does.Will you accept max? fizzbuzz = ["", "", "Fizz", "", "Buzz", "Fizz", "", "", "Fizz", "Buzz", "", "Fizz", "", "", "FizzBuzz"] for i in range(1,101): print(max((fizzbuzz[(i-1)% 15],str(i),))) Probably still bools under the hood.
Doesn't a for loop contain a conditional? (If i <101)
Re: Fizz Buzz without conditionals or booleans
#69Late to the party but here's a solution I worked out using roots of unity and cosines: from math import cos, pi for n in range(1, 101): print([n, 'Fizz', 'Buzz', 'FizzBuzz'][round((1 + 2 * cos(2 * pi * n / 3)) / 3 + 2 * (1 + 2 * cos(2 * pi * n / 5) + 2 * cos(4 * pi * n / 5)) / 5)])
Re: Fizz Buzz without conditionals or booleans
#70Earlier quoted context omitted.
Doesn't a for loop contain a conditional? (If i <101)
That is in arbitrary line I am willing to draw in the battle against pedantry. Surely though a fizzbuzz isn't supposed to run forever though right? Gotta end it somehow, and the for loop is idiomatic.
If I'm pedantring about it it's because it can be done with a stricter restriction (or at least I think it can be done).
I'd also add that it isn't a particularly hard problem, so I would recommend that if you are having trouble with solving for the hardest interpretation of the problem, that you keep on trying because there is quite a simple solution.
If I were hiring based on this, your answer is a pass for sure. But it isn't exactly clear to me that you know about cpu comparisions or conditional jumps. In fact I think if you knew about them you wouldn't call it an arbitrary line.
I think there is a python solution as a prototype, but of course the robust solution would be in python to ensure control over the cpu instructions and avoid conditionals for good.