Created
June 17, 2021 18:33
-
-
Save endersonmenezes/530ce989a0498798914198e255f82a19 to your computer and use it in GitHub Desktop.
Arvore Binária que Adivinha Número de 0 a 100
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Busca Binária de 0 - 100 (Número Inteiros) | |
def pesquisa_binaria(numero, lista): | |
baixo = 0 | |
alto = len(lista) - 1 | |
contador = 0 | |
while baixo <= alto: | |
contador += 1 | |
meio = round((baixo + alto) / 2) | |
chute = lista[meio] | |
print(f"Etapa: {contador} | Baixo: {baixo} | Alto: {alto} | Meio: {meio} | Chute: {chute}" ) | |
if chute == numero: | |
print(f'Você achou o número! --> {numero}') | |
return meio | |
if chute > numero: | |
alto = meio - 1 | |
else: | |
baixo = meio + 1 | |
print('Número não presente na lista!') | |
return None | |
lista = range(0, 100) | |
numero = 7 | |
pesquisa_binaria(numero, lista) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment