示例#1
0
def compute():
    """ Runs all algorithms N_EXECUTIONS times. On each execution logs time
    execution results to a file
    """

    with open(LOG_FILE, "w") as output:

        header = "Input{0}Brute Force{0}Constant\n"
        print(header.format("\t"))
        output.write(header.format(", "))

        # We start from 1 because there is no result of a matrix of length 0
        for i in range(1, N_EXECUTIONS):
            matrix = build_matrix(i)

            # Compute brute force algorithm
            start_brute_force = default_timer()
            total_brute_force = brute_force(matrix)
            elapsed_brute_force = default_timer() - start_brute_force

            # Compute constant algorithm
            start_constant = default_timer()
            total_constant = constant(len(matrix))
            elapsed_constant = default_timer() - start_constant

            print("{0}\t{1}\t{2}".format(i, elapsed_brute_force,
                                         elapsed_constant))
            # Logs to file
            output.write("{0}, {1}, {2}\n".format(i, elapsed_brute_force,
                                                  elapsed_constant))
示例#2
0
def test_domino_with_correct_n_numbers():
    """ Compute domino with 7 numbers """
    assert constant(7) == 12, "Not ok"
示例#3
0
def test_domino_with_0_numbers():
    """ Compute domino with 0 numbers """
    try:
        constant(0)
    except Exception as e:
        assert isinstance(e, ValueError)
示例#4
0
def test_domino_with_15_numbers():
    """ Compute domino with 15 numbers """
    assert constant(15) == 56, "Not ok"
示例#5
0
def test_domino_with_42_numbers():
    """ Compute domino with 42 numbers """
    assert constant(42) == 441, "Not ok"
示例#6
0
def test_domino_with_9_numbers():
    """ Compute domino with 9 numbers """
    assert constant(9) == 20, "Not ok"
示例#7
0
def test_domino_with_4_numbers():
    """ Compute domino with 4 numbers """
    assert constant(4) == 4, "Not ok"
示例#8
0
def test_domino_with_3_numbers():
    """ Compute domino with 3 numbers """
    assert constant(3) == 2, "Not ok"
示例#9
0
def test_domino_with_2_numbers():
    """ Compute domino with 2 numbers """
    assert constant(2) == 1, "Not ok"
示例#10
0
def test_domino_with_1_numbers():
    """ Compute domino with 1 numbers """
    assert constant(1) == 0, "Not ok"