Added utility function for calculating power set

This commit is contained in:
Shay Nehmad 2019-08-11 11:43:12 +03:00
parent 1a2d61e3a1
commit e500068e45
2 changed files with 27 additions and 0 deletions

View File

@ -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))

View File

@ -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)