Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@
* [Volume Conversions](conversions/volume_conversions.py)
* [Weight Conversion](conversions/weight_conversion.py)

## [Coordinate Compression](/coordinate_compression.py)

## Data Compression
* [Burrows Wheeler](data_compression/burrows_wheeler.py)
* [Coordinate Compression](data_compression/coordinate_compression.py)
Expand Down Expand Up @@ -1243,6 +1245,7 @@

## Searches
* [Binary Search](searches/binary_search.py)
* [Binary Search With Duplicates](searches/binary_search_with_duplicates.py)
* [Binary Tree Traversal](searches/binary_tree_traversal.py)
* [Double Linear Search](searches/double_linear_search.py)
* [Double Linear Search Recursion](searches/double_linear_search_recursion.py)
Expand Down
10 changes: 10 additions & 0 deletions coordinate_compression.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def compress(value):
if value not in coordinate_map:

Check failure on line 2 in coordinate_compression.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F821)

coordinate_compression.py:2:21: F821 Undefined name `coordinate_map`
raise ValueError(f"{value} not found in coordinate map")

Check failure on line 3 in coordinate_compression.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (EM102)

coordinate_compression.py:3:26: EM102 Exception must not use an f-string literal, assign to variable first
return coordinate_map[value]

Check failure on line 4 in coordinate_compression.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F821)

coordinate_compression.py:4:12: F821 Undefined name `coordinate_map`


def decompress(index):
if index < 0 or index >= len(original_values):

Check failure on line 8 in coordinate_compression.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F821)

coordinate_compression.py:8:34: F821 Undefined name `original_values`
raise ValueError(f"Index {index} is out of bounds")

Check failure on line 9 in coordinate_compression.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (EM102)

coordinate_compression.py:9:26: EM102 Exception must not use an f-string literal, assign to variable first
return original_values[index]

Check failure on line 10 in coordinate_compression.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F821)

coordinate_compression.py:10:12: F821 Undefined name `original_values`
21 changes: 21 additions & 0 deletions searches/binary_search_with_duplicates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
def binary_search_first_occurrence(arr: list[int], target: int) -> int:
"""
Return the index of the first occurrence of target in a sorted list.

>>> binary_search_first_occurrence([1, 2, 4, 4, 4, 5], 4)
2
>>> binary_search_first_occurrence([1, 2, 3], 5)
-1
"""
left, right = 0, len(arr) - 1
result = -1
while left <= right:
mid = (left + right) // 2
if arr[mid] == target:
result = mid
right = mid - 1
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1
return result
Loading