예제 #1
0
def split_by_key(seq, key_cmp, min_result_size=1):
    """Returns a list of lists each containing the elements of `seq` with a
       single key as determined by `key_cmp`.

       The result is sorted by `key_cmp`.
    """
    result = [[]]
    source = sorted(seq, key_cmp)
    if source:
        for (a, b) in TFL.pairwise(source):
            result[-1].append(a)
            if key_cmp(a, b) != 0:
                result.append([])
        result[-1].append(source[-1])
    if len(result) < min_result_size:
        result = result + ([[]] * (min_result_size - len(result)))
    return result
예제 #2
0
파일: predicate.py 프로젝트: Tapyr/tapyr
def split_by_key (seq, key_cmp, min_result_size = 1) :
    """Returns a list of lists each containing the elements of `seq` with a
       single key as determined by `key_cmp`.

       The result is sorted by `key_cmp`.
    """
    result = [[]]
    source = sorted (seq, key_cmp)
    if source :
        for (a, b) in TFL.pairwise (source) :
            result [-1].append (a)
            if key_cmp (a, b) != 0 :
                result.append ([])
        result [-1].append (source [-1])
    if len (result) < min_result_size :
        result = result + ([[]] * (min_result_size - len (result)))
    return result
예제 #3
0
def is_contiguous(seq):
    """Tells whether the sequence of integers in `seq` is contiguous

       >>> is_contiguous ([1, 2, 3, 4, 5])
       True
       >>> is_contiguous ([10, 8, 9])
       True
       >>> is_contiguous ([42])
       True
       >>> is_contiguous ([])
       True
       >>> is_contiguous ([1, 3, 4])
       False
    """
    for l, r in TFL.pairwise(sorted(seq)):
        try:
            if (r - l) != 1:
                return False
        except TypeError:
            return False
    return True
예제 #4
0
파일: predicate.py 프로젝트: Tapyr/tapyr
def is_contiguous (seq) :
    """Tells whether the sequence of integers in `seq` is contiguous

       >>> is_contiguous ([1, 2, 3, 4, 5])
       True
       >>> is_contiguous ([10, 8, 9])
       True
       >>> is_contiguous ([42])
       True
       >>> is_contiguous ([])
       True
       >>> is_contiguous ([1, 3, 4])
       False
    """
    for l, r in TFL.pairwise (sorted (seq)) :
        try :
            if (r - l) != 1 :
                return False
        except TypeError :
            return False
    return True
예제 #5
0
def dusplit(seq, decorator, min_result_size=1):
    """Returns a list of lists each containing the elements of `seq`
       comparing equal under `decorator` (`dusplit` is to `split_by_key` what
       `dusort` is to `sorted`).

       >>> dusplit ([(0,1), (1,1), (0,2), (0,3), (2,3)], lambda x : x [0])
       [[(0, 1), (0, 2), (0, 3)], [(1, 1)], [(2, 3)]]
       >>> dusplit ([(0,1), (1,1), (0,2), (0,3), (2,3)], lambda x : x [1])
       [[(0, 1), (1, 1)], [(0, 2)], [(0, 3), (2, 3)]]
    """
    result = [[]]
    temp = [(decorator(p), i, p) for (i, p) in enumerate(seq)]
    if temp:
        temp.sort()
        for (a, b) in TFL.pairwise(temp):
            result[-1].append(a[-1])
            if a[0] != b[0]:
                result.append([])
        result[-1].append(temp[-1][-1])
    if len(result) < min_result_size:
        result = result + ([[]] * (min_result_size - len(result)))
    return result
예제 #6
0
파일: predicate.py 프로젝트: Tapyr/tapyr
def dusplit (seq, decorator, min_result_size = 1) :
    """Returns a list of lists each containing the elements of `seq`
       comparing equal under `decorator` (`dusplit` is to `split_by_key` what
       `dusort` is to `sorted`).

       >>> dusplit ([(0,1), (1,1), (0,2), (0,3), (2,3)], lambda x : x [0])
       [[(0, 1), (0, 2), (0, 3)], [(1, 1)], [(2, 3)]]
       >>> dusplit ([(0,1), (1,1), (0,2), (0,3), (2,3)], lambda x : x [1])
       [[(0, 1), (1, 1)], [(0, 2)], [(0, 3), (2, 3)]]
    """
    result = [[]]
    temp   = [(decorator (p), i, p) for (i, p) in enumerate (seq)]
    if temp :
        temp.sort ()
        for (a, b) in TFL.pairwise (temp) :
            result [-1].append (a [-1])
            if a [0] != b [0] :
                result.append ([])
        result [-1].append (temp [-1] [-1])
    if len (result) < min_result_size :
        result = result + ([[]] * (min_result_size - len (result)))
    return result