do you fail at FizzBuzz? simple prog test

Kam-Hung Soh kamhung.soh at gmail.com
Sat May 10 21:54:00 EDT 2008


On Sun, 11 May 2008 11:12:37 +1000, globalrev <skanemupp 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?

Looks OK to me.

A different version, and I test for multiples of 3 and 5 first:

map(lambda x: (not x%3 and not x%5 and "FizzBuzz") or (not x%3 and "Fizz")  
or (not x%5 and "Buzz") or x, xrange(1,101))

-- 
Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</a>



More information about the Python-list mailing list