Skip to content

Added binary_xor_operator.py and binary_and_operator.py #2433

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

Merged
merged 4 commits into from
Sep 24, 2020
Merged

Added binary_xor_operator.py and binary_and_operator.py #2433

merged 4 commits into from
Sep 24, 2020

Conversation

vivek9patel
Copy link
Contributor

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

@TravisBuddy
Copy link

Travis tests have failed

Hey @vivek9patel,
Please read the following log in order to understand the failure reason.
It'll be awesome if you fix what's wrong and commit the changes.

TravisBuddy Request Identifier: d0978fe0-f766-11ea-bb1a-aff04add7ab5

@vivek9patel
Copy link
Contributor Author

@cclauss can you please review this?

if a < 0 or b < 0:
raise ValueError("the value of both input must be positive")

a_binary = str(bin(a))[2:] # remove the leading "0b"
Copy link
Contributor

@Abdujabbar Abdujabbar Sep 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just use

def binary_xor(a: int, b: int):
xor = a ^ b
return bin(xor)

would not it be faster or not?

and I don't understand why xor operators binary representation in your result differ than core xor operator:

for inputs:
37 ^ 50 = 0b10111

but in your version:
37 ^ 50 = 0b010111

yes of course you would be right if you say that leading zeros would not change the value but I think it would be better if we remove leading zeros after 0b ?

it's just my opinion, maybe @cclauss and others are agree with your version.

Copy link
Member

@cclauss cclauss Sep 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Often in this repo we are trying to demonstrate how an algorithm works. Our primary goal is education not always optimized performance. If we just use Python's builtin capabilities and its standard library we can often write code in a line or two but we lose a lot of educational value. Often the one or two-liner is great to use in our doctests to prove that the educational long-form gives the same results as the builtin short-form over a range of test values.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first thing using a^b gives us decimal representation on xor value
this function gives us a binary representation of xor function.
Totally agree with @cclauss ,this is just for learning and practicing things!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Often in this repo we are trying to demonstrate how an algorithm works. Our primary goal is education not always optimized performance. If we just use Python's builtin capabilities and its standard library we can often write code in a line or two but we lose a lot of educational value. Often the one or two-liner is great to use in our doctests to prove that the educational long-form gives the same results as the builtin short-form over a range of test values.

But one more thing @cclauss does my PR is having any problems, because it is one week and it is not reviewed yet!?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cclauss ok, understand, thank you for your clarification

Copy link
Member

@cclauss cclauss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@cclauss cclauss merged commit 4a3b8d6 into TheAlgorithms:master Sep 24, 2020
stokhos pushed a commit to stokhos/Python that referenced this pull request Jan 3, 2021
…s#2433)

* Added binary_and_operator.py & binary_xor_operator.py

* Updated binary_and_operator.py

* Updated binary_xor_operator.py

* Updated binary_xor_operator.py
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.

4 participants