def test(): '''Test basic workings of `combinations`.''' my_range = [0, 1, 2, 3, 4] assert list(combinations(xrange(4), n=2)) == [ [0, 1], [0, 2], [0, 3], [1, 2], [1, 3], [2, 3] ] assert list(combinations(xrange(4), 3)) == [ [0, 1, 2], [0, 1, 3], [0, 2, 3], [1, 2, 3] ] assert tuple(combinations('meowfrr', 5)) == ( ['m', 'e', 'o', 'w', 'f'], ['m', 'e', 'o', 'w', 'r'], ['m', 'e', 'o', 'w', 'r'], ['m', 'e', 'o', 'f', 'r'], ['m', 'e', 'o', 'f', 'r'], ['m', 'e', 'o', 'r', 'r'], ['m', 'e', 'w', 'f', 'r'], ['m', 'e', 'w', 'f', 'r'], ['m', 'e', 'w', 'r', 'r'], ['m', 'e', 'f', 'r', 'r'], ['m', 'o', 'w', 'f', 'r'], ['m', 'o', 'w', 'f', 'r'], ['m', 'o', 'w', 'r', 'r'], ['m', 'o', 'f', 'r', 'r'], ['m', 'w', 'f', 'r', 'r'], ['e', 'o', 'w', 'f', 'r'], ['e', 'o', 'w', 'f', 'r'], ['e', 'o', 'w', 'r', 'r'], ['e', 'o', 'f', 'r', 'r'], ['e', 'w', 'f', 'r', 'r'], ['o', 'w', 'f', 'r', 'r'] ) assert list(combinations(xrange(5), n=2, start=2)) == \ list(combinations(xrange(2, 5), n=2))
def test(): '''Test basic workings of `combinations`.''' my_range = [0, 1, 2, 3, 4] assert list(combinations(xrange(4), n=2)) == [[0, 1], [0, 2], [0, 3], [1, 2], [1, 3], [2, 3]] assert list(combinations(xrange(4), 3)) == [[0, 1, 2], [0, 1, 3], [0, 2, 3], [1, 2, 3]] assert tuple(combinations( 'meowfrr', 5)) == (['m', 'e', 'o', 'w', 'f'], ['m', 'e', 'o', 'w', 'r'], ['m', 'e', 'o', 'w', 'r'], ['m', 'e', 'o', 'f', 'r'], ['m', 'e', 'o', 'f', 'r'], ['m', 'e', 'o', 'r', 'r'], ['m', 'e', 'w', 'f', 'r'], ['m', 'e', 'w', 'f', 'r'], ['m', 'e', 'w', 'r', 'r'], ['m', 'e', 'f', 'r', 'r'], ['m', 'o', 'w', 'f', 'r'], ['m', 'o', 'w', 'f', 'r'], ['m', 'o', 'w', 'r', 'r'], ['m', 'o', 'f', 'r', 'r'], ['m', 'w', 'f', 'r', 'r'], ['e', 'o', 'w', 'f', 'r'], ['e', 'o', 'w', 'f', 'r'], ['e', 'o', 'w', 'r', 'r'], ['e', 'o', 'f', 'r', 'r'], ['e', 'w', 'f', 'r', 'r'], ['o', 'w', 'f', 'r', 'r']) assert list(combinations(xrange(5), n=2, start=2)) == \ list(combinations(xrange(2, 5), n=2))
def all_equal(iterable, exhaustive=False): ''' Return whether all elements in the iterable are equal to each other. If `exhaustive` is set to `False`, it's assumed that the equality relation is transitive, therefore not every member is tested against every other member. So in a list of size `n`, `n-1` equality checks will be made. If `exhaustive` is set to `True`, every member will be checked against every other member. So in a list of size `n`, `(n*(n-1))/2` equality checks will be made. ''' # todo: Maybe I should simply check if `len(set(iterable)) == 1`? Will not # work for unhashables. if exhaustive is True: pairs = sequence_tools.combinations(list(iterable), 2) else: # exhaustive is False pairs = cute_iter_tools.iterate_overlapping_subsequences(iterable) return all(a==b for (a, b) in pairs)
def all_equal(iterable, exhaustive=False): ''' Return whether all elements in the iterable are equal to each other. If `exhaustive` is set to `False`, it's assumed that the equality relation is transitive, therefore not every member is tested against every other member. So in a list of size `n`, `n-1` equality checks will be made. If `exhaustive` is set to `True`, every member will be checked against every other member. So in a list of size `n`, `(n*(n-1))/2` equality checks will be made. ''' # todo: Maybe I should simply check if `len(set(iterable)) == 1`? Will not # work for unhashables. if exhaustive is True: pairs = sequence_tools.combinations(list(iterable), 2) else: # exhaustive is False pairs = cute_iter_tools.iterate_overlapping_subsequences(iterable) return all(a == b for (a, b) in pairs)
def test_all_sizes(): '''Test using `n=None` so combinations of all sizes are returned.''' assert list(combinations(xrange(4))) == sequence_tools.flatten( list(combinations(xrange(4), i)) for i in xrange(1, 4+1) )
def test_all_sizes(): '''Test using `n=None` so combinations of all sizes are returned.''' assert list(combinations(xrange(4))) == sequence_tools.flatten( list(combinations(xrange(4), i)) for i in xrange(1, 4 + 1))