-
-
Notifications
You must be signed in to change notification settings - Fork 48.7k
feat: Implemented Matrix Exponentiation Method #11636
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
Closed
Closed
Changes from 9 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
f5211cc
feat: add Matrix Exponentiation method
Acuspeedster 95421d7
feat: added new function matrix exponetiation method
Acuspeedster adf10bf
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ba1cbc6
feat: This function uses the tail-recursive form of the Euclidean alg…
Acuspeedster fd37108
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 50fd058
reduced the number of characters per line in the comments
Acuspeedster d6fec67
Merge branch 'branch1' of https://github.com/Acuspeedster/Python into…
Acuspeedster ede7215
removed unwanted code
Acuspeedster 0f939a8
feat: Implemented a new function to swaap numbers without dummy variable
Acuspeedster 5dc67f8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] b581cf5
removed previos code
Acuspeedster 3ad4b6a
removed the previous code
Acuspeedster 31c5645
Done with the required changes
Acuspeedster 6ee1a40
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e48a3f3
Done with the required changes
Acuspeedster 8085e66
Merge branch 'branch1' of https://github.com/Acuspeedster/Python into…
Acuspeedster f82bd8c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 8bf1389
Done with the required changes
Acuspeedster 7860646
Merge branch 'branch1' of https://github.com/Acuspeedster/Python into…
Acuspeedster 9ffd634
Done with the required changes
Acuspeedster 4e2456f
Done with the required changes
Acuspeedster 07ed6b9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 955c792
Update maths/fibonacci.py
Acuspeedster 70dd92d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 1909143
Done with the required changes
Acuspeedster 6cc39c0
Merge branch 'branch1' of https://github.com/Acuspeedster/Python into…
Acuspeedster b7db4ca
Done with the required changes
Acuspeedster 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
68 changes: 68 additions & 0 deletions
68
bit_manipulation/swaping_two_variables_without_using _temporary_variables.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,68 @@ | ||
def xor_swap(a: int, b: int) -> (int, int): | ||
Check failure on line 1 in bit_manipulation/swaping_two_variables_without_using _temporary_variables.py
|
||
""" | ||
Swap two integers using bitwise XOR | ||
without using a temporary variable. | ||
This algorithm utilizes the properties | ||
of the bitwise XOR operation to swap the values | ||
of two integers `a` and `b` without | ||
the use of a temporary variable. XOR swap is a | ||
rarely used trick but showcases the power | ||
of bit manipulation for efficient operations | ||
at the hardware level. | ||
The steps involved are: | ||
1. `a = a ^ b` | ||
2. `b = a ^ b` (Now `b` holds the original value of `a`) | ||
3. `a = a ^ b` (Now `a` holds the original value of `b`) | ||
Although this technique can swap variables | ||
without extra space, it is generally not | ||
recommended in production code because it is | ||
less readable than using a temporary variable. | ||
Args: | ||
a (int): The first integer to be swapped. | ||
b (int): The second integer to be swapped. | ||
Returns: | ||
(int, int): The swapped values of `a` and `b`. | ||
Raises: | ||
None | ||
Example: | ||
>>> xor_swap(5, 10) | ||
(10, 5) | ||
>>> xor_swap(0, 100) | ||
(100, 0) | ||
>>> xor_swap(-3, 9) | ||
(9, -3) | ||
Notes: | ||
- Swapping using XOR can lead to confusion and | ||
should generally be avoided in | ||
favor of more readable methods. | ||
- This algorithm does not work if both `a` and `b` | ||
refer to the same variable. | ||
""" | ||
if a == b: | ||
print("Both values are the same; no swap needed.") | ||
return a, b | ||
|
||
# print(f"Original a = {a}, b = {b}") | ||
a = a ^ b # Step 1 | ||
b = a ^ b # Step 2: Now b is the original value of a | ||
a = a ^ b # Step 3: Now a is the original value of b | ||
# print(f"Swapped a = {a}, b = {b}") | ||
return a, b | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
doctest.testmod() | ||
print(xor_swap(5, 10)) # (10, 5) | ||
print(xor_swap(0, 100)) # (100, 0) | ||
print(xor_swap(-3, 9)) # (9, -3) | ||
|
||
Check failure on line 68 in bit_manipulation/swaping_two_variables_without_using _temporary_variables.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 @@ | ||
# print(f"Swapped a = {a}, b = {b}") | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.