예제 #1
0
 def test_binary_search(self):
     array = [1, 3, 7, 10, 11]
     self.assertEqual(binary_search.binary_search([1, 2, 4], 3), -1)
     self.assertEqual(binary_search.binary_search(array, 3), 0)
     self.assertEqual(binary_search.binary_search(array, 5), -1)
     self.assertEqual(binary_search.binary_search([15, 16, 19, 20], 20),
                      0)
     self.assertEqual(binary_search.binary_search([16, 19, 20, 15], 15),
                      -1)
    def test_not_found(self):
        t = 3
        a = [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

        i = binary_search(a, t)

        assert None == i
def test_find_common_elements():
    list1 = [13, 27, 35, 40, 49, 55, 59]
    list2 = [17, 35, 39, 40, 55, 58, 60]

    min_index1 = None
    min_index2 = None
    starting_index = 0
    for index, elem in enumerate(list1):
        min_index2 = binary_search(list2, starting_index, len(list2)-1, elem)
        if min_index2[0]:
            min_index2 = min_index2[1]
            min_index1 = index
            break
        starting_index = min_index2[1]

    if min_index1 is not None:
        result = [list1[min_index1]]
        min_index1 += 1
        min_index2 += 1
        while min_index1 < len(list1) and min_index2 < len(list2):
            if list1[min_index1] == list2[min_index2]:
                result.append(list1[min_index1])
                min_index1 += 1
                min_index2 += 1
            elif list1[min_index1] > list2[min_index2]:
                min_index2 += 1
            else:
                min_index1 += 1

        assert sorted(list(set(list1) & set(list2))) == result
    def test_binary_search(self):
        t = 8
        a = [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

        i = binary_search(a, t)

        assert t == a[i]
예제 #5
0
def test_binary_search_not_found_unsorted():
    items = [9, 3, 7, 5, 1]
    wanted = 3

    index = binary_search(items, wanted)
    assert index == None
예제 #6
0
    def test_empty_list(self):
        self.list = []
        found = binary_search.binary_search(self.list, 1)

        self.assertFalse(found)
예제 #7
0
    def test_for_element_not_in_list(self):
        found = binary_search.binary_search(self.list, 100001)

        self.assertFalse(found)
예제 #8
0
 def testBaseCaseNotFound(self):
     self.assertFalse(binary_search([1], 2))
예제 #9
0
 def testNormalCase3(self):
     self.assertTrue(binary_search([1, 4, 6, 7, 8, 10, 12, 15, 17], 8))
예제 #10
0
 def testNormalCase3(self):
     self.assertTrue(binary_search([1, 4, 6, 7, 8, 10, 12, 15, 17], 8))
예제 #11
0
    def test_list_of_one_element(self):
        self.list = [1]
        found = binary_search.binary_search(self.list, 1)

        self.assertTrue(found)
예제 #12
0
 def test_return_none_if_target_not_present(self):
     # given & when
     index_found = binary_search([1, 2, 3, 6], 5)
     # then
     self.assertEqual(index_found, None)
예제 #13
0
 def test_binary_search_finds_last_index(self):
     # given & when
     index_found = binary_search([1, 2, 3, 6], 6)
     # then
     self.assertEqual(index_found, 3)
예제 #14
0
 def test_binary_search_finds_first_index(self):
     # given & when
     index_found = binary_search([1, 2, 3, 4], 1)
     # then
     self.assertEqual(index_found, 0)
예제 #15
0
파일: sort.py 프로젝트: richard-to/bscs
 def testMergeSort(self):
     numlist = randomSequence()
     numfind = choice(numlist)
     result = mergeSort(numlist)
     self.assertTrue(binary_search(result, numfind))
예제 #16
0
    def test_empty_list(self):
        self.list = []
        found = binary_search.binary_search(self.list, 1)

        self.assertFalse(found)
예제 #17
0
def test_binary_search_finds_sorted():
    items = [1, 3, 5, 7, 9]
    wanted = 7

    index = binary_search(items, wanted)
    assert index == 3
예제 #18
0
def test_binary_search(test_input, value, expected):
    out = binary_search.binary_search(test_input, value)
    assert out == expected
예제 #19
0
 def test_binary_search(self):
     # given & when
     index_found = binary_search([1, 2, 3, 4], 3)
     # then
     self.assertEqual(index_found, 2)
예제 #20
0
    def test_list_of_odd_number_of_elements(self):
        self.list = [i for i in range(10001)]
        found = binary_search.binary_search(self.list, 500)

        self.assertTrue(found)
예제 #21
0
def test_binary_search():
    keys = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    expected_res = 9
    actual_res = binary_search(keys, 10)
    assert actual_res == expected_res
예제 #22
0
    def test_for_element_in_list(self):
        found = binary_search.binary_search(self.list, 69)

        self.assertTrue(found)
예제 #23
0
 def testBaseCaseNotFound(self):
     self.assertFalse(binary_search([1], 2))
예제 #24
0
    def test_for_element_not_in_list(self):
        found = binary_search.binary_search(self.list, 100001)

        self.assertFalse(found)
예제 #25
0
 def testBaseCaseFound(self):
     self.assertTrue(binary_search([1], 1))
예제 #26
0
    def test_list_of_odd_number_of_elements(self):
        self.list = [i for i in range(10001)]
        found = binary_search.binary_search(self.list, 500)

        self.assertTrue(found)
예제 #27
0
 def testBaseCaseFound(self):
     self.assertTrue(binary_search([1], 1))
예제 #28
0
    def test_list_of_one_element(self):
        self.list = [1]
        found = binary_search.binary_search(self.list, 1)

        self.assertTrue(found)
def test_binary_search(array, value, expected):
    assert type(binary_search(array, value)) == int or\
           type(binary_search(array, value)) == str
    assert binary_search(array, value) == expected
예제 #30
0
    def test_for_element_in_list(self):
        found = binary_search.binary_search(self.list, 69)

        self.assertTrue(found)