mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-12-01 00:41:09 +00:00
iterating_through_submasks is added with doctests
This commit is contained in:
parent
6e484d8e7c
commit
00351ca899
|
@ -1,10 +1,13 @@
|
||||||
'''
|
"""
|
||||||
|
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
|
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
|
its submasks. The mask s is submask of m if only bits that were included in
|
||||||
bitmask are set
|
bitmask are set
|
||||||
'''
|
"""
|
||||||
|
|
||||||
def list_of_submasks(mask)->list:
|
|
||||||
|
def list_of_submasks(mask) -> list:
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Args:
|
Args:
|
||||||
|
@ -21,26 +24,36 @@ def list_of_submasks(mask)->list:
|
||||||
[15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
|
[15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
|
||||||
>>> list_of_submasks(13)
|
>>> list_of_submasks(13)
|
||||||
[13, 12, 9, 8, 5, 4, 1]
|
[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 = "n needs to be positive integer, your input {}"
|
fmt = "n needs to be positive integer, your input {}"
|
||||||
assert isinstance(mask, int) and mask > 0, fmt.format(mask)
|
assert isinstance(mask, int) and mask > 0, fmt.format(mask)
|
||||||
|
|
||||||
'''
|
"""
|
||||||
first submask iterated will be mask itself then operation will be performed
|
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
|
to get other submasks till we reach empty submask that is zero ( zero is not
|
||||||
included in final submasks list )
|
included in final submasks list )
|
||||||
'''
|
"""
|
||||||
all_submasks = []
|
all_submasks = []
|
||||||
submask = mask
|
submask = mask
|
||||||
|
|
||||||
while submask:
|
while submask:
|
||||||
all_submasks.append(submask)
|
all_submasks.append(submask)
|
||||||
submask = (submask-1) & mask
|
submask = (submask - 1) & mask
|
||||||
|
|
||||||
return all_submasks
|
return all_submasks
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
|
if __name__ == "__main__":
|
||||||
import doctest
|
import doctest
|
||||||
|
|
||||||
doctest.testmod()
|
doctest.testmod()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user