Compare commits

...

12 Commits

Author SHA1 Message Date
Hariharan
82ea472a4c
Merge aba42dce0a into f3f32ae3ca 2024-11-22 13:57:25 +01:00
pre-commit-ci[bot]
f3f32ae3ca
[pre-commit.ci] pre-commit autoupdate (#12385)
updates:
- [github.com/astral-sh/ruff-pre-commit: v0.7.3 → v0.7.4](https://github.com/astral-sh/ruff-pre-commit/compare/v0.7.3...v0.7.4)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-11-18 22:07:12 +01:00
Christian Clauss
e3bd7721c8
validate_filenames.py Shebang python for Windows (#12371) 2024-11-15 14:59:14 +01:00
pre-commit-ci[bot]
e3f3d668be
[pre-commit.ci] pre-commit autoupdate (#12370)
* [pre-commit.ci] pre-commit autoupdate

updates:
- [github.com/astral-sh/ruff-pre-commit: v0.7.2 → v0.7.3](https://github.com/astral-sh/ruff-pre-commit/compare/v0.7.2...v0.7.3)
- [github.com/abravalheri/validate-pyproject: v0.22 → v0.23](https://github.com/abravalheri/validate-pyproject/compare/v0.22...v0.23)

* Update sudoku_solver.py

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
2024-11-11 21:05:50 +01:00
Hariharan
aba42dce0a
Delete searches/peakelementin2D.py 2024-10-15 20:03:42 +05:30
pre-commit-ci[bot]
ac0008a523 [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2024-10-15 14:33:40 +00:00
Hariharan
c0b6798952
Update and rename BinarySearch_on_Sorted_2D_Array.py to binarysearch_on_sorted_2d_array.py 2024-10-15 20:03:04 +05:30
Hariharan
3da098ba6f
Update BinarySearch_on_Sorted_2D_Array.py 2024-10-15 19:59:26 +05:30
Hariharan
5afd15a9fe
Update and rename Peak_Element_on_2D.py to peakelementin2D.py 2024-10-15 19:50:27 +05:30
pre-commit-ci[bot]
14f8cf47ec [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2024-10-15 14:17:26 +00:00
Hariharan
03adefc12a
Add files via upload 2024-10-15 19:40:42 +05:30
Hariharan
eeeb9ab48a
Add files via upload 2024-10-15 19:33:08 +05:30
4 changed files with 76 additions and 4 deletions

View File

@ -16,7 +16,7 @@ repos:
- id: auto-walrus - id: auto-walrus
- repo: https://github.com/astral-sh/ruff-pre-commit - repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.7.2 rev: v0.7.4
hooks: hooks:
- id: ruff - id: ruff
- id: ruff-format - id: ruff-format
@ -42,7 +42,7 @@ repos:
pass_filenames: false pass_filenames: false
- repo: https://github.com/abravalheri/validate-pyproject - repo: https://github.com/abravalheri/validate-pyproject
rev: v0.22 rev: v0.23
hooks: hooks:
- id: validate-pyproject - id: validate-pyproject

View File

@ -172,7 +172,7 @@ def solved(values):
def from_file(filename, sep="\n"): def from_file(filename, sep="\n"):
"Parse a file into a list of strings, separated by sep." "Parse a file into a list of strings, separated by sep."
return open(filename).read().strip().split(sep) # noqa: SIM115 return open(filename).read().strip().split(sep)
def random_puzzle(assignments=17): def random_puzzle(assignments=17):

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python3 #!python
import os import os
try: try:

View File

@ -0,0 +1,72 @@
#!/usr/bin/env python3
def binary_search_2d(matrix: list[list[int]], target: int) -> tuple[int, int]:
"""
Searches for a target value in a 2D sorted array.
The matrix is sorted such that each row is in ascending order, and the
first element of each row is greater than the last element of the previous row.
Args:
matrix: A 2D list of integers sorted in ascending order.
target: The integer value to search for.
Returns:
A tuple (row_index, col_index) if found, otherwise (-1, -1).
Raises:
ValueError: If matrix is empty or not rectangular.
>>> binary_search_2d([[1, 3, 5], [7, 9, 11], [12, 13, 15]], 9)
(1, 1)
>>> binary_search_2d([[1, 3, 5], [7, 9, 11], [12, 13, 15]], 4)
(-1, -1)
>>> binary_search_2d([], 1) # doctest: +ELLIPSIS
Traceback (most recent call last):
...
ValueError: matrix must not be empty
>>> binary_search_2d([[1, 2], [3, 4, 5]], 3) # doctest: +ELLIPSIS
Traceback (most recent call last):
...
ValueError: matrix must be rectangular
"""
if not matrix or not matrix[0]:
raise ValueError("matrix must not be empty")
rows = len(matrix)
cols = len(matrix[0])
for row in matrix:
if len(row) != cols:
raise ValueError("matrix must be rectangular")
left, right = 0, rows * cols - 1
while left <= right:
mid = left + (right - left) // 2
mid_value = matrix[mid // cols][mid % cols]
if mid_value == target:
return mid // cols, mid % cols
elif mid_value < target:
left = mid + 1
else:
right = mid - 1
return -1, -1
if __name__ == "__main__":
import doctest
doctest.testmod()
# Example usage
matrix = [[1, 3, 5], [7, 9, 11], [12, 13, 15]]
target = 9
result = binary_search_2d(matrix, target)
if result == (-1, -1):
print(f"{target} was not found in the matrix.")
else:
print(f"{target} was found at position {result} in the matrix.")