[GO] Recherche binaire en Python

C'est le premier message de Qiita.

Cette fois, j'ai implémenté la recherche binaire avec deux modèles, boucle et récursif.

Recherche binaire en boucle

binary_search_loop.py


from typing import List


def binary_search_loop(data: List[int], target: int) -> int:
    left, right = 0, len(data)

    while left <= right:
        mid = (left + right) // 2
        if data[mid] == target:
            return mid
        elif data[mid] < target:
            left = mid + 1
        else:
            right = mid - 1

    return None


if __name__ == "__main__":
    data: List[int] = [int(x) for x in input("Tableau trié(Espace délimité)Entrer: ").split()] #J'ai utilisé la notation d'inclusion
    target = int(input("Entrez la cible: "))

    result = binary_search_loop(data, target)
    if result is not None:
        print(f"found: {result}Était deuxième")
    else:
        print("not found")

Recherche binaire récursive

binary_search_recursion.py


from typing import List


def binary_search_recursion(data: List[int], target: int) -> int:
    def recursion(left=0, right=len(data) - 1):
        if left > right:
            return None

        mid = (left + right) // 2
        if data[mid] == target:
            return mid
        elif data[mid] < target:
            return recursion(mid + 1, right)
        else:
            return recursion(left, mid - 1)

    return recursion()


if __name__ == "__main__":
    data: List[int] = list(map(int, input("Tableau trié(Espace délimité)Entrer: ").split())) #Utilisation de la carte
    target = int(input("Entrez la cible: "))

    result = binary_search_recursion(data, target)
    if result is not None:
        print(f"found: {result}Était deuxième")
    else:
        print("not found")

Ce que j'ai remarqué

Recommended Posts

Dichotomie avec Python
Recherche binaire en Python
Recherche binaire en Python / C ++
Recherche de bisection (python2.7) mémo
[Python] Recherche de bisection ABC155D
Recherche linéaire en Python
Dichotomie avec python
Dichotomie avec Python 3
Algorithme en Python (ABC 146 C Dichotomy
Algorithme en Python (recherche de priorité de largeur, bfs)
Enregistrez le fichier binaire en Python
Créer un fichier binaire en Python
Algorithme en Python (recherche de priorité en profondeur, dfs)
Écrire une recherche de priorité en profondeur en Python
Recherche de priorité de profondeur à l'aide de la pile en Python
Python en optimisation
CURL en Python
Métaprogrammation avec Python
Python 3.3 avec Anaconda
Géocodage en python
Méta-analyse en Python
Unittest en Python
Époque en Python
Discord en Python
Allemand en Python
DCI en Python
visualiser la recherche binaire
tri rapide en python
nCr en python
N-Gram en Python
Programmation avec Python
Plink en Python
Constante en Python
ABC146C (dichotomie)
FizzBuzz en Python
Étape AIC en Python
CSV en Python
Assemblage inversé avec Python
Réflexion en Python
Constante en Python
nCr en Python.
format en python
Scons en Python 3
Puyopuyo en python
python dans virtualenv
PPAP en Python
Quad-tree en Python
Réflexion en Python
Chimie avec Python
Hashable en Python
DirectLiNGAM en Python
LiNGAM en Python
Aplatir en Python
Aplatir en python
Essayez de travailler avec des données binaires en Python
Rechercher et lire des vidéos YouTube avec Python
À la recherche du FizzBuzz le plus rapide en Python