Пример #1
0
def show_queue_stats_comparisons():
    for start_word,target_word in CASES:
        print

        for search_method_name,queue_type in SEARCH_METHODS:
            opts = {
                'search_method': search_method_name,
                'queue': QueueStatsWrapper(queue_type()),
                'dictionary_filename': LOCAL_DICTIONARY
            }

            maybe_path = solution(start_word, target_word, opts)
            q = opts['queue']

            formatted_path = show_path(maybe_path) if maybe_path else 'None'
            node_count = '({} nodes)'.format(len(maybe_path)) if maybe_path else ''

            queue_class_name = 'Queue' if queue_type == Queue else 'PriorityQueue'

            benchmark_config = dict(search_method_name=search_method_name,
                                    queue_class_name=queue_class_name,
                                    start_word=start_word,
                                    target_word=target_word)

            time = timeit(BENCHMARK_STATEMENT_TEMPLATE.format(**benchmark_config),
                          BENCHMARK_SETUP_STATEMENT,
                          number=1)

            print search_method_name
            print '  Time:', time
            print '  Shortest path:', formatted_path, node_count
            print '  Queue stats:  ', q.show_stats()

        print
Пример #2
0
def tests2():
    t0 = time()
    opts = { 'search_method': 'A*', 'dictionary_filename': LOCAL_DICTIONARY }

    for start_word,target_word,path_len in TEST_CASES:
        path = solution(start_word, target_word, opts)
        assert (len(path) if path else None) == path_len

    return 'tests pass in {} seconds!'.format(time() - t0)
Пример #3
0
    def test_0_case1(self):
        print("Start test 1\n")
        out_1 = solution(self.inp_1)
        print("\nDesired output\n")
        print(self.des_out_1)
        print("\nActual output\n")
        print(out_1)
        self.assertEqual(out_1, self.des_out_1)

        print("\nFinish test 1\n")
Пример #4
0
    def test_2_case3(self):
        print("Start test 3\n")
        out_3 = solution(self.inp_3)

        print("\nDesired output\n")
        print(self.des_out_3)
        print("\nActual output\n")
        print(out_3)
        self.assertEqual(out_3, self.des_out_3)

        print("\nFinish test 3\n")
Пример #5
0
    def test_1_case2(self):
        print("Start test 2\n")
        out_2 = solution(self.inp_2)

        print("\nDesired output\n")
        print(self.des_out_2)
        print("\nActual output\n")
        print(out_2)
        self.assertEqual(out_2, self.des_out_2)

        print("\nFinish test 2\n")
Пример #6
0
def run_tests():
    errors = 0
    total_cases = len(CASES)
    for (i, (test_data, correct_result)) in enumerate(CASES):
        result = solution(test_data)
        if result != correct_result:
            print('Expected {}, got {}. Case #{}: {}'.format(
                correct_result, result, i, test_data))
            errors += 1

    if errors:
        print('Solution failed on {} test cases'.format(errors))
    else:
        print('All tests passed ({}/{})'.format(total_cases, total_cases))
Пример #7
0
    def test_solve(self):
        actual1: str = solution("101")
        actual2: str = solution("000")

        self.assertEqual(actual1, 2)
        self.assertEqual(actual2, 0)
Пример #8
0
 def test(self):
     self.assertEqual(solution('abcde', 'cde'), True)
     self.assertEqual(solution('abcde', 'abc'), False)
Пример #9
0
 def test_solution_part_2(self):
     self.assertEqual(solution("testData.txt", 30000000), 18929178)
Пример #10
0
 def test_solution_part_1(self):
     self.assertEqual(solution("testData.txt", 2020), 436)
Пример #11
0
def test():
    global n
    global arr1, arr2, ans
    res = main.solution(n, arr1, arr2)
    assert res != ans
Пример #12
0
 def test(self):
     self.assertEqual(solution('world'), 'dlrow')
     self.assertEqual(solution('hello'), 'olleh')
     self.assertEqual(solution(''), '')
     self.assertEqual(solution('h'), 'h')
Пример #13
0
    ('rue',       'be'),
    ('rue',       'defuse'),
    ('rue',       'bend'),
    ('zoologist', 'zoology')
)


BENCHMARK_SETUP_STATEMENT = """
from __main__ import solution, make_trie, CASES, LOCAL_DICTIONARY, \
                     PriorityQueue, Queue

make_trie(LOCAL_DICTIONARY)
"""


BENCHMARK_STATEMENT_TEMPLATE = """
opts = dict(
    search_method='{search_method_name}',
    queue={queue_class_name}(),
    dictionary_filename=LOCAL_DICTIONARY
)

solution('{start_word}', '{target_word}', opts)
"""


def show_queue_stats_comparisons():
    for start_word,target_word in CASES:
        print

        for search_method_name,queue_type in SEARCH_METHODS:
Пример #14
0
def tests():
    for search_method in ('BFS', 'A*'):
        opts = { 'search_method': search_method }

        assert solution('cat', 'dog', opts) == ('cat', 'cot', 'dot', 'dog')
        assert solution('cat', 'dot', opts) == ('cat', 'cot', 'dot')
        assert solution('cat', 'cot', opts) == ('cat', 'cot')
        assert solution('cat', 'cat', opts) == ('cat', )

        assert solution('fan', 'for', opts) == ('fan', 'fin', 'fir', 'for')

        assert solution('place', 'places', opts) == ('place', 'places')

        assert solution('duck', 'dusty', opts) == ('duck', 'dusk', 'dust', 'dusty')
        assert solution('duck', 'ducked', opts) is None

        assert solution('rue', 'be', opts) == ('rue', 'run', 'runt', 'bunt', 'bent', 'beet', 'bee', 'be')
        assert solution('rue', 'defuse', opts) == ('rue', 'ruse', 'reuse', 'refuse', 'defuse')

        not_a_word_1 = 'NotAWord'
        assert solution('rue', not_a_word_1, opts) is None

        not_a_word_2 = 'plar'
        assert solution(not_a_word_2, 'play', opts) == (not_a_word_2, 'play')

        not_a_word_3 = 'blah'
        assert solution(not_a_word_3, 'defuse', opts) is None

    return 'tests pass!'
Пример #15
0
def test_sample():
    assert solution() is None
Пример #16
0
 def test(self):
     self.assertEqual(solution([1, 2, 10, 5]), [1, 2, 5, 10])
Пример #17
0
 def test_solution(self):
   self.assertEqual(main.solution(), 162)