-
-
Notifications
You must be signed in to change notification settings - Fork 47.1k
Bit manipulation algorithm to check is_power_of_four #9452
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
mehul-sweeti-agrawal
wants to merge
6
commits into
TheAlgorithms:master
from
mehul-sweeti-agrawal:master
Closed
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bb9c95a
added is_power_of_four.py
mehul-sweeti-agrawal 19b7446
added tests to is_power_of_four.py
mehul-sweeti-agrawal ccb2d2c
added input parameter type and return type to my function
mehul-sweeti-agrawal c0b0754
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 184933a
added decimal_to_binary conversion
mehul-sweeti-agrawal 3f6ebde
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 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
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
- Loading branch information
commit c0b075444756e5fe6740ef060175aa4039ce9ef3
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 |
---|---|---|
|
@@ -8,8 +8,9 @@ | |
|
||
""" | ||
|
||
|
||
def is_power_of_four(N: int) -> bool: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: |
||
""" | ||
""" | ||
Returns whether a number is a power of 4 or not | ||
>>> is_power_of_four(8) | ||
False | ||
|
@@ -25,25 +26,20 @@ def is_power_of_four(N: int) -> bool: | |
Traceback (most recent call last): | ||
... | ||
TypeError: unsupported operand type(s) for &: 'float' and 'float' | ||
""" | ||
|
||
#For non-positive numbers | ||
if N <= 0: | ||
raise ValueError("number must be positive") | ||
|
||
#If number is a power of 2 and ends with 4 or 6 | ||
if (N & (N-1) == 0) and (N%10 == 6 or N%10 == 4): | ||
return True | ||
else: | ||
return False | ||
|
||
""" | ||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() | ||
# For non-positive numbers | ||
if N <= 0: | ||
raise ValueError("number must be positive") | ||
|
||
# If number is a power of 2 and ends with 4 or 6 | ||
if (N & (N - 1) == 0) and (N % 10 == 6 or N % 10 == 4): | ||
return True | ||
else: | ||
return False | ||
|
||
|
||
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.
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.
Please provide descriptive name for the parameter:
N