No. of sublists having XOR equal to zero [closed]

Use zip to find all the possible sublists, and then use reduce to find the xor value of all the elements of the sublists and filter the ones that has the value as 0

>>> from operator import xor
>>> from functools import reduce
>>> 
>>> A = [2,5,7,1,3]
>>> [sl for n in range(len(A)) for sl in zip(*(A[i:] for i in range(n+1))) if not reduce(xor, sl)]
[(2, 5, 7), (5, 7, 1, 3)]

Leave a Comment