MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / mat_bin_search

Function mat_bin_search

matrix/binary_search_matrix.py:25–51  ·  view source on GitHub ↗

This function loops over a 2d matrix and calls binarySearch on the selected 1d array and returns [-1, -1] is it do not exist value : value meant to be searched matrix = a sorted 2d matrix >>> matrix = [[1, 4, 7, 11, 15], ... [2, 5, 8, 12, 19], ... [3,

(value: int, matrix: list)

Source from the content-addressed store, hash-verified

23
24
25def mat_bin_search(value: int, matrix: list) -> list:
26 """
27 This function loops over a 2d matrix and calls binarySearch on
28 the selected 1d array and returns [-1, -1] is it do not exist
29 value : value meant to be searched
30 matrix = a sorted 2d matrix
31 >>> matrix = [[1, 4, 7, 11, 15],
32 ... [2, 5, 8, 12, 19],
33 ... [3, 6, 9, 16, 22],
34 ... [10, 13, 14, 17, 24],
35 ... [18, 21, 23, 26, 30]]
36 >>> target = 1
37 >>> mat_bin_search(target, matrix)
38 [0, 0]
39 >>> target = 34
40 >>> mat_bin_search(target, matrix)
41 [-1, -1]
42 """
43 index = 0
44 if matrix[index][0] == value:
45 return [index, 0]
46 while index < len(matrix) and matrix[index][0] < value:
47 r = binary_search(matrix[index], 0, len(matrix[index]) - 1, value)
48 if r != -1:
49 return [index, r]
50 index += 1
51 return [-1, -1]
52
53
54if __name__ == "__main__":

Callers

nothing calls this directly

Calls 1

binary_searchFunction · 0.70

Tested by

no test coverage detected