forked from p15670423/monkey
Added utility function for calculating power set
This commit is contained in:
parent
1a2d61e3a1
commit
e500068e45
|
@ -0,0 +1,9 @@
|
||||||
|
from itertools import chain, combinations
|
||||||
|
|
||||||
|
|
||||||
|
def power_set(iterable):
|
||||||
|
"""
|
||||||
|
https://docs.python.org/3/library/itertools.html#itertools-recipes
|
||||||
|
"""
|
||||||
|
s = list(iterable)
|
||||||
|
return chain.from_iterable(combinations(s, r) for r in range(1, len(s) + 1))
|
|
@ -0,0 +1,18 @@
|
||||||
|
from unittest import TestCase
|
||||||
|
|
||||||
|
|
||||||
|
class TestPower_set(TestCase):
|
||||||
|
def test_power_set(self):
|
||||||
|
before = ('a', 'b', 'c')
|
||||||
|
after_expected = [
|
||||||
|
('a', ),
|
||||||
|
('b',),
|
||||||
|
('c',),
|
||||||
|
('a', 'b'),
|
||||||
|
('a', 'c'),
|
||||||
|
('b', 'c'),
|
||||||
|
('a', 'b', 'c'),
|
||||||
|
]
|
||||||
|
|
||||||
|
from common.utils.itertools_extensions import power_set
|
||||||
|
self.assertEquals(list(power_set(before)), after_expected)
|
Loading…
Reference in New Issue