Live data from Hacker News

Fizz Buzz without conditionals or booleans

evanhahn.com

21–30 of 72 posts

Re: Fizz Buzz without conditionals or booleans

#22
post #13

Enumerating all values probably can't be done in python as that requires some sort of unchecked loop construct, that is a goto or bare loop nether of which is present in python. perhaps a recursive solution(throws up a little in mouth) baring that I too got nerd sniped by this and unsatisfied by the limitations of the authors solution here is my attempt. and when I read up on fizzbuz to make sure I was solving the co…

You can replace `while 1:` with `for x in iter(int, 1):` .

Re: Fizz Buzz without conditionals or booleans

#23
post #10

What's a "disguised Boolean" in this context?

I think it was more about doing it without a Boolean-based branch construct like a ternary or switch or whatever flavor of thing that abstracts away the explicit checks for true/false by other means. Idk though for sure

Re: Fizz Buzz without conditionals or booleans

#24

Much like stop50's solution, I also used the modulo, but I make use of the terminal to overwrite the number. It's only three lines of code, but I split up the list to be more readable on here. This works from 1 to 100000000000000000000 before it overflows, and 100000000000000000000 is above the max size of a unsigned 64 bit int, so I feel that it's good enough fizzbuzz = [ "fizzbuzz ", "", "", "fizz ", "", "buzz ", "…

A for loop has an implicit conditional in its stop condition check.

Re: Fizz Buzz without conditionals or booleans

#26
post #25

A for loop has a conditional in it. Unless by conditionals we mean “no if/else” and not “no branch instructions”.

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.

Re: Fizz Buzz without conditionals or booleans

#27
Obviously 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]

Re: Fizz Buzz without conditionals or booleans

#28
post #25

A for loop has a conditional in it. Unless by conditionals we mean “no if/else” and not “no branch instructions”.

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

#29
post #13

Enumerating all values probably can't be done in python as that requires some sort of unchecked loop construct, that is a goto or bare loop nether of which is present in python. perhaps a recursive solution(throws up a little in mouth) baring that I too got nerd sniped by this and unsatisfied by the limitations of the authors solution here is my attempt. and when I read up on fizzbuz to make sure I was solving the co…

Make it throw an exception with an index out of bounds to terminate the loop.

Re: Fizz Buzz without conditionals or booleans

#30
post #24

Much like stop50's solution, I also used the modulo, but I make use of the terminal to overwrite the number. It's only three lines of code, but I split up the list to be more readable on here. This works from 1 to 100000000000000000000 before it overflows, and 100000000000000000000 is above the max size of a unsigned 64 bit int, so I feel that it's good enough fizzbuzz = [ "fizzbuzz ", "", "", "fizz ", "", "buzz ", "…

A for loop has an implicit conditional in its stop condition check.

I could see that both ways. Python’s for loops are different than, say, C’s, in that they always consume an iterator. The implementation is that it calls next(iter) until it raises a StopIteration exception, but you could argue that’s just an implementation detail and not cheating.

If you wanted to be more general, you could use map() to apply the function to every member of the iterator, and implementation details aside, that feels solidly in the spirit of the challenge.

Post reply on HN