Exemplo n.º 1
0
def hornersrule(coeff, x):
    """
    Horner's Rule is a simple optimization of polynomial evaluation,
    following the form:
    
        y = a0 + x(a1 + x(a2 + ... + x(a_(n-1) + x*an)...)

    Test the equivalent of a 16-bit integer with all bits flipped
    >>> hornersrule([1 for i in range(0, 16)], 2)
    65535
    """
    y = 0
    for i in xrange(len(coeff)-1, -1, -1):
        y = coeff[i] + x*y

    return y

if __name__ == '__main__':
    from filetolist import argstolist
    lines = argstolist(float)

    # Assume the first is x
    x = lines[0]
    lines = lines[1:]

    print x, lines
    print hornersrule(lines, x)
Exemplo n.º 2
0
    [2, 1, 3, 4, 7, 5, 6, 8]
    """
    x = A[r]
    i = p - 1
    for j in xrange(p, r):
        if A[j] <= x:
            i = i + 1
            exchange(A, i, j)
    q = i + 1
    exchange(A, q, r)
    return q

def quicksort(A, p, r):
    """
    >>> A = [2, 8, 7, 1, 3, 5, 6, 4]
    >>> quicksort(A, 0, 7)
    [1, 2, 3, 4, 5, 6, 7, 8]
    """
    if p < r:
        q = partition(A, p, r)
        quicksort(A, p, q - 1)
        quicksort(A, q + 1, r)
    return A

if __name__ == '__main__':
    from filetolist import argstolist
    lines = argstolist(int)

    print lines
    print quicksort(lines, 0, len(lines)-1)