-
-
Notifications
You must be signed in to change notification settings - Fork 47k
file iterating_through_submasks.py for given mask is added in dynamic_programming #1635
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
01121af
new file *iterating_through_submasks* is added in dynamic_programming…
faizan2700 ac5b41c
no changes
faizan2700 6e484d8
*iterating_through_submasks.py is added in dynamic_programming
faizan2700 00351ca
iterating_through_submasks is added with doctests
faizan2700 b394609
iterating_through_submasks.py is added in dynamic_programming
faizan2700 9f4f239
changes made in *iterating_through_submasks.py
faizan2700 e4686e3
changes made in *iterating_through_submasks.py
faizan2700 73456f8
updated
faizan2700 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,60 @@ | ||
""" | ||
Author : Syed Faizan (3rd Year Student IIIT Pune) | ||
github : faizan2700 | ||
You are given a bitmask m and you want to efficiently iterate through all of | ||
its submasks. The mask s is submask of m if only bits that were included in | ||
bitmask are set | ||
""" | ||
from typing import List | ||
|
||
|
||
def list_of_submasks(mask: int) -> List[int]: | ||
|
||
""" | ||
Args: | ||
mask : number which shows mask ( always integer > 0, zero does not have any submasks ) | ||
|
||
Returns: | ||
all_submasks : the list of submasks of mask (mask s is called submask of mask | ||
m if only bits that were included in original mask are set | ||
|
||
Raises: | ||
AssertionError: mask not positive integer | ||
|
||
>>> list_of_submasks(15) | ||
[15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1] | ||
>>> list_of_submasks(13) | ||
[13, 12, 9, 8, 5, 4, 1] | ||
>>> list_of_submasks(-7) # doctest: +ELLIPSIS | ||
Traceback (most recent call last): | ||
... | ||
AssertionError: mask needs to be positive integer, your input -7 | ||
>>> list_of_submasks(0) # doctest: +ELLIPSIS | ||
Traceback (most recent call last): | ||
... | ||
AssertionError: mask needs to be positive integer, your input 0 | ||
|
||
""" | ||
|
||
fmt = "mask needs to be positive integer, your input {}" | ||
assert isinstance(mask, int) and mask > 0, fmt.format(mask) | ||
|
||
""" | ||
first submask iterated will be mask itself then operation will be performed | ||
to get other submasks till we reach empty submask that is zero ( zero is not | ||
included in final submasks list ) | ||
""" | ||
all_submasks = [] | ||
submask = mask | ||
|
||
while submask: | ||
all_submasks.append(submask) | ||
submask = (submask - 1) & mask | ||
|
||
return all_submasks | ||
|
||
|
||
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.