def test_mathtools_integer_to_base_k_tuple_01():

    assert mathtools.integer_to_base_k_tuple(0, 2) == (0, )
    assert mathtools.integer_to_base_k_tuple(1, 2) == (1, )
    assert mathtools.integer_to_base_k_tuple(2, 2) == (1, 0)
    assert mathtools.integer_to_base_k_tuple(3, 2) == (1, 1)
    assert mathtools.integer_to_base_k_tuple(4, 2) == (1, 0, 0)
    assert mathtools.integer_to_base_k_tuple(5, 2) == (1, 0, 1)
    assert mathtools.integer_to_base_k_tuple(6, 2) == (1, 1, 0)
    assert mathtools.integer_to_base_k_tuple(7, 2) == (1, 1, 1)
    assert mathtools.integer_to_base_k_tuple(8, 2) == (1, 0, 0, 0)
def test_mathtools_integer_to_base_k_tuple_01():

    assert mathtools.integer_to_base_k_tuple(0, 2) == (0, )
    assert mathtools.integer_to_base_k_tuple(1, 2) == (1, )
    assert mathtools.integer_to_base_k_tuple(2, 2) == (1, 0)
    assert mathtools.integer_to_base_k_tuple(3, 2) == (1, 1)
    assert mathtools.integer_to_base_k_tuple(4, 2) == (1, 0, 0)
    assert mathtools.integer_to_base_k_tuple(5, 2) == (1, 0, 1)
    assert mathtools.integer_to_base_k_tuple(6, 2) == (1, 1, 0)
    assert mathtools.integer_to_base_k_tuple(7, 2) == (1, 1, 1)
    assert mathtools.integer_to_base_k_tuple(8, 2) == (1, 0, 0, 0)
示例#3
0
def yield_all_k_ary_sequences_of_length(k, length):
    '''Yields all `k`-ary sequences of `length`.

    ::

        >>> for sequence in sequencetools.yield_all_k_ary_sequences_of_length(2, 3):
        ...     sequence
        ...
        (0, 0, 0)
        (0, 0, 1)
        (0, 1, 0)
        (0, 1, 1)
        (1, 0, 0)
        (1, 0, 1)
        (1, 1, 0)
        (1, 1, 1)

    Returns generator of tuples.
    '''

    assert isinstance(k, int) and 1 <= k
    assert isinstance(length, int) and 0 <= length

    total_sequences = k**length
    for rank in range(total_sequences):
        result = list(mathtools.integer_to_base_k_tuple(rank, k))
        n_leading_zeros = length - len(result)
        leading_zeros = n_leading_zeros * [0]
        # to avoid pychecker slice assignment bug
        #result[0:0] = leading_zeros
        result.__setitem__(slice(0, 0), leading_zeros)
        yield tuple(result)
def yield_all_k_ary_sequences_of_length(k, length):
    '''Yields all `k`-ary sequences of `length`.

    ::

        >>> for sequence in sequencetools.yield_all_k_ary_sequences_of_length(2, 3):
        ...     sequence
        ...
        (0, 0, 0)
        (0, 0, 1)
        (0, 1, 0)
        (0, 1, 1)
        (1, 0, 0)
        (1, 0, 1)
        (1, 1, 0)
        (1, 1, 1)

    Returns generator of tuples.
    '''

    assert isinstance(k, int) and 1 <= k
    assert isinstance(length, int) and 0 <= length

    total_sequences = k ** length
    for rank in range(total_sequences):
        result = list(mathtools.integer_to_base_k_tuple(rank, k))
        n_leading_zeros = length - len(result)
        leading_zeros = n_leading_zeros * [0]
        # to avoid pychecker slice assignment bug
        #result[0:0] = leading_zeros
        result.__setitem__(slice(0, 0), leading_zeros)
        yield tuple(result)
def test_mathtools_integer_to_base_k_tuple_02():

    assert mathtools.integer_to_base_k_tuple(1066, 10) == (1, 0, 6, 6)
    assert mathtools.integer_to_base_k_tuple(1987, 10) == (1, 9, 8, 7)
    assert mathtools.integer_to_base_k_tuple(3012, 10) == (3, 0, 1, 2)
def test_mathtools_integer_to_base_k_tuple_02():

    assert mathtools.integer_to_base_k_tuple(1066, 10) == (1, 0, 6, 6)
    assert mathtools.integer_to_base_k_tuple(1987, 10) == (1, 9, 8, 7)
    assert mathtools.integer_to_base_k_tuple(3012, 10) == (3, 0, 1, 2)