PyRival icon indicating copy to clipboard operation
PyRival copied to clipboard

Added Xor Basis

Open khan-shahnawaz opened this issue 1 year ago • 5 comments

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.

khan-shahnawaz avatar Dec 14 '23 11:12 khan-shahnawaz

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

bjorn-martinsson avatar Dec 14 '23 13:12 bjorn-martinsson

Updated the code. Please have a look

khan-shahnawaz avatar Dec 15 '23 10:12 khan-shahnawaz

bit length shouldn't be a parameter

cheran-senthil avatar Dec 18 '23 16:12 cheran-senthil

bit length shouldn't be a parameter

Actually it's needed to print xor basis upto certain number of bits. It is anyway optional.

khan-shahnawaz avatar Dec 18 '23 17:12 khan-shahnawaz

  1. 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.
  2. Keep __contains__ and remove the function is_redundant. There is no reason to have both, and contains is a nice name for the function.
  3. 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.

bjorn-martinsson avatar Dec 18 '23 17:12 bjorn-martinsson