Skip to content

Commit 86dbf0a

Browse files
faizan2700onlinejudge95
authored andcommitted
file iterating_through_submasks.py for given mask is added in dynamic_programming (TheAlgorithms#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
1 parent 1af4c02 commit 86dbf0a

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
"""
2+
Author : Syed Faizan (3rd Year Student IIIT Pune)
3+
github : faizan2700
4+
You are given a bitmask m and you want to efficiently iterate through all of
5+
its submasks. The mask s is submask of m if only bits that were included in
6+
bitmask are set
7+
"""
8+
from typing import List
9+
10+
11+
def list_of_submasks(mask: int) -> List[int]:
12+
13+
"""
14+
Args:
15+
mask : number which shows mask ( always integer > 0, zero does not have any submasks )
16+
17+
Returns:
18+
all_submasks : the list of submasks of mask (mask s is called submask of mask
19+
m if only bits that were included in original mask are set
20+
21+
Raises:
22+
AssertionError: mask not positive integer
23+
24+
>>> list_of_submasks(15)
25+
[15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
26+
>>> list_of_submasks(13)
27+
[13, 12, 9, 8, 5, 4, 1]
28+
>>> list_of_submasks(-7) # doctest: +ELLIPSIS
29+
Traceback (most recent call last):
30+
...
31+
AssertionError: mask needs to be positive integer, your input -7
32+
>>> list_of_submasks(0) # doctest: +ELLIPSIS
33+
Traceback (most recent call last):
34+
...
35+
AssertionError: mask needs to be positive integer, your input 0
36+
37+
"""
38+
39+
fmt = "mask needs to be positive integer, your input {}"
40+
assert isinstance(mask, int) and mask > 0, fmt.format(mask)
41+
42+
"""
43+
first submask iterated will be mask itself then operation will be performed
44+
to get other submasks till we reach empty submask that is zero ( zero is not
45+
included in final submasks list )
46+
"""
47+
all_submasks = []
48+
submask = mask
49+
50+
while submask:
51+
all_submasks.append(submask)
52+
submask = (submask - 1) & mask
53+
54+
return all_submasks
55+
56+
57+
if __name__ == "__main__":
58+
import doctest
59+
60+
doctest.testmod()

0 commit comments

Comments
 (0)