missing 'xor' Boolean operator
Paul Rubin
http
Wed Jul 15 20:32:34 EDT 2009
Anthony Tolle <anthony.tolle at gmail.com> writes:
> def xor(*operands):
> if operands:
> operands = list(operands)
> a = bool(operands.pop(0))
> while operands:
> b = bool(operands.pop(0))
> if a:
> if b:
> a = False
> elif b:
> a = True
> return a
> return False
Among other things, that uses quadratic time! Why do you want to keep
popping items from that list instead of iterating through it anyway?
Anyway, I think you wrote something close to this:
def xor(*operands):
r = False
for x in operands:
r = (r != bool(x))
return r
or in map-reduce style:
from operator import ne
def xor(*operands):
return reduce(ne, map(bool, operands), False)
More information about the Python-list
mailing list