Beispiel #1
0
def sort_matrix(item, matrix, low=(0,0), high=None):
    high = (len(matrix)-1, len(matrix[0])-1) if high is None else high
    mid, x_remains, y_remains = diag_mid(low, high)

    if item == matrix[mid[0]][mid[1]]:
        return mid
    elif low==high:
        return -1
    elif low[0]==high[0]:
        #we have a single row or column
        index = binary_search_recursive(matrix[low[0]], item, low=low[1], high=high[1])
        if index != -1:
            return (low[0], index)  
        else:
            return index
    elif low[1] == high[1]:
        #we will need to copy the array over by slicing - no longer O(logn) for this part
        new_array = list()
        for i in range(low[0], high[0]):
            new_array.append(matrix[i][low[1]])

        index = binary_search_recursive(new_array, item)
        if index != -1:
            return (low[0]+index,index[1])
        else:
            return index

    if item < matrix[mid[0]][mid[1]]:
        index = sort_matrix(item, matrix, low, mid)
        if index == -1: #and x_remains: 
            index = sort_matrix(item, matrix, low=(mid[0], low[1]), high=(high[0], mid[1])) 
        
        if index == -1: #and y_remains:
            index = sort_matrix(item, matrix, low=(low[0], mid[1]) , high=(mid[0],high[1])) 

    elif item > matrix[mid[0]][mid[1]]:
        if mid == low:
            mid = high

        index = sort_matrix(item, matrix, mid, high)
        if index == -1: #and x_remains: 
            index = sort_matrix(item, matrix, low=(mid[0], low[1]), high=(high[0], mid[1])) 
        
        if index == -1: #and y_remains:
            index = sort_matrix(item, matrix, low=(low[0], mid[1]) , high=(mid[0],high[1]))

    else: #item found
        return mid

    return index
 def test_binary_search_recursive_even_true(self):
     arr = [5, 9, 10, 73]
     val = 73
     min_pos = 0
     max_pos = len(arr) - 1
     answer = binary_search.binary_search_recursive(arr, val, min_pos,
                                                    max_pos)
     self.assertEqual(True, answer)
 def test_binary_search_recursive_odd_false(self):
     arr = [5, 9, 10, 55, 73]
     val = 89
     min_pos = 0
     max_pos = len(arr) - 1
     answer = binary_search.binary_search_recursive(arr, val, min_pos,
                                                    max_pos)
     self.assertEqual(False, answer)
 def test_recursive(self):
     self.assertEqual(
         binary_search_recursive(8, 0,
                                 len(self.sequence) - 1, self.sequence), 3)
     self.assertEqual(
         binary_search_recursive(20, 0,
                                 len(self.sequence) - 1, self.sequence), 6)
     self.assertEqual(
         binary_search_recursive(2, 0,
                                 len(self.sequence) - 1, self.sequence), 0)
     self.assertEqual(
         binary_search_recursive(42, 0,
                                 len(self.sequence) - 1, self.sequence), 7)
     self.assertEqual(
         binary_search_recursive(100, 0,
                                 len(self.sequence) - 1, self.sequence),
         None)
def search_for_two_values(_list, _sum):
    """ Search in list for two items whose sum is equal to parameter _sum. Difficulty is O(n*lg(n)) """
    # sort list by merge sort
    sorted_list = _list[:]
    merge_sort(sorted_list, 0, len(sorted_list) - 1)
    for item in sorted_list:
        if item >= _sum:
            return None
        else:
            second_item = _sum - item
            # use binary search for searching for item in sorted list
            if binary_search_recursive(sorted_list, 0,
                                       len(sorted_list) - 1, second_item):
                return {
                    item: _list.index(item),
                    second_item: _list.index(second_item)
                }
    return None
Beispiel #6
0
def test_binary_search_recursive():
    array = [0, 1, 2, 3, 4, 5]
    index = binary_search_recursive(array, 0)
    assert index == True
    index = binary_search_recursive(array, 3)
    assert index == True
    index = binary_search_recursive(array, -1)
    assert index == False
    index = binary_search_recursive(array, 1)
    assert index == True
    index = binary_search_recursive(array, 2)
    assert index == True
    index = binary_search_recursive(array, 5)
    assert index == True
def test_binary_search_recursive(input_list, item, expected):
    """Test binary search recursive"""

    assert binary_search_recursive(input_list, item) == expected