mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
file iterating_through_submasks.py for given mask is added in dynamic_programming (#1635)
* new file *iterating_through_submasks* is added in dynamic_programming section * no changes * *iterating_through_submasks.py is added in dynamic_programming * iterating_through_submasks is added with doctests * iterating_through_submasks.py is added in dynamic_programming * changes made in *iterating_through_submasks.py * changes made in *iterating_through_submasks.py * updated
This commit is contained in:
parent
1af4c02ba6
commit
86dbf0a9d3
60
dynamic_programming/iterating_through_submasks.py
Normal file
60
dynamic_programming/iterating_through_submasks.py
Normal file
@ -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()
|
Loading…
Reference in New Issue
Block a user