Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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)


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.


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


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




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: