-
Notifications
You must be signed in to change notification settings - Fork 2
feat(math): add two negabinary numbers #198
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
Open
BrianLusina
wants to merge
2
commits into
main
Choose a base branch
from
feat/math-add-two-negabinary-numbers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,41 @@ | ||
| # Adding Two Negabinary Numbers | ||
|
|
||
| Given two numbers arr1 and arr2 in base -2, return the result of adding them together. | ||
|
|
||
| Each number is given in array format: as an array of 0s and 1s, from most significant bit to least significant bit. | ||
| For example, arr = [1,1,0,1] represents the number (-2)^3 + (-2)^2 + (-2)^0 = -3. A number arr in array, format is also | ||
| guaranteed to have no leading zeros: either arr == [0] or arr[0] == 1. | ||
|
|
||
| Return the result of adding arr1 and arr2 in the same format: as an array of 0s and 1s with no leading zeros. | ||
|
|
||
| ## Examples | ||
|
|
||
| Example 1: | ||
| ```text | ||
| Input: arr1 = [1,1,1,1,1], arr2 = [1,0,1] | ||
| Output: [1,0,0,0,0] | ||
| Explanation: arr1 represents 11, arr2 represents 5, the output represents 16. | ||
| ``` | ||
|
|
||
| Example 2: | ||
| ```text | ||
| Input: arr1 = [0], arr2 = [0] | ||
| Output: [0] | ||
| ``` | ||
|
|
||
| Example 3: | ||
| ```text | ||
| Input: arr1 = [0], arr2 = [1] | ||
| Output: [1] | ||
| ``` | ||
|
|
||
| ## Constraints | ||
|
|
||
| - 1 <= arr1.length, arr2.length <= 1000 | ||
| - arr1[i] and arr2[i] are 0 or 1 | ||
| - arr1 and arr2 have no leading zeros | ||
|
|
||
| ## Topics | ||
|
|
||
| - Array | ||
| - Math | ||
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,81 @@ | ||
| from typing import List | ||
|
|
||
|
|
||
| def add_negabinary(arr1: List[int], arr2: List[int]) -> List[int]: | ||
| # Initialize pointers to the least significant bits(rightmost elements) | ||
| index_1, index_2 = len(arr1) - 1, len(arr2) - 1 | ||
|
|
||
| # Initialize carry value for addition | ||
| carry = 0 | ||
|
|
||
| # Result to store the sum digits | ||
| result = [] | ||
|
|
||
| # Process digits from right to left, including any remaining carry | ||
| while index_1 >= 0 or index_2 >= 0 or carry != 0: | ||
| # Get current digit from arr1, or 0 if we've exhausted arr1 | ||
| digit_1 = 0 if index_1 < 0 else arr1[index_1] | ||
|
|
||
| # Get current digit from arr2, or 0 if we've exhausted arr2 | ||
| digit_2 = 0 if index_2 < 0 else arr2[index_2] | ||
|
|
||
| # Calculate sum of current position including carry | ||
| current_sum = digit_1 + digit_2 + carry | ||
|
|
||
| # Reset carry for next iteration | ||
| carry = 0 | ||
|
|
||
| # Handle negabinary addition rules | ||
| if current_sum >= 2: | ||
| # If sum is 2 or more, subtract 2 and set negative carry | ||
| current_sum -= 2 | ||
| carry = -1 | ||
| elif current_sum == -1: | ||
| # If sum is -1, set digit to 1 and positive carry | ||
| current_sum = 1 | ||
| carry = 1 | ||
|
|
||
| # Append the computed digit to result | ||
| result.append(current_sum) | ||
|
|
||
| # Move pointest to the next more significant bits | ||
| index_1 -= 1 | ||
| index_2 -= 1 | ||
|
|
||
| # Remove leading zeros from the result (except if result is just [0] | ||
| while len(result) > 1 and result[-1] == 0: | ||
| result.pop() | ||
|
|
||
| # Reverse the result since we built it from least to most significant | ||
| return result[::-1] | ||
|
|
||
|
|
||
| def add_negabinary_2(arr1: List[int], arr2: List[int]) -> List[int]: | ||
| arr1 = arr1[::-1] | ||
| arr2 = arr2[::-1] | ||
|
|
||
| max_len = max(len(arr1), len(arr2)) | ||
|
|
||
| result = [] | ||
| carry = 0 | ||
|
|
||
| i = 0 | ||
| while i < max_len or carry != 0: | ||
| bit1 = arr1[i] if i < len(arr1) else 0 | ||
| bit2 = arr2[i] if i < len(arr2) else 0 | ||
|
|
||
| total = bit1 + bit2 + carry | ||
|
|
||
| if total >= 0: | ||
| result.append(total % 2) | ||
| carry = -(total // 2) | ||
| else: | ||
| result.append(1) | ||
| carry = 1 | ||
|
|
||
| i += 1 | ||
|
|
||
| while len(result) > 1 and result[-1] == 0: | ||
| result.pop() | ||
|
|
||
| return result[::-1] |
40 changes: 40 additions & 0 deletions
40
pymath/adding_two_negabinary_numbers/test_add_two_negabinary_numbers.py
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,40 @@ | ||
| import unittest | ||
| from typing import List | ||
| from parameterized import parameterized | ||
| from utils.test_utils import custom_test_name_func | ||
| from pymath.adding_two_negabinary_numbers import add_negabinary, add_negabinary_2 | ||
|
|
||
| ADD_TWO_NEGABINARY_NUMBERS_TEST_CASES = [ | ||
| ([0], [0], [0]), | ||
| ([0], [1], [1]), | ||
| ([1], [1], [1, 1, 0]), | ||
| ([1, 0], [1], [1, 1]), | ||
| ([1, 1, 1, 1, 1], [1, 0, 1], [1, 0, 0, 0, 0]), | ||
| ([1, 0, 0], [0], [1, 0, 0]), | ||
| ([1, 1, 0], [1, 0, 1], [1, 1, 0, 1, 1]), | ||
| ([1, 1], [1], [0]), | ||
| ] | ||
|
|
||
|
|
||
| class AddTwoNegabinaryNumbersTestCase(unittest.TestCase): | ||
| @parameterized.expand( | ||
| ADD_TWO_NEGABINARY_NUMBERS_TEST_CASES, name_func=custom_test_name_func | ||
| ) | ||
| def test_add_two_negabinary_numbers( | ||
| self, arr1: List[int], arr2: List[int], expected: List[int] | ||
| ): | ||
| actual = add_negabinary(arr1, arr2) | ||
| self.assertEqual(expected, actual) | ||
|
|
||
| @parameterized.expand( | ||
| ADD_TWO_NEGABINARY_NUMBERS_TEST_CASES, name_func=custom_test_name_func | ||
| ) | ||
| def test_add_two_negabinary_numbers_2( | ||
| self, arr1: List[int], arr2: List[int], expected: List[int] | ||
| ): | ||
| actual = add_negabinary_2(arr1, arr2) | ||
| self.assertEqual(expected, actual) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix the input-format sentence to avoid ambiguity.
Line 5 through Line 7 have grammar/punctuation issues (
"format: as an array"and"array, format") that make the input contract harder to parse.Suggested patch
📝 Committable suggestion
🧰 Tools
🪛 LanguageTool
[grammar] ~5-~5: A determiner may be missing.
Context: ...0s and 1s, from most significant bit to least significant bit. For example, arr = [1...
(THE_SUPERLATIVE)
🤖 Prompt for AI Agents
Source: Linters/SAST tools