Exemplo n.º 1
0
    def test_random(self):

        failures = []

        for i in range(0, self.random_runs):
            len_list = random.randrange(self.min_len, self.max_len)

            random_list = self.random_list_gen.get_list(len_list)

            a1_out = enumeration(random_list)
            a2_out = better_enumeration(random_list)
            a3_out = divide_and_conquer(random_list)
            # a3_out = a2_out
            a4_out = dynamic_programming(random_list)

            if a1_out != a2_out or a1_out != a3_out or a1_out != a4_out:
                failures.append((random_list, a1_out, a2_out, a3_out, a4_out))

        message_template = "%s\na1 %s\na2 %s\na3 %s\na4 %s\n\n"
        message = "%d runs, %d failures\n\n" % (self.random_runs, len(failures))

        for failure in failures:
            (failed_list, failed_a1, failed_a2, failed_a3, failed_a4) = failure
            message += message_template % (failed_list, failed_a1, failed_a2, failed_a3, failed_a4)

        message += "%d runs, %d failures\n\n" % (self.random_runs, len(failures))

        self.assertTrue(len(failures) == 0, message)
Exemplo n.º 2
0
    def test_algorithm2_providedInput_shouldReturnMatchProvidedOutput(self):
        test_lists = load_lists_from_file('Problems/MSS_TestProblems-1.txt')
        expected = [(3, 14, 34), (0, 5, 30), (6, 12, 50), (2, 7, 187), (0, 4, 7), (0, 3, 210), (3, 7, 6)]
        actual = []
        for test_list in test_lists:
            actual.append(better_enumeration(test_list))
        message = "For the instructor provided input, should return the max sum of the provided output \n%s, but was \n%s" % (expected, actual)

        self.assertEqual(expected, actual, message)
Exemplo n.º 3
0
    a1_random_lists = random_list_gen.get_lists(100, a1_list_lengths[i])
    start = time.clock()

    # For each of the random lists of the current size
    for random_list in a1_random_lists:
        enumeration(random_list)
    elapsed = time.clock() - start
    out.write("%f, " % elapsed)
out.write_line('')

out.write("Better Enumeration, ")
for i in range(0, len(a2_list_lengths)):
    a2_random_lists = random_list_gen.get_lists(100, a2_list_lengths[i])
    start = time.clock()
    for random_list in a2_random_lists:
        better_enumeration(random_list)
    elapsed = time.clock() - start
    out.write("%f, " % elapsed)
out.write_line('')

out.write("Divide and Conquer, ")
for i in range(0, len(a3_list_lengths)):
    a3_random_lists = random_list_gen.get_lists(100, a3_list_lengths[i])
    start = time.clock()
    for random_list in a3_random_lists:
        divide_and_conquer(random_list)
    elapsed = time.clock() - start
    out.write("%f, " % elapsed)
out.write_line('')

out.write("Dynamic Programming, ")
Exemplo n.º 4
0
from algorithm1.algorithm1 import enumeration
from algorithm2.algorithm2 import better_enumeration
from algorithm3.algorithm3 import divide_and_conquer
from algorithm4.algorithm4 import dynamic_programming
from file_loader.file_loader import load_lists_from_file
from file_writer.file_writer import FileWriter


lists = load_lists_from_file('Problems/MSS_Problems.txt')

out = FileWriter('Output/MSS_Results.txt')

for array_list in lists:

    algorithm1_output = enumeration(array_list)
    algorithm2_output = better_enumeration(array_list)
    algorithm3_output = divide_and_conquer(array_list)
    algorithm4_output = dynamic_programming(array_list)

    out.write_line(array_list)
    out.write_line('enumeration: ')
    out.write_line(algorithm1_output)
    out.write_line('better enumeration: ')
    out.write_line(algorithm2_output)
    out.write_line('divide and conquer: ')
    out.write_line(algorithm3_output)
    out.write_line('dynamic programming: ')
    out.write_line(algorithm4_output)
    out.write_line('\n')

out.close()