do you fail at FizzBuzz? simple prog test

castironpi castironpi at gmail.com
Tue May 20 09:27:25 EDT 2008


On May 20, 6:57 am, Wolfgang Grafen <wolfgang.gra... at ericsson.com>
wrote:
> globalrev schrieb:
>
>
>
>
>
> >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?
>
> Your example is comprehensive compared to many other suggestions which
> is an advantage IMO. I minimised it further
>
> for i in range(start, stop, step):
>      if not i%15:
>          print "FizzBuzz"
>      elif not i%3:
>          print "Fizz"
>      elif not i%5:
>          print "Buzz"
>      else:
>          print i
>
> Best regards
>
> Wolfgang- Hide quoted text -
>
> - Show quoted text -

This one eliminates the modulo operation.

dyn= list( xrange( 1, 101 ) )
for i in xrange( 1, len( dyn ), 3 ):
	dyn[ i ]= ''
for i in xrange( 1, len( dyn ), 5 ):
	dyn[ i ]= ''
for i in xrange( 1, len( dyn ), 3 ):
	dyn[ i ]+= 'Fizz'
for i in xrange( 1, len( dyn ), 5 ):
	dyn[ i ]+= 'Buzz'
for d in dyn:
	print d

Can we compare?



More information about the Python-list mailing list