-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c81473b
commit 0ab9f82
Showing
4 changed files
with
106 additions
and
10 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
algoritmos-y-poo-python/modulo_III/clase01-busqueda-lineal.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
""" | ||
Tema: Algoritmos de busqueda y ordenamiento - Busqueda Lineal. | ||
Curso: Pensamiento Computacional, 2da entrega. | ||
Plataforma: Platzi. | ||
Profesor: David Aroesti. | ||
Alumno: @edinsonrequena. | ||
""" | ||
|
||
import random | ||
|
||
def busqueda_lineal(lista, objetivo): | ||
''' | ||
O(n) | ||
int lista = [] | ||
int objetivo | ||
match = False | ||
if objetivo in lista: | ||
match = True | ||
return match | ||
''' | ||
|
||
match = False | ||
|
||
for elemento in lista: # 0(n) | ||
if elemento == objetivo: | ||
match = True | ||
break | ||
|
||
return match | ||
|
||
|
||
def main(): | ||
|
||
tamano_lista = int(input('Tamano de la lista: ')) | ||
objetivo = int(input('Numero a encontrar: ')) | ||
|
||
lista = [random.randint(0, 100) for i in range(tamano_lista)] | ||
|
||
encontrado = busqueda_lineal(lista, objetivo) | ||
print(lista) | ||
|
||
print(f'el elemento {objetivo} {"esta" if encontrado else "no esta"} en la lista') | ||
|
||
if __name__ == '__main__': | ||
|
||
main() |
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
1 change: 1 addition & 0 deletions
1
algoritmos-y-poo-python/modulo_III/complementos/TimeComplexity.txt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Documentacion oficial de complejidad algoritmica de python: https://wiki.python.org/moin/TimeComplexity |
43 changes: 43 additions & 0 deletions
43
algoritmos-y-poo-python/modulo_III/complementos/busqueda-lineal-aporte-estudiante-platzi.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
""" | ||
Tema: Algoritmos de busqueda y ordenamiento - Busqueda Lineal. | ||
Curso: Pensamiento Computacional, 2da entrega. | ||
Plataforma: Platzi. | ||
Profesor: David Aroesti. | ||
Aporte de varios estudiantes de platzi | ||
EXPLICACIION: Es bastante más limpio el código como lo estas planteando pero la | ||
complejidad algoritmica seria la misma ya que internmente python aplica la función | ||
iter() al objeto iterable “lista” para obtener cada elemento y la compara con la | ||
variable “objetivo” para devolver un false o un true. | ||
. | ||
Eso quiere decir que aplicariamos la mismas iteraciones a lo largo del tiempo, un O(n) | ||
en Big oh notation. iteraciones definidas por el tamaño del objeto iterable y recuerda | ||
que para definir la complejidad de un algoritmo tomamos el peor de los casos es decir | ||
cuando se recorre el objeto iterable por completo. | ||
. | ||
En la documentacion de python, en la direccion que comparte @danielfernando, lo dice | ||
directamente. Las expresiones con la forma < X in S > tienen una complejidad O(n) | ||
""" | ||
|
||
import random | ||
|
||
def busqueda_lineal(lista, objetivo): return objetivo in lista # O(n) | ||
|
||
|
||
def main(): | ||
|
||
tamano_lista = int(input('Tamano de la lista: ')) | ||
objetivo = int(input('Numero a encontrar: ')) | ||
|
||
lista = [random.randint(0, 100) for i in range(tamano_lista)] | ||
|
||
encontrado = busqueda_lineal(lista, objetivo) | ||
print(lista) | ||
|
||
print(f'el elemento {objetivo} {"esta" if encontrado else "no esta"} en la lista') | ||
|
||
if __name__ == '__main__': | ||
|
||
main() |