do you fail at FizzBuzz? simple prog test
John Machin
sjmachin at lexicon.net
Mon May 12 08:30:58 EDT 2008
Duncan Booth wrote:
> Ivan Illarionov <ivan.illarionov at gmail.com> wrote:
>
>>>> is there a better way than my solution? is mine ok?
>>> ['%s%s' % (not i%3 and 'Fizz' or '', not i%5 and 'Buzz' or '')
>>> or str(i) for i in xrange(1, 101)]
>>>
>>> -- Ivan
>> or, more correctly, if you actually need to "print":
>>
>> sys.stdout.write('\n'.join('%s%s' %
>> (not i%3 and 'Fizz' or '', not i%5 aBuzz' or '')
>> or str(i)
>> for i in xrange(1, 101)))
>
> 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) ?
More information about the Python-list
mailing list