Live data from Hacker News

Fizz Buzz without conditionals or booleans

evanhahn.com

11–20 of 72 posts

Re: Fizz Buzz without conditionals or booleans

#12
Sigh…

Saying the code doesn’t have conditions or booleans is only true if you completely ignore how the functions being called are being implemented.

Cycle involves conditionals, zip involves conditionals, range involves conditionals, array access involves conditionals, the string concatenation involves conditionals, the iterator expansion in the for loop involves conditionals.

This has orders of magnitude more conditionals than normal fizz buzz would.

Even the function calls involve conditionals (python uses dynamic dispatch). Even if call site caching is used to avoid repeated name lookups, that involves conditionals.

There is not a line of code in that file (even the import statement) that does not use at least one conditional.

So… interesting implementation, but it’s not “fizzbuzz without booleans or conditionals”.

Re: Fizz Buzz without conditionals or booleans

#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 correct thing. (I was not and my elegant duel state engine was wasted) it turns out the problem solution could be as simple as

    f_out = ['', '', 'fizz']
    b_out = ['', '', '', '', 'buzz']
    
    def fizz_buz(n):
        return(f_out[n % 3] +  b_out[n % 5])
anyhow the rest of my clever but unneeded and useless enumeration system, remember to read the spec first.

    f_state = {
        0:1,
        1:2,
        2:0,
        }

    b_state = {
        0:1,
        1:2,
        2:3,
        3:4,
        4:0,
        }

   def fizz_buzz_all():
        f_index = 0
        b_index = 0
        while 1: #how to loop with no end check?
            print(f_out([f_index] + b_out[b_index] )
            f_index = f_state[f_index]
            b_index = b_state[b_index]
and the recursive solution:

    def fizz_buzz_recurse(n):
        print(fizz_buzz(n))
        fizz_buzz_recurse(n + 1)

Re: Fizz Buzz without conditionals or booleans

#14
What exactly are we counting as “a conditional”? Is it only “if” statements? Do “case” or “switch” statements count? Do loops with loop conditions count? Do all the included functions being abused count for all the conditionals in them? Do short-circuited boolean operations count, or only boolean variables?

I mean, if we want to play fast and loose with those definitions then this also has no conditionals and no booleans.(Warning: Perl, somewhat golfed)

  $s = '', $i % 3 || ($s .= 'fizz'), $i % 5 || ($s .= 'buzz'), $s ||= $i, print "$s\n" while (++$i 

Re: Fizz Buzz without conditionals or booleans

#15
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…

That solution fails for any value that is a multiple of neither 3 nor 5. In those cases, the result should be the original number.

Re: Fizz Buzz without conditionals or booleans

#16

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 ", "…

Very nice.

Re: Fizz Buzz without conditionals or booleans

#17

Sigh… Saying the code doesn’t have conditions or booleans is only true if you completely ignore how the functions being called are being implemented. Cycle involves conditionals, zip involves conditionals, range involves conditionals, array access involves conditionals, the string concatenation involves conditionals, the iterator expansion in the for loop involves conditionals. This has orders of magnitude more condi…

Not sure why this got downvoted.

The technique could be implemented without conditionals, but not in python, and not using iterators.

You could do it in C, and use & and ~ to make the cyclic counters work.

But, like I mentioned, the code in the article is very far from being free of conditionals.

Re: Fizz Buzz without conditionals or booleans

#18
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…

That solution fails for any value that is a multiple of neither 3 nor 5. In those cases, the result should be the original number.

Sigh, Even after I reread the spec... I did not in fact read the spec. complete failure on my part.

Re: Fizz Buzz without conditionals or booleans

#19
post #7

package main import ( "fmt" "math/rand" ) var fb [4]string = [4]string{"", "fizz", "buzz", "fizzbuzz"} var lucky int64 = 176064004 func main() { for i := 1; i

There's a conditional, though?

Yeah this is weird. It's against the rules to speculate about whether a commenter read the article, but what about the title of the article?
Post reply on HN