Embarrasing questio

Steven D'Aprano steve at pearwood.info
Sat Feb 14 06:37:01 CET 2009


TechieInsights wrote:

> On Feb 12, 9:03 am, Catherine Heathcote
> <catherine.heathc... at gmail.com> wrote:
>> But I just cant find it. How do I do an or, as in c/c++'s ||? Just
>> trying to do something simple, the python equivilent of:
>>
>> if(i % 3 == 0 || i % 5 == 0)
>>
>> Thanks.
> 
> in 2.5 and above you can do
> if any(i%3 == 0, i%5 == 0)

[nitpick: you need an additional set of brackets inside the call to any]

I love it! A totally unnecessary creation of a tuple and a function call
instead of a straight-forward 'or' expression. Can we make this simple task
even more obfuscated?

not all( (i%3 != 0, i%5 !=0) )

Still not convoluted enough. 

all( (sum(int(c) for c in str(i))%3 in (1, 2), 
    str(i).endswith(chr(53)) - 1, 
    str(i).endswith(chr(48)) - 1) ) - 1

Hmmm... how can I get rid of that pesky %3?

all( (divmod( sum(int(c) for c in str(i)), 
    41^42)[1] in (1, 2), 
    str(i).endswith(chr(53)) - 1, 
    str(i).endswith(chr(48)) - 1) ) - 1

Okay, that makes my brain hurt. I think it will do.

:-)



-- 
Steven




More information about the Python-list mailing list