do you fail at FizzBuzz? simple prog test

Mensanator mensanator at aol.com
Sun May 11 01:48:43 EDT 2008


On May 11, 12:04 am, John Machin <sjmac... at lexicon.net> wrote:
> On May 11, 1:24 pm, Mensanator <mensana... at aol.com> wrote:
>
>
>
>
>
> > On May 10, 8:12�pm, 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?
>
> > Define better.
>
> > >>> f = ['','','Fizz']*100
> > >>> b = ['','','','','Buzz']*100
> > >>> for i in xrange(1,100):
>
> >         fb = f[i-1]+b[i-1]
> >         if fb=='':
> >                 print i
> >         else:
> >                 print fb
>
> You seem to have an unfortunate fixation on 100. Consider changing the
> above instances to 34, 20, and 101.

Ok, I agree with 101, but I wouldn't necessarily
say the others were unfortunate. You might be
surprised at how often such fixations discover
bugs, something that I have a gift for.



More information about the Python-list mailing list