コード例 #1
0
def join_subsequences_by_sign_of_elements(sequence):
    '''Join subsequences in `sequence` by sign of elements.

    ::

        >>> sequence = [[1, 2], [3, 4], [-5, -6, -7], [-8, -9, -10], [11, 12]]
        >>> sequencetools.join_subsequences_by_sign_of_elements(sequence)
        [[1, 2, 3, 4], [-5, -6, -7, -8, -9, -10], [11, 12]]

    ::

        >>> sequence = [[1, 2], [], [], [3, 4, 5], [6, 7]]
        >>> sequencetools.join_subsequences_by_sign_of_elements(sequence)
        [[1, 2], [], [3, 4, 5, 6, 7]]

    Returns new list.
    '''

    if not isinstance(sequence, list):
        raise TypeError

    if not all(isinstance(x, list) for x in sequence):
        raise TypeError

    if any(mathtools.get_shared_numeric_sign(x) is None for x in sequence):
        raise ValueError

    result = []

    for sublist in sequence:
        try:
            previous_sublist = result[-1]
            if mathtools.get_shared_numeric_sign(previous_sublist) == \
                mathtools.get_shared_numeric_sign(sublist):
                previous_sublist.extend(sublist)
            else:
                result.append(sublist[:])
        except IndexError:
            result.append(sublist[:])

    return result
コード例 #2
0
def lengths_to_joined_positive_counts(sequence):
    '''Return a list of positive counts grouped into sublists.

    ::

        >>> import lidercfeny

    ::

        >>> sequence = [1, -1, -2, 1, -2, -1, -2, 2, 1, -3, -1, 2, -2, -1, -1]
        >>> lidercfeny.etc.transforms.lengths_to_joined_positive_counts(sequence)
        [[1], [5], [11, 12, 13], [18, 19]]

    '''

    result = baca.tools.map_elements_to_numbered_sublists(sequence)
    result = sequencetools.join_subsequences_by_sign_of_elements(result)
    result = [x for x in result if mathtools.get_shared_numeric_sign(x) == 1]

    return result
コード例 #3
0
def test_mathtools_get_shared_numeric_sign_01():

    assert mathtools.get_shared_numeric_sign([1, 2, 3]) == 1
    assert mathtools.get_shared_numeric_sign([-1, -2, -3]) == -1
    assert mathtools.get_shared_numeric_sign([]) == 0
    assert mathtools.get_shared_numeric_sign([1, 2, -3]) is None
コード例 #4
0
def join_subsequences_by_sign_of_elements(sequence):
    '''Joins subsequences in `sequence` by sign of elements.

    ..  container:: example

        **Example 1.** Joins lists by sign of elements:

        ::

            >>> sequence = [[1, 2], [3, 4], [-5, -6, -7], [-8, -9, -10], [11, 12]]
            >>> sequencetools.join_subsequences_by_sign_of_elements(sequence)
            [[1, 2, 3, 4], [-5, -6, -7, -8, -9, -10], [11, 12]]

    ..  container:: example

        **Example 2.** Works with empty lists:

        ::

            >>> sequence = [[1, 2], [], [], [3, 4, 5], [6, 7]]
            >>> sequencetools.join_subsequences_by_sign_of_elements(sequence)
            [[1, 2], [], [3, 4, 5, 6, 7]]

    ..  container:: example

        **Example 3.** Joins tuples by sign of elements:

        ::

            >>> sequence = [(1, 2), (), (), (3, 4, 5), (6, 7)]
            >>> sequencetools.join_subsequences_by_sign_of_elements(sequence)
            [(1, 2), (), (3, 4, 5, 6, 7)]

    Returns new list.
    '''

    if not isinstance(sequence, collections.Sequence):
        message = 'must be sequence: {!r}.'
        message = message.format(sequence)
        raise Exception(message)

    if not all(isinstance(x, collections.Sequence) for x in sequence):
        message = 'must contain only sequences: {!r}.'
        message = message.format(sequence)
        raise Exception(message)

    if any(mathtools.get_shared_numeric_sign(x) is None for x in sequence):
        raise ValueError

    sequence_type = type(sequence)

    result = []

    for subsequence in sequence:
        try:
            previous_subsequence = result[-1]
            previous_subsequence_type = type(previous_subsequence)
            previous_sublist = list(previous_subsequence)
            if mathtools.get_shared_numeric_sign(previous_subsequence) == \
                mathtools.get_shared_numeric_sign(subsequence):
                previous_sublist.extend(subsequence)
                previous_subsequence = previous_subsequence_type(
                    previous_sublist)
                result[-1] = previous_subsequence
            else:
                result.append(subsequence[:])
        except IndexError:
            result.append(subsequence[:])

    result = sequence_type(result)

    return result