Exemplo n.º 1
0
 def test_linear_search_with_items_in_list(self):
     # linear search can find items regardless of list order
     names = ['Winnie', 'Kojin', 'Brian', 'Nabil', 'Julia', 'Alex', 'Nick']
     # linear search should return the index of each item in the list
     assert linear_search(names, 'Winnie') == 0
     assert linear_search(names, 'Kojin') == 1
     assert linear_search(names, 'Brian') == 2
     assert linear_search(names, 'Nabil') == 3
     assert linear_search(names, 'Julia') == 4
     assert linear_search(names, 'Alex') == 5
     assert linear_search(names, 'Nick') == 6
     assert linear_search(names, 'Jeremy') is None
     assert linear_search(names, 'nobody') is None
Exemplo n.º 2
0
def search(list, name, method):
    time1 = timeit.default_timer()
    if (method == 0):  #linear_search
        relatorio = linear_search(list, name)

    else:  #binary_search
        relatorio = binary_search(list, name)
    time2 = timeit.default_timer()

    relatorio.time = time2 - time1
    relatorio.text = "Tempo para buscar dado: "
    return (relatorio)
Exemplo n.º 3
0
def test_search_not_found():
    gen = make_data()
    data = next(gen)

    result = linear_search(data, DATA_SIZE * 4)
    assert not result

    result = binary_search(data, DATA_SIZE * 4)
    assert not result

    result = jump_search(data, DATA_SIZE * 4)
    assert not result
Exemplo n.º 4
0
def test_search_at_middle():
    gen = make_data()
    data = next(gen)

    result = linear_search(data, data[(DATA_SIZE // 2) - 1])
    assert result

    result = binary_search(data, data[(DATA_SIZE // 2) - 1])
    assert result

    result = jump_search(data, data[(DATA_SIZE // 2) - 1])
    assert result
Exemplo n.º 5
0
def test_search_at_beginning():
    gen = make_data()
    data = next(gen)

    result = linear_search(data, data[0])
    assert result

    result = binary_search(data, data[0])
    assert result

    result = jump_search(data, data[0])
    assert result
Exemplo n.º 6
0
 def test_binary_search_with_items_in_list2(self):
     # binary search requires list values to be in sorted order
     names = [
         'Person0', 'Person1', 'Person2', 'Person3', 'Person4', 'Person5',
         'Person6'
     ]
     # binary search should return the index of each item in the list
     assert linear_search(names, 'Person0') == 0
     assert linear_search(names, 'Person1') == 1
     assert linear_search(names, 'Person2') == 2
     assert linear_search(names, 'Person3') == 3
     assert linear_search(names, 'Person4') == 4
     assert linear_search(names, 'Person5') == 5
     assert linear_search(names, 'Person6') == 6
Exemplo n.º 7
0
def test_search_at_end():
    gen = make_data()
    data = next(gen)

    start = time.perf_counter()
    result = binary_search(data, data[-1])
    fastest = time.perf_counter() - start
    assert result

    start = time.perf_counter()
    result = linear_search(data, data[-1])
    slowest = time.perf_counter() - start
    assert result
    assert fastest * 10000 < slowest * 10000

    start = time.perf_counter()
    result = jump_search(data, data[-1])
    fastest = time.perf_counter() - start
    assert result
    assert fastest * 10000 < slowest * 10000
 def test_search(self):
     data = [1, 2, 3, 5, 6, 12, 7, 4, 8]
     self.assertEqual(linear_search(data, 6), 4)
     self.assertEqual(linear_search(data, 10), -1)
     self.assertEqual(binarySearch(data, 6), 4)
Exemplo n.º 9
0
 def test_not_found(self):
     size = 723
     array = self.make_random_array(size)
     self.assertEqual(linear_search(array, size + 100), -1)
     self.assertEqual(binary_search(array, size + 100), -1)
Exemplo n.º 10
0
 def testLinear(self):
     data = [1, 2, 3, 4, 5]
     self.assertEqual(linear_search(data, 2), 1)
     self.assertEqual(linear_search(data, 10), -1)
Exemplo n.º 11
0
 def testChar(self):
     data = ['a', 'e', 'i', 'o', 'u']
     self.assertEqual(linear_search(data, 'a'), 0)
     self.assertEqual(linear_search(data, 'z'), -1)
     self.assertEqual(linear_search(data, 'u'), 4)