-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
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 all commits
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
There are no files selected for viewing
This file contains 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,46 @@ | ||
""" | ||
Author :- mehul-sweeti-agrawal | ||
|
||
Task :- Given a positive decimal integer, convert it to binary | ||
|
||
Input - 9 | ||
Output - 1001 | ||
|
||
""" | ||
|
||
|
||
def convert_to_binary(number: int) -> int: | ||
""" | ||
Returns binary equivalent of a decimal number | ||
>>> convert_to_binary(8) | ||
1000 | ||
>>> convert_to_binary(5) | ||
101 | ||
>>> convert_to_binary(-3) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: number must be non-negative | ||
>>> convert_to_binary(9.8) | ||
Traceback (most recent call last): | ||
... | ||
TypeError: unsupported operand type(s) for &: 'float' and 'int' | ||
""" | ||
|
||
# For negative numbers | ||
if number < 0: | ||
raise ValueError("number must be non-negative") | ||
|
||
power = 1 # helper variable | ||
ans = 0 # stores binary equivalent of decimal number | ||
while number: | ||
if number & 1: | ||
ans += power | ||
power *= 10 | ||
number >>= 1 | ||
return ans | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
This file contains 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,45 @@ | ||
""" | ||
Author :- mehul-sweeti-agrawal | ||
|
||
Task :- Given an integer N, find whether that integer is a power of 4 or not. | ||
|
||
Input - N | ||
Output - Yes/No | ||
|
||
""" | ||
|
||
|
||
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 | ||
>>> is_power_of_four(4) | ||
True | ||
>>> is_power_of_four(16) | ||
True | ||
>>> is_power_of_four(-1) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: number must be positive | ||
>>> is_power_of_four(9.8) | ||
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() |
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