-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Create binary_search_matrix.py #6995
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
71b0f85
Create binary_search_matrix.py
anuragshuklajec b889a90
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 62a23c4
Update binary_search_matrix.py
anuragshuklajec 1252572
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 805b7ed
fix Indentation
anuragshuklajec c400ed0
Update matrix/binary_search_matrix.py
anuragshuklajec File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
def binary_search(array: list, lower_bound: int, upper_bound: int, value: int) -> int: | ||
""" | ||
This function carries out Binary search on a 1d array and | ||
return -1 if it do not exist | ||
array: A 1d sorted array | ||
value : the value meant to be searched | ||
>>> matrix = [1, 4, 7, 11, 15] | ||
>>> binary_search(matrix, 0, len(matrix) - 1, 1) | ||
0 | ||
>>> binary_search(matrix, 0, len(matrix) - 1, 23) | ||
-1 | ||
""" | ||
|
||
r = int((lower_bound + upper_bound) // 2) | ||
if array[r] == value: | ||
return r | ||
if lower_bound >= upper_bound: | ||
return -1 | ||
if array[r] < value: | ||
return binary_search(array, r + 1, upper_bound, value) | ||
else: | ||
return binary_search(array, lower_bound, r - 1, value) | ||
|
||
|
||
def mat_bin_search(value: int, matrix: list) -> list: | ||
""" | ||
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, 6, 9, 16, 22], | ||
... [10, 13, 14, 17, 24], | ||
... [18, 21, 23, 26, 30]] | ||
>>> target = 1 | ||
>>> mat_bin_search(target, matrix) | ||
[0, 0] | ||
>>> target = 34 | ||
>>> mat_bin_search(target, matrix) | ||
[-1, -1] | ||
""" | ||
index = 0 | ||
if matrix[index][0] == value: | ||
return [index, 0] | ||
while index < len(matrix) and matrix[index][0] < value: | ||
r = binary_search(matrix[index], 0, len(matrix[index]) - 1, value) | ||
if r != -1: | ||
return [index, r] | ||
index += 1 | ||
return [-1, -1] | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An error occured while parsing the file: Traceback (most recent call last):
File "/opt/render/project/src/.venv/lib/python3.10/site-packages/libcst/_parser/base_parser.py", line 151, in _add_token
plan = stack[-1].dfa.transitions[transition]
KeyError: ReservedString(import)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/opt/render/project/src/algorithms_keeper/parser/python_parser.py", line 145, in parse
reports = lint_file(
libcst._exceptions.ParserSyntaxError: Syntax Error @ 54:5.
Incomplete input. Encountered 'import', but expected 'INDENT'.
import doctest
^ |
||
|
||
doctest.testmod() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An error occured while parsing the file:
matrix/binary_search_matrix.py