Пример #1
0
def part2():
    with ManyLineInput('./input.txt', Ingredient) as data:
        answer = max([
            product2(list(zip(data, partition)))
            for partition in partitions(len(data), 100)
        ])
        print(f"part 2: {answer}")
Пример #2
0
def describe_moment_polynomial(syms, moments_x, moment_y, alpha, b):
    """
    Computes the moment polynomial for E[x^alpha, y^b]
    """
    D = len(syms)
    expr = -moment_y
    coeffs = sp.ntheory.multinomial_coefficients(D, b)
    for beta in partitions(D, b):
        expr += coeffs[beta] * monomial(syms, beta) * moments_x[tuple_add(alpha, beta)]
    return expr
Пример #3
0
def describe_moment_polynomial(syms, moments_x, moment_y, alpha, b):
    """
    Computes the moment polynomial for E[x^alpha, y^b]
    """
    D = len(syms)
    expr = -moment_y
    coeffs = sp.ntheory.multinomial_coefficients(D, b)
    for beta in partitions(D, b):
        expr += coeffs[beta] * monomial(syms, beta) * moments_x[tuple_add(
            alpha, beta)]
    return expr
Пример #4
0
def compute_exact_y_moments(ws, pis, moments_x, alpha, b):
    """
    Compute the exact moments E[x^a y^b] using coefficients ws, pis and moments_x.
    """
    D, _ = ws.shape
    coeffs = sp.ntheory.multinomial_coefficients(D, b)

    ret = 0.
    for beta in partitions(D, b):
        ret += coeffs[beta] * evaluate_mixture(ws, pis, beta) * moments_x[tuple_add(alpha, beta)]
    return ret
Пример #5
0
def compute_exact_y_moments(ws, pis, moments_x, alpha, b):
    """
    Compute the exact moments E[x^a y^b] using coefficients ws, pis and moments_x.
    """
    D, _ = ws.shape
    coeffs = sp.ntheory.multinomial_coefficients(D, b)

    ret = 0.
    for beta in partitions(D, b):
        ret += coeffs[beta] * evaluate_mixture(
            ws, pis, beta) * moments_x[tuple_add(alpha, beta)]
    return ret
Пример #6
0
def test_partitions():
    try:
        partitions([1, 2, 3, 4], 0)
        raise AssertionError("Expected ValueError")
    except ValueError:
        pass

    try:
        partitions([1, 2, 3, 4], -1)
        raise AssertionError("Expected ValueError")
    except ValueError:
        pass

    # strings
    assert partitions("abcd", 1) == 4
    assert partitions("abcd", 2) == 2
    assert partitions("abcd", 3) == 2

    assert partitions("_ä__", 2) == 2

    assert partitions("", 1) == 0

    # lists
    assert partitions([1, 2, 3, 4], 1) == 4
    assert partitions([1, 2, 3, 4], 2) == 2
    assert partitions([1, 2, 3, 4], 3) == 2

    assert partitions([], 1) == 0