Skip to content

Commit e94ff2c

Browse files
committed
fix: total cleanup of jump_search for ruff and mypy
1 parent c23157f commit e94ff2c

File tree

1 file changed

+21
-26
lines changed

1 file changed

+21
-26
lines changed

searches/jump_search.py

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,66 @@
1-
from typing import Any
1+
#!/usr/bin/env python3
22
"""
33
Pure Python implementation of the jump search algorithm.
4-
This algorithm iterates through a sorted collection with a step of n^(1/2),
5-
until the element compared is bigger than the one searched.
6-
It will then perform a linear search until it matches the wanted number.
7-
If not found, it returns -1.
8-
9-
https://en.wikipedia.org/wiki/Jump_search
104
"""
5+
from __future__ import annotations
116

127
import math
13-
from collections.abc import Sequence
148
from typing import Any, Protocol, TypeVar
159

16-
1710
class Comparable(Protocol):
1811
def __lt__(self, other: Any, /) -> bool: ...
1912

20-
2113
T = TypeVar("T", bound=Comparable)
2214

23-
24-
def jump_search(arr: list, x) -> int:
15+
def jump_search(arr: list[T], item: T) -> int:
2516
"""
2617
Python implementation of the jump search algorithm.
2718
Return the index if the `item` is found, otherwise return -1.
2819
29-
Examples:
3020
>>> jump_search([0, 1, 2, 3, 4, 5], 3)
3121
3
3222
>>> jump_search([-5, -2, -1], -1)
3323
2
3424
>>> jump_search([0, 5, 10, 20], 8)
3525
-1
36-
>>> jump_search([0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610], 55)
37-
10
3826
>>> jump_search(["aa", "bb", "cc", "dd", "ee", "ff"], "ee")
3927
4
4028
"""
41-
4229
arr_size = len(arr)
43-
block_size = int(math.sqrt(arr_size))
30+
if arr_size == 0:
31+
return -1
4432

33+
block_size = int(math.sqrt(arr_size))
4534
prev = 0
4635
step = block_size
36+
37+
# Finding the block where element is present
4738
while arr[min(step, arr_size) - 1] < item:
4839
prev = step
4940
step += block_size
5041
if prev >= arr_size:
5142
return -1
5243

44+
# Linear search within the block
5345
while arr[prev] < item:
5446
prev += 1
5547
if prev == min(step, arr_size):
5648
return -1
49+
5750
if arr[prev] == item:
5851
return prev
5952
return -1
6053

61-
6254
if __name__ == "__main__":
63-
user_input = input("Enter numbers separated by a comma:\n").strip()
64-
array = [int(item) for item in user_input.split(",")]
65-
x = int(input("Enter the number to be searched:\n"))
55+
import doctest
6656

67-
res = jump_search(array, x)
68-
if res == -1:
69-
print("Number not found!")
70-
else:
71-
print(f"Number {x} is at index {res}")
57+
doctest.testmod()
58+
user_input = input("Enter numbers separated by a comma:\n").strip()
59+
if user_input:
60+
array: list[Any] = [int(i) for i in user_input.split(",")]
61+
search_item = int(input("Enter the number to be searched:\n"))
62+
res = jump_search(array, search_item)
63+
if res == -1:
64+
print("Number not found!")
65+
else:
66+
print(f"Number {search_item} is at index {res}")

0 commit comments

Comments
 (0)