PyRival
PyRival copied to clipboard
Added Xor Basis
Xor basis is an useful technique to solve many complex problems in Competitive Programming involving bitwise xors. This pull request adds code for xor basis.
There is a simpler algorithm for xor basis.
basis = []
def reduce(x):
for b in basis:
x = min(x, x ^ b)
return x
def insert(x):
x = reduce(x)
if x:
basis.append(x)
return True
return False
Updated the code. Please have a look
bit length shouldn't be a parameter
bit length shouldn't be a parameter
Actually it's needed to print xor basis upto certain number of bits. It is anyway optional.
- I agree with Cheran that bit_length should not be a parameter. If you want a fancy print it would be better to use
max(self.basis).bit_length()
. But you probably should just remove this completely. - Keep
__contains__
and remove the functionis_redundant
. There is no reason to have both, and contains is a nice name for the function. - A
repr
-function should never directly call the print function. Instead it should return a string. One more remark. The comment in repr" Prints the basis in descending order "
is wrong. You would need to sort basis for this.