mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-10 14:23:43 +08:00
Pyupgrade to Python 3.9 (#4718)
* Pyupgrade to Python 3.9 * updating DIRECTORY.md Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
@ -9,12 +9,13 @@ python3 -m doctest -v binary_search.py
|
||||
For manual testing run:
|
||||
python3 binary_search.py
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import bisect
|
||||
from typing import List, Optional
|
||||
|
||||
|
||||
def bisect_left(
|
||||
sorted_collection: List[int], item: int, lo: int = 0, hi: int = -1
|
||||
sorted_collection: list[int], item: int, lo: int = 0, hi: int = -1
|
||||
) -> int:
|
||||
"""
|
||||
Locates the first element in a sorted array that is larger or equal to a given
|
||||
@ -60,7 +61,7 @@ def bisect_left(
|
||||
|
||||
|
||||
def bisect_right(
|
||||
sorted_collection: List[int], item: int, lo: int = 0, hi: int = -1
|
||||
sorted_collection: list[int], item: int, lo: int = 0, hi: int = -1
|
||||
) -> int:
|
||||
"""
|
||||
Locates the first element in a sorted array that is larger than a given value.
|
||||
@ -105,7 +106,7 @@ def bisect_right(
|
||||
|
||||
|
||||
def insort_left(
|
||||
sorted_collection: List[int], item: int, lo: int = 0, hi: int = -1
|
||||
sorted_collection: list[int], item: int, lo: int = 0, hi: int = -1
|
||||
) -> None:
|
||||
"""
|
||||
Inserts a given value into a sorted array before other values with the same value.
|
||||
@ -148,7 +149,7 @@ def insort_left(
|
||||
|
||||
|
||||
def insort_right(
|
||||
sorted_collection: List[int], item: int, lo: int = 0, hi: int = -1
|
||||
sorted_collection: list[int], item: int, lo: int = 0, hi: int = -1
|
||||
) -> None:
|
||||
"""
|
||||
Inserts a given value into a sorted array after other values with the same value.
|
||||
@ -190,7 +191,7 @@ def insort_right(
|
||||
sorted_collection.insert(bisect_right(sorted_collection, item, lo, hi), item)
|
||||
|
||||
|
||||
def binary_search(sorted_collection: List[int], item: int) -> Optional[int]:
|
||||
def binary_search(sorted_collection: list[int], item: int) -> int | None:
|
||||
"""Pure implementation of binary search algorithm in Python
|
||||
|
||||
Be careful collection must be ascending sorted, otherwise result will be
|
||||
@ -228,7 +229,7 @@ def binary_search(sorted_collection: List[int], item: int) -> Optional[int]:
|
||||
return None
|
||||
|
||||
|
||||
def binary_search_std_lib(sorted_collection: List[int], item: int) -> Optional[int]:
|
||||
def binary_search_std_lib(sorted_collection: list[int], item: int) -> int | None:
|
||||
"""Pure implementation of binary search algorithm in Python using stdlib
|
||||
|
||||
Be careful collection must be ascending sorted, otherwise result will be
|
||||
@ -258,8 +259,8 @@ def binary_search_std_lib(sorted_collection: List[int], item: int) -> Optional[i
|
||||
|
||||
|
||||
def binary_search_by_recursion(
|
||||
sorted_collection: List[int], item: int, left: int, right: int
|
||||
) -> Optional[int]:
|
||||
sorted_collection: list[int], item: int, left: int, right: int
|
||||
) -> int | None:
|
||||
|
||||
"""Pure implementation of binary search algorithm in Python by recursion
|
||||
|
||||
|
@ -13,7 +13,7 @@ python3 fibonacci_search.py
|
||||
from functools import lru_cache
|
||||
|
||||
|
||||
@lru_cache()
|
||||
@lru_cache
|
||||
def fibonacci(k: int) -> int:
|
||||
"""Finds fibonacci number in index k.
|
||||
|
||||
|
@ -6,7 +6,7 @@ This is a type of divide and conquer algorithm which divides the search space in
|
||||
Time Complexity : O(log3 N)
|
||||
Space Complexity : O(1)
|
||||
"""
|
||||
from typing import List
|
||||
from __future__ import annotations
|
||||
|
||||
# This is the precision for this function which can be altered.
|
||||
# It is recommended for users to keep this number greater than or equal to 10.
|
||||
@ -16,7 +16,7 @@ precision = 10
|
||||
# This is the linear search that will occur after the search space has become smaller.
|
||||
|
||||
|
||||
def lin_search(left: int, right: int, array: List[int], target: int) -> int:
|
||||
def lin_search(left: int, right: int, array: list[int], target: int) -> int:
|
||||
"""Perform linear search in list. Returns -1 if element is not found.
|
||||
|
||||
Parameters
|
||||
@ -58,7 +58,7 @@ def lin_search(left: int, right: int, array: List[int], target: int) -> int:
|
||||
return -1
|
||||
|
||||
|
||||
def ite_ternary_search(array: List[int], target: int) -> int:
|
||||
def ite_ternary_search(array: list[int], target: int) -> int:
|
||||
"""Iterative method of the ternary search algorithm.
|
||||
>>> test_list = [0, 1, 2, 8, 13, 17, 19, 32, 42]
|
||||
>>> ite_ternary_search(test_list, 3)
|
||||
@ -110,7 +110,7 @@ def ite_ternary_search(array: List[int], target: int) -> int:
|
||||
return -1
|
||||
|
||||
|
||||
def rec_ternary_search(left: int, right: int, array: List[int], target: int) -> int:
|
||||
def rec_ternary_search(left: int, right: int, array: list[int], target: int) -> int:
|
||||
"""Recursive method of the ternary search algorithm.
|
||||
|
||||
>>> test_list = [0, 1, 2, 8, 13, 17, 19, 32, 42]
|
||||
|
Reference in New Issue
Block a user