Exemplo n.º 1
0
def test_merge_sort_sorts_the_challenge_list():
    array = parse_text(text)
    result = merge_sort(array, sort_in_ascending)
    assert result[:20] == [
        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
    ]
    assert result[-20:] == [
        9981, 9982, 9983, 9984, 9985, 9986, 9987, 9988, 9989, 9990, 9991, 9992,
        9993, 9994, 9995, 9996, 9997, 9998, 9999, 10000
    ]
Exemplo n.º 2
0
def test_merge_sort_has_right_number_of_comparisons():
    track = {'comparisons': 0, 'copies': 0}
    array = parse_text(text)
    result = merge_sort(array, sort_ascending_while_tracking, track)
    assert result[:20] == [
        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
    ]
    assert result[-20:] == [
        9981, 9982, 9983, 9984, 9985, 9986, 9987, 9988, 9989, 9990, 9991, 9992,
        9993, 9994, 9995, 9996, 9997, 9998, 9999, 10000
    ]
    assert track['comparisons'] == 120473
    assert track['copies'] == 133616
Exemplo n.º 3
0
def time_algorithm(*args):
    # accept a function and pass in a lambda
    array = parse_text(text)
    start_time = time.time()
    if len(args) == 1:
        args[0](array)
    elif len(args) == 2:
        args[0](array, args[1])
    elif len(args) == 3:
        args[0](array, args[1], args[2])
    elif len(args) == 4:
        args[0](array, args[1], args[2], args[3])
    elif len(args) == 5:
        args[0](array, args[1], args[2], args[3], args[4])
    execution_time = (time.time() - start_time)
    return execution_time
Exemplo n.º 4
0
def test_quick_sort_has_right_number_of_comparisons():
    track = {'comparisons': 0, 'copies': 0}
    array = parse_text(text)
    quick_sort(array, 0, len(array) - 1, sort_ascending_while_tracking, pivot_on_zero, track)
    assert track['comparisons'] == 171705
    assert track['copies'] == 113769
Exemplo n.º 5
0
def test_quick_sort_sorts_the_challenge_list():
    array = parse_text(text)
    quick_sort(array, 0, len(array) - 1, sort_in_ascending, pivot_on_zero)
    assert array[:20] == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
    assert array[-20:] == [9981, 9982, 9983, 9984, 9985, 9986, 9987, 9988, 9989, 9990, 9991, 9992, 9993, 9994, 9995,
                           9996, 9997, 9998, 9999, 10000]
Exemplo n.º 6
0
def test_parse_text_parses_text():
    array = parse_text(text)
    assert array[:10] == [
        2148, 9058, 7742, 3153, 6324, 609, 7628, 5469, 7017, 504
    ]
Exemplo n.º 7
0
def test_quick_select_gets_median_of_text():
    array = parse_text(text)
    result = quick_select(array, 0, len(array) - 1, len(array) / 2, sort_in_ascending)
    assert result == 5001
Exemplo n.º 8
0
def test_alg_2(func):
    array = parse_text(text)
    start_time = time.time()
    func(array)
    execution_time = (time.time() - start_time)
    return execution_time