Skip to content
Closed

Patch 4 #14367

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
2 changes: 1 addition & 1 deletion data_structures/hashing/hash_table_with_linked_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

def _set_value(self, key, data):
self.values[key] = deque([]) if self.values[key] is None else self.values[key]
self.values[key] = deque() if self.values[key] is None else self.values[key]
self.values[key].appendleft(data)
self._keys[key] = self.values[key]

Expand Down
2 changes: 1 addition & 1 deletion machine_learning/linear_discriminant_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def accuracy(actual_y: list, predicted_y: list) -> float:
num = TypeVar("num")


def valid_input(
def valid_input[num](
input_type: Callable[[object], num], # Usually float or int
input_msg: str,
err_msg: str,
Expand Down
40 changes: 40 additions & 0 deletions maths/sign.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""Sign Function."""


def sign(num: float) -> int:
"""
Return the sign of a number: -1 for negative, 0 for zero, 1 for positive.

>>> sign(-5)
-1
>>> sign(0)
0
>>> sign(10)
1
>>> sign(-0.5)
-1
"""
if num > 0:
return 1
elif num < 0:
return -1
return 0


def test_sign() -> None:
"""
>>> test_sign()
"""
assert sign(-5) == -1
assert sign(0) == 0
assert sign(10) == 1
assert sign(-0.001) == -1
assert sign(0.001) == 1


if __name__ == "__main__":
import doctest

doctest.testmod()
test_sign()
print(sign(-5)) # --> -1
5 changes: 1 addition & 4 deletions searches/jump_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ class Comparable(Protocol):
def __lt__(self, other: Any, /) -> bool: ...


T = TypeVar("T", bound=Comparable)


def jump_search(arr: Sequence[T], item: T) -> int:
def jump_search[T: Comparable](arr: Sequence[T], item: T) -> int:
"""
Python implementation of the jump search algorithm.
Return the index if the `item` is found, otherwise return -1.
Expand Down