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))
Exemple #2
0
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 __partially_compact(self):
        """Try to make the NodeSelection a bit more compact."""
        # todo: consider defining a canonic decomposition of a node selection
        # to node ranges. This will make a few things easier, like checking
        # equality.
        first, second = None, None
        for (r1, r2) in sequence_tools.combinations(self.ranges, 2):
            if r1.head in r2:
                second, first = r1, r2
                break
            elif r2.head in r1:
                first, second = r1, r2
                break
            else:
                pass
        if first is not None and second is not None:
            if second.tail in first:
                pass
            else:  # second.tail not in first
                for current in second:
                    if current not in first:
                        break
                if current.parent is first.tail:
                    self.ranges.remove(first)
                    new_range = NodeRange(head=first.head, tail=second.tail)
                else:
                    new_range = NodeRange(head=current, tail=second.tail)
                self.ranges.append(new_range)

            self.ranges.remove(second)
            return
        else:
            raise CompletelyCompact
Exemple #4
0
    def __partially_compact(self):
        '''Try to make the NodeSelection a bit more compact.'''
        # todo: consider defining a canonic decomposition of a node selection
        # to node ranges. This will make a few things easier, like checking
        # equality.
        first, second = None, None
        for (r1, r2) in sequence_tools.combinations(self.ranges, 2):
            if r1.head in r2:
                second, first = r1, r2
                break
            elif r2.head in r1:
                first, second = r1, r2
                break
            else:
                pass
        if first is not None and second is not None:
            if second.tail in first:
                pass
            else:  # second.tail not in first
                for current in second:
                    if current not in first:
                        break
                if current.parent is first.tail:
                    self.ranges.remove(first)
                    new_range = NodeRange(head=first.head, tail=second.tail)
                else:
                    new_range = NodeRange(head=current, tail=second.tail)
                self.ranges.append(new_range)

            self.ranges.remove(second)
            return
        else:
            raise CompletelyCompact
Exemple #5
0
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.consecutive_pairs(iterable)

    return all(a == b for (a, b) in pairs)
Exemple #6
0
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.consecutive_pairs(iterable)
        
    return all(a==b for (a, b) in pairs)
Exemple #7
0
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)
    )