xor operator

Michael Speer knomenet at gmail.com
Mon Nov 13 16:20:27 EST 2023


I don't think an exclusive-or/truthy-entries-count-checker needs to be a
builtin by any stretch.

>>> def xor( iterable, n = 1 ):
...     return sum( map( bool, iterable ) ) == n

Or if you insist on short circuiting:

>>> def xor_ss( iterable, n = 1 ):
...   for intermediate in itertools.accumulate( iterable, (lambda x, y: x +
bool(y)), initial = 0 ):
...     if intermediate > n:
...       return False
...   return intermediate == n



On Mon, Nov 13, 2023 at 4:05 PM Barry via Python-list <
python-list at python.org> wrote:

>
>
> > On 13 Nov 2023, at 17:48, Dom Grigonis <dom.grigonis at gmail.com> wrote:
> >
> > Short circuiting happens, when:
> > xor([True, True, False, False], n=1)
> > At index 1 it is clear that the answer is false.
>
> Can you share an example with 4 values that is true?
> And explain why it is xor.
>
> Barry
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>


More information about the Python-list mailing list