do you fail at FizzBuzz? simple prog test

Chris cwitts at gmail.com
Mon May 12 06:31:21 EDT 2008


On May 11, 3:12 am, globalrev <skanem... at yahoo.se> wrote:
> http://reddit.com/r/programming/info/18td4/comments
>
> claims people take a lot of time to write a simple program like this:
>
> "Write a program that prints the numbers from 1 to 100. But for
> multiples of three print "Fizz" instead of the number and for the
> multiples of five print "Buzz". For numbers which are multiples of
> both three and five print "FizzBuzz".
>
> for i in range(1,101):
>     if i%3 == 0 and i%5 != 0:
>         print "Fizz"
>     elif i%5 == 0 and i%3 != 0:
>         print "Buzz"
>     elif i%5 == 0 and i%3 == 0:
>         print "FizzBuzz"
>     else:
>         print i
>
> is there a better way than my solution? is mine ok?

personally if you're just checking if a modulus result is 0 or not I
would rather do as it looks neat imho.

for i in xrange(1,101):
    if not i % 3 and i % 5:
        print 'Fizz'
    elif i % 3 and not i % 5:
        print 'Buzz'
    elif not i % 3 and not i % 5:
        print 'FizzBuzz'
    else:
        print i



More information about the Python-list mailing list