def test_multiset_partitions_versions(): """Compares Knuth-based versions of multiset_partitions""" multiplicities = [5, 2, 2, 1] m = MultisetPartitionTraverser() for s1, s2 in zip_longest(m.enum_all(multiplicities), multiset_partitions_taocp(multiplicities)): assert compare_multiset_states(s1, s2)
def subrange_exercise(mult, lb, ub): """Compare filter-based and more optimized subrange implementations Helper for tests, called with both small and larger multisets. """ m = MultisetPartitionTraverser() assert m.count_partitions(mult) == \ m.count_partitions_slow(mult) # Note - multiple traversals from the same # MultisetPartitionTraverser object cannot execute at the same # time, hence make several instances here. ma = MultisetPartitionTraverser() mc = MultisetPartitionTraverser() md = MultisetPartitionTraverser() # Several paths to compute just the size two partitions a_it = ma.enum_range(mult, lb, ub) b_it = part_range_filter(multiset_partitions_taocp(mult), lb, ub) c_it = part_range_filter(mc.enum_small(mult, ub), lb, sum(mult)) d_it = part_range_filter(md.enum_large(mult, lb), 0, ub) for sa, sb, sc, sd in zip_longest(a_it, b_it, c_it, d_it): assert compare_multiset_states(sa, sb) assert compare_multiset_states(sa, sc) assert compare_multiset_states(sa, sd)
def test_multiset_partitions_versions(): """Compares Knuth-based versions of multiset_partitions.""" multiplicities = [5, 2, 2, 1] m = MultisetPartitionTraverser() for s1, s2 in zip_longest(m.enum_all(multiplicities), multiset_partitions_taocp(multiplicities)): assert compare_multiset_states(s1, s2)
def compare_multiset_w_baseline(multiplicities): # Enumerates the partitions of multiset with AOCP algorithm and # baseline implementation, and compare the results. letters = string.ascii_lowercase bl_partitions = multiset_partitions_baseline(multiplicities, letters) # The partitions returned by the different algorithms may have # their parts in different orders. Also, they generate partitions # in different orders. Hence the sorting, and set comparison. aocp_partitions = set() for state in multiset_partitions_taocp(multiplicities): p1 = tuple(sorted(tuple(p) for p in list_visitor(state, letters))) aocp_partitions.add(p1) assert bl_partitions == aocp_partitions
def compare_multiset_w_baseline(multiplicities): """ Enumerates the partitions of multiset with AOCP algorithm and baseline implementation, and compare the results. """ letters = "abcdefghijklmnopqrstuvwxyz" bl_partitions = multiset_partitions_baseline(multiplicities, letters) # The partitions returned by the different algorithms may have # their parts in different orders. Also, they generate partitions # in different orders. Hence the sorting, and set comparison. aocp_partitions = set() for state in multiset_partitions_taocp(multiplicities): p1 = tuple(sorted(tuple(p) for p in list_visitor(state, letters))) aocp_partitions.add(p1) assert bl_partitions == aocp_partitions
def compare_multiset_w_baseline(multiplicities): """ Enumerates the partitions of multiset with AOCP algorithm and baseline implementation, and compare the results. """ letters = "abcdefghijklmnopqrstuvwxyz" bl_partitions = multiset_partitions_baseline(multiplicities, letters) # The partitions returned by the different algorithms may have # their parts in different orders. Also, they generate partitions # in different orders. Hence the sorting, and set comparison. aocp_partitions = set() for state in multiset_partitions_taocp(multiplicities): p1 = tuple(sorted( [tuple(p) for p in list_visitor(state, letters)])) aocp_partitions.add(p1) assert bl_partitions == aocp_partitions
def test_multiset_partitions(): A = [0, 1, 2, 3, 4] assert list(multiset_partitions(A, 5)) == [[[0], [1], [2], [3], [4]]] assert len(list(multiset_partitions(A, 4))) == 10 assert len(list(multiset_partitions(A, 3))) == 25 assert list(multiset_partitions([1, 1, 1, 2, 2], 2)) == [[[1, 1, 1, 2], [2]], [[1, 1, 1], [2, 2]], [[1, 1, 2, 2], [1]], [[1, 1, 2], [1, 2]], [[1, 1], [1, 2, 2]]] assert list(multiset_partitions([1, 1, 2, 2], 2)) == [[[1, 1, 2], [2]], [[1, 1], [2, 2]], [[1, 2, 2], [1]], [[1, 2], [1, 2]]] assert list(multiset_partitions([1, 2, 3, 4], 2)) == [[[1, 2, 3], [4]], [[1, 2, 4], [3]], [[1, 2], [3, 4]], [[1, 3, 4], [2]], [[1, 3], [2, 4]], [[1, 4], [2, 3]], [[1], [2, 3, 4]]] assert list(multiset_partitions([1, 2, 2], 2)) == [[[1, 2], [2]], [[1], [2, 2]]] assert list(multiset_partitions(3)) == [[[0, 1, 2]], [[0, 1], [2]], [[0, 2], [1]], [[0], [1, 2]], [[0], [1], [2]]] assert list(multiset_partitions(3, 2)) == [[[0, 1], [2]], [[0, 2], [1]], [[0], [1, 2]]] assert list(multiset_partitions([1] * 3, 2)) == [[[1], [1, 1]]] assert list(multiset_partitions([1] * 3)) == [[[1, 1, 1]], [[1], [1, 1]], [[1], [1], [1]]] a = [3, 2, 1] assert list(multiset_partitions(a)) == \ list(multiset_partitions(sorted(a))) assert list(multiset_partitions(a, 5)) == [] assert list(multiset_partitions(a, 1)) == [[[1, 2, 3]]] assert list(multiset_partitions(a + [4], 5)) == [] assert list(multiset_partitions(a + [4], 1)) == [[[1, 2, 3, 4]]] assert list(multiset_partitions(2, 5)) == [] assert list(multiset_partitions(2, 1)) == [[[0, 1]]] assert list(multiset_partitions('a')) == [[['a']]] assert list(multiset_partitions('a', 2)) == [] assert list(multiset_partitions('ab')) == [[['a', 'b']], [['a'], ['b']]] assert list(multiset_partitions('ab', 1)) == [[['a', 'b']]] assert list(multiset_partitions('aaa', 1)) == [['aaa']] assert list(multiset_partitions([1, 1], 1)) == [[[1, 1]]] ans = [('mpsyy', ), ('mpsy', 'y'), ('mps', 'yy'), ('mps', 'y', 'y'), ('mpyy', 's'), ('mpy', 'sy'), ('mpy', 's', 'y'), ('mp', 'syy'), ('mp', 'sy', 'y'), ('mp', 's', 'yy'), ('mp', 's', 'y', 'y'), ('msyy', 'p'), ('msy', 'py'), ('msy', 'p', 'y'), ('ms', 'pyy'), ('ms', 'py', 'y'), ('ms', 'p', 'yy'), ('ms', 'p', 'y', 'y'), ('myy', 'ps'), ('myy', 'p', 's'), ('my', 'psy'), ('my', 'ps', 'y'), ('my', 'py', 's'), ('my', 'p', 'sy'), ('my', 'p', 's', 'y'), ('m', 'psyy'), ('m', 'psy', 'y'), ('m', 'ps', 'yy'), ('m', 'ps', 'y', 'y'), ('m', 'pyy', 's'), ('m', 'py', 'sy'), ('m', 'py', 's', 'y'), ('m', 'p', 'syy'), ('m', 'p', 'sy', 'y'), ('m', 'p', 's', 'yy'), ('m', 'p', 's', 'y', 'y')] assert list( tuple("".join(part) for part in p) for p in multiset_partitions('sympy')) == ans factorings = [[24], [8, 3], [12, 2], [4, 6], [4, 2, 3], [6, 2, 2], [2, 2, 2, 3]] assert list( factoring_visitor(p, [2, 3]) for p in multiset_partitions_taocp([3, 1])) == factorings
def test_multiset_partitions(): A = [0, 1, 2, 3, 4] assert list(multiset_partitions(A, 5)) == [[[0], [1], [2], [3], [4]]] assert len(list(multiset_partitions(A, 4))) == 10 assert len(list(multiset_partitions(A, 3))) == 25 assert list(multiset_partitions([1, 1, 1, 2, 2], 2)) == [ [[1, 1, 1, 2], [2]], [[1, 1, 1], [2, 2]], [[1, 1, 2, 2], [1]], [[1, 1, 2], [1, 2]], [[1, 1], [1, 2, 2]]] assert list(multiset_partitions([1, 1, 2, 2], 2)) == [ [[1, 1, 2], [2]], [[1, 1], [2, 2]], [[1, 2, 2], [1]], [[1, 2], [1, 2]]] assert list(multiset_partitions([1, 2, 3, 4], 2)) == [ [[1, 2, 3], [4]], [[1, 2, 4], [3]], [[1, 2], [3, 4]], [[1, 3, 4], [2]], [[1, 3], [2, 4]], [[1, 4], [2, 3]], [[1], [2, 3, 4]]] assert list(multiset_partitions([1, 2, 2], 2)) == [ [[1, 2], [2]], [[1], [2, 2]]] assert list(multiset_partitions(3)) == [ [[0, 1, 2]], [[0, 1], [2]], [[0, 2], [1]], [[0], [1, 2]], [[0], [1], [2]]] assert list(multiset_partitions(3, 2)) == [ [[0, 1], [2]], [[0, 2], [1]], [[0], [1, 2]]] assert list(multiset_partitions([1] * 3, 2)) == [[[1], [1, 1]]] assert list(multiset_partitions([1] * 3)) == [ [[1, 1, 1]], [[1], [1, 1]], [[1], [1], [1]]] a = [3, 2, 1] assert list(multiset_partitions(a)) == \ list(multiset_partitions(sorted(a))) assert list(multiset_partitions(a, 5)) == [] assert list(multiset_partitions(a, 1)) == [[[1, 2, 3]]] assert list(multiset_partitions(a + [4], 5)) == [] assert list(multiset_partitions(a + [4], 1)) == [[[1, 2, 3, 4]]] assert list(multiset_partitions(2, 5)) == [] assert list(multiset_partitions(2, 1)) == [[[0, 1]]] assert list(multiset_partitions('a')) == [[['a']]] assert list(multiset_partitions('a', 2)) == [] assert list(multiset_partitions('ab')) == [[['a', 'b']], [['a'], ['b']]] assert list(multiset_partitions('ab', 1)) == [[['a', 'b']]] assert list(multiset_partitions('aaa', 1)) == [['aaa']] assert list(multiset_partitions([1, 1], 1)) == [[[1, 1]]] ans = [('mpsyy',), ('mpsy', 'y'), ('mps', 'yy'), ('mps', 'y', 'y'), ('mpyy', 's'), ('mpy', 'sy'), ('mpy', 's', 'y'), ('mp', 'syy'), ('mp', 'sy', 'y'), ('mp', 's', 'yy'), ('mp', 's', 'y', 'y'), ('msyy', 'p'), ('msy', 'py'), ('msy', 'p', 'y'), ('ms', 'pyy'), ('ms', 'py', 'y'), ('ms', 'p', 'yy'), ('ms', 'p', 'y', 'y'), ('myy', 'ps'), ('myy', 'p', 's'), ('my', 'psy'), ('my', 'ps', 'y'), ('my', 'py', 's'), ('my', 'p', 'sy'), ('my', 'p', 's', 'y'), ('m', 'psyy'), ('m', 'psy', 'y'), ('m', 'ps', 'yy'), ('m', 'ps', 'y', 'y'), ('m', 'pyy', 's'), ('m', 'py', 'sy'), ('m', 'py', 's', 'y'), ('m', 'p', 'syy'), ('m', 'p', 'sy', 'y'), ('m', 'p', 's', 'yy'), ('m', 'p', 's', 'y', 'y')] assert [tuple("".join(part) for part in p) for p in multiset_partitions('sympy')] == ans factorings = [[24], [8, 3], [12, 2], [4, 6], [4, 2, 3], [6, 2, 2], [2, 2, 2, 3]] assert [factoring_visitor(p, [2, 3]) for p in multiset_partitions_taocp([3, 1])] == factorings