-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Power of 4 #9505
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
Power of 4 #9505
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
0d565cc
added power_of_4
siddwarr 4722bb2
updated power_of_4
siddwarr 92b9420
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 63ee5d8
updated power_of_4
siddwarr 1a9683e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] d7950c0
updated power_of_4
siddwarr 4f4d6dc
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 079e7a0
updated power_of_4
siddwarr 6c87d47
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 8743e19
updated power_of_4
siddwarr 9db8c30
Merge branch 'power_of_4' of https://github.com/siddwarr/Python_hackt…
siddwarr fa18bdb
added type check
siddwarr 550fb3f
added tescase
siddwarr 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,67 @@ | ||
""" | ||
|
||
Task: | ||
Given a positive int number. Return True if this number is power of 4 | ||
or False otherwise. | ||
|
||
Implementation notes: Use bit manipulation. | ||
For example if the number is the power of 2 it's bits representation: | ||
n = 0..100..00 | ||
n - 1 = 0..011..11 | ||
|
||
n & (n - 1) - no intersections = 0 | ||
If the number is a power of 4 then it should be a power of 2 | ||
and the set bit should be at an odd position. | ||
""" | ||
|
||
|
||
def power_of_4(number: int) -> bool: | ||
""" | ||
Return True if this number is power of 4 or False otherwise. | ||
|
||
>>> power_of_4(0) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: number must be positive | ||
>>> power_of_4(1) | ||
True | ||
>>> power_of_4(2) | ||
False | ||
>>> power_of_4(4) | ||
True | ||
>>> power_of_4(6) | ||
False | ||
>>> power_of_4(8) | ||
False | ||
>>> power_of_4(17) | ||
False | ||
>>> power_of_4(64) | ||
True | ||
>>> power_of_4(-1) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: number must be positive | ||
>>> power_of_4(1.2) | ||
Traceback (most recent call last): | ||
... | ||
TypeError: number must be an integer | ||
|
||
""" | ||
if not isinstance(number, int): | ||
raise TypeError("number must be an integer") | ||
if number <= 0: | ||
raise ValueError("number must be positive") | ||
if number & (number - 1) == 0: | ||
c = 0 | ||
while number: | ||
c += 1 | ||
number >>= 1 | ||
return c % 2 == 1 | ||
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.
Uh oh!
There was an error while loading. Please reload this page.