Skip to content

Commit 00351ca

Browse files
committed
iterating_through_submasks is added with doctests
1 parent 6e484d8 commit 00351ca

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed
Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
'''
1+
"""
2+
Author : Syed Faizan (3rd Year Student IIIT Pune)
3+
github : faizan2700
24
You are given a bitmask m and you want to efficiently iterate through all of
35
its submasks. The mask s is submask of m if only bits that were included in
46
bitmask are set
5-
'''
7+
"""
68

7-
def list_of_submasks(mask)->list:
9+
10+
def list_of_submasks(mask) -> list:
811

912
"""
1013
Args:
@@ -21,26 +24,36 @@ def list_of_submasks(mask)->list:
2124
[15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
2225
>>> list_of_submasks(13)
2326
[13, 12, 9, 8, 5, 4, 1]
27+
>>> list_of_submasks(-7) # doctest: +ELLIPSIS
28+
Traceback (most recent call last):
29+
...
30+
AssertionError: mask needs to be positive integer, your input -7
31+
>>> list_of_submasks(0) # doctest: +ELLIPSIS
32+
Traceback (most recent call last):
33+
...
34+
AssertionError: mask needs to be positive integer, your input 0
2435
2536
"""
2637

2738
fmt = "n needs to be positive integer, your input {}"
2839
assert isinstance(mask, int) and mask > 0, fmt.format(mask)
29-
30-
'''
40+
41+
"""
3142
first submask iterated will be mask itself then operation will be performed
3243
to get other submasks till we reach empty submask that is zero ( zero is not
3344
included in final submasks list )
34-
'''
45+
"""
3546
all_submasks = []
3647
submask = mask
3748

3849
while submask:
3950
all_submasks.append(submask)
40-
submask = (submask-1) & mask
41-
51+
submask = (submask - 1) & mask
52+
4253
return all_submasks
4354

44-
if __name__ == '__main__':
55+
56+
if __name__ == "__main__":
4557
import doctest
58+
4659
doctest.testmod()

0 commit comments

Comments
 (0)