-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
feat(maths): add function to perform calculation #6602
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
tianyizheng02
merged 7 commits into
TheAlgorithms:master
from
okzapradhana:add-sum-of-two-positive-numbers-bitwise-program
Sep 27, 2023
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3ffc115
feat(maths): add function to perform calculation
82ffd35
docs: add wikipedia url as explanation
cf5d31e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 73e32c1
Apply suggestions from code review
tianyizheng02 3e73174
Update sum_of_two_positive_numbers_bitwise.py
tianyizheng02 aa64f29
Update sum_of_two_positive_numbers_bitwise.py
tianyizheng02 b1109a5
Update sum_of_two_positive_numbers_bitwise.py
tianyizheng02 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
""" | ||
Calculates the sum of two positive numbers using bitwise operator | ||
Wikipedia explanation: https://en.wikipedia.org/wiki/Binary_number | ||
""" | ||
|
||
|
||
def sum_of_two_positive_numbers_bitwise(number: int, other_number: int) -> int: | ||
""" " | ||
>>> sum_of_two_positive_numbers_bitwise(4, 5) | ||
9 | ||
>>> sum_of_two_positive_numbers_bitwise(8, 9) | ||
17 | ||
>>> sum_of_two_positive_numbers_bitwise(0, 4) | ||
4 | ||
>>> sum_of_two_positive_numbers_bitwise(4.5, 9) | ||
Traceback (most recent call last): | ||
... | ||
TypeError: Both parameters MUST be in integer type! | ||
>>> sum_of_two_positive_numbers_bitwise('4', 9) | ||
Traceback (most recent call last): | ||
... | ||
TypeError: Both parameters MUST be in integer type! | ||
>>> sum_of_two_positive_numbers_bitwise('4.5', 9) | ||
Traceback (most recent call last): | ||
... | ||
TypeError: Both parameters MUST be in integer type! | ||
>>> sum_of_two_positive_numbers_bitwise(-1, 9) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: Both parameters MUST be in positive value! | ||
>>> sum_of_two_positive_numbers_bitwise(1, -9) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: Both parameters MUST be in positive value! | ||
""" | ||
|
||
if isinstance(number, int) is False or isinstance(other_number, int) is False: | ||
raise TypeError("Both parameters MUST be in integer type!") | ||
tianyizheng02 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if number < 0 or other_number < 0: | ||
raise ValueError("Both parameters MUST be in positive value!") | ||
tianyizheng02 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Base case | ||
sum = number ^ other_number | ||
carry = number & other_number | ||
|
||
if (carry) == 0: | ||
tianyizheng02 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return sum | ||
|
||
# Recursive case | ||
return sum_of_two_positive_numbers_bitwise(sum, carry << 1) | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.