Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Xor Basis #89

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

khan-shahnawaz
Copy link

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.

@bjorn-martinsson
Copy link
Collaborator

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

@khan-shahnawaz
Copy link
Author

Updated the code. Please have a look

@cheran-senthil
Copy link
Owner

bit length shouldn't be a parameter

@khan-shahnawaz
Copy link
Author

bit length shouldn't be a parameter

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

@bjorn-martinsson
Copy link
Collaborator

bjorn-martinsson commented Dec 18, 2023

  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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants