do you fail at FizzBuzz? simple prog test

Arnaud Delobelle arnodel at googlemail.com
Mon May 12 13:29:06 EDT 2008


Paul Hankin <paul.hankin at gmail.com> writes:

> On May 12, 1:59 pm, Arnaud Delobelle <arno... at googlemail.com> wrote:
>> On May 12, 1:30 pm, John Machin <sjmac... at lexicon.net> wrote:
>>
>> > Duncan Booth wrote:
>> [...]
>> > > I think the variant I came up with is a bit clearer:
>>
>> > > for i in range(1,101):
>> > >    print '%s%s' % ('' if i%3 else 'Fizz', '' if i%5 else 'Buzz') or i
>>
>> > More than a bit clearer, IMO. How about
>> >      print ('' if i%3 else 'Fizz') + ('' if i%5 else 'Buzz') or i
>> > (or perhaps
>> >      print (('' if i%3 else 'Fizz') + ('' if i%5 else 'Buzz')) or i
>> > to save looking up the precedence rules) ?
>>
>> Stuff clarity!  How about
>>
>> for i in xrange(1, 101):
>>     print 'FizzBuzz'[4*(i%3>0):4+4*(i%5<1)] or i
>
> for i in xrange(1, 101):
>     print 'Fizz'*(i%3<1)+'Buzz'*(i%5<1) or i

I like this, I can imagine plenty of use cases...

...in codegolf

-- 
Arnaud



More information about the Python-list mailing list