-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
Fixes #9347 #9706
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
Fixes #9347 #9706
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0037ffe
Fixes #9374
Sharnabh f518c1f
Fixes #9374
Sharnabh 93d4444
Update bit_manipulation/add_largest_power_of_2_less_than_or_equal_to_…
Sharnabh 50ff9c0
Fixes #9347
Sharnabh 1c9d4bc
Merge branch 'Fixes_#9374' of https://github.com/Sharnabh/Python into…
Sharnabh 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
44 changes: 44 additions & 0 deletions
44
bit_manipulation/add_largest_power_of_2_less_than_or_equal_to_a_given_number.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,44 @@ | ||
""" | ||
Fixes #9347 | ||
Name : Sharnabh Banerjee | ||
|
||
""" | ||
|
||
|
||
def add_largest(num: int) -> int: | ||
""" | ||
Add Largest Power of 2 less then or equal to a given number | ||
>>> add_largest(5.3) | ||
Traceback (most recent call last): | ||
... | ||
TypeError: num must be an integer !! | ||
>>> add_largest(5) | ||
9 | ||
>>> add_largest(10) | ||
18 | ||
>>> add_largest(99) | ||
163 | ||
>>> add_largest(-10) | ||
0 | ||
>>> add_largest(999) | ||
1511 | ||
|
||
""" | ||
|
||
# Checks if Float or not | ||
if isinstance(num, float): | ||
raise TypeError("num must be an integer !!") | ||
# Checks if negative or Zero | ||
if num <= 0: | ||
return 0 | ||
res = 1 | ||
# Left Bit Shift till it res is less than or equal to the given number | ||
while (res << 1) <= num: | ||
res <<= 1 | ||
return res + num | ||
|
||
|
||
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.
Can you rename the file to be less wordy.
This function doesn't accomplish what the issue is referencing.