diff --git a/exercises/exercise1.py b/exercises/exercise1.py index c9c0e74..96f6eba 100644 --- a/exercises/exercise1.py +++ b/exercises/exercise1.py @@ -1,6 +1,10 @@ """Bloque IF, operadores lógicos, función max y operador ternario.""" +from ast import arg +from re import A + + def maximo_basico(a: float, b: float) -> float: """Toma dos números y devuelve el mayor. @@ -9,6 +13,10 @@ def maximo_basico(a: float, b: float) -> float: - No utilizar ELSE - No utilizar la función max """ + if a > b: + return a + if b > a: + return b # NO MODIFICAR - INICIO @@ -24,6 +32,8 @@ def maximo_libreria(a: float, b: float) -> float: """Re-escribir utilizando el built-in max. Referencia: https://docs.python.org/3/library/functions.html#max """ + resultado = max(a, b) + return resultado # NO MODIFICAR - INICIO @@ -39,6 +49,7 @@ def maximo_ternario(a: float, b: float) -> float: """Re-escribir utilizando el operador ternario. Referencia: https://docs.python.org/3/reference/expressions.html#conditional-expressions # noqa: E501 """ + return a if a > b else b # NO MODIFICAR - INICIO diff --git a/exercises/exercise2.py b/exercises/exercise2.py index 1f4e7f6..01641dd 100644 --- a/exercises/exercise2.py +++ b/exercises/exercise2.py @@ -2,6 +2,10 @@ """ +from ast import arg +from this import d + + def maximo_encadenado(a: float, b: float, c: float) -> float: """Toma 3 números y devuelve el máximo. @@ -14,6 +18,12 @@ def maximo_encadenado(a: float, b: float, c: float) -> float: Referencia: https://docs.python.org/3/reference/expressions.html#comparisons # noqa: E501 """ + if b < a > c: + return a + if a < b > c: + return b + return c + # NO MODIFICAR - INICIO assert maximo_encadenado(1, 10, 5) == 10 @@ -38,6 +48,9 @@ def maximo_cuadruple(a: float, b: float, c: float, d: float) -> float: Referencia: https://docs.python.org/3/library/functions.html#max""" + resultado = max(a, b, c, d) + return resultado + # NO MODIFICAR - INICIO assert maximo_cuadruple(1, 10, 5, -5) == 10 @@ -55,6 +68,9 @@ def maximo_arbitrario(*args) -> float: Referencia: https://docs.python.org/3/tutorial/controlflow.html#arbitrary-argument-lists # noqa: E501 """ + resultado = max(*args) + return resultado + # NO MODIFICAR - INICIO assert maximo_arbitrario(1, 10, 5, -5) == 10 diff --git a/exercises/exercise3.py b/exercises/exercise3.py index 68339cd..851fbdf 100644 --- a/exercises/exercise3.py +++ b/exercises/exercise3.py @@ -14,6 +14,13 @@ def operacion_basica(a: float, b: float, multiplicar: bool) -> Union[float, str] - Utilizar IF con ELIF con ELSE. - No utilizar AND ni OR. """ + if multiplicar == True: + resultado = a*b + elif b == 0: + resultado = "Operación no válida" + else: + resultado = a/b + return resultado # NO MODIFICAR - INICIO @@ -38,8 +45,12 @@ def operacion_multiple(a: float, b: float, multiplicar: bool) -> Union[float, st - Utilizar 2 IF. - No Utilizar IF anidados. - No utilizar ELIF ni ELSE. - - No utilizar AND ni OR. - """ + - No utilizar AND ni OR.""" + if multiplicar == True: + return a*b + if b == 0: + return "Operación no válida" + return a/b # NO MODIFICAR - INICIO diff --git a/exercises/exercise4.py b/exercises/exercise4.py index 4a33f8b..f34cfcd 100644 --- a/exercises/exercise4.py +++ b/exercises/exercise4.py @@ -13,6 +13,18 @@ def es_vocal_if(letra: str) -> bool: Referencia: https://docs.python.org/3/library/stdtypes.html#string-methods """ + letra = letra.lower() + if letra == "a": + return True + if letra == "e": + return True + if letra == "i": + return True + if letra == "o": + return True + if letra == "u": + return True + return False # NO MODIFICAR - INICIO @@ -40,6 +52,10 @@ def es_vocal_if_in(letra: str) -> bool: Referencia: https://docs.python.org/3/reference/expressions.html#membership-test-operations # noqa: E501 """ + if letra == letra.lower() in "a" "e" "i" "o" "u": + return True + return False + # NO MODIFICAR - INICIO assert es_vocal_if_in("a") @@ -61,8 +77,9 @@ def es_vocal_in(letra: str) -> bool: - No utilizar listas. """ + return letra == letra.lower() in "a" "e" "i" "o" "u" -# NO MODIFICAR - INICIO + # NO MODIFICAR - INICIO assert es_vocal_in("a") assert not es_vocal_in("b") assert es_vocal_in("A") diff --git a/exercises/exercise5.py b/exercises/exercise5.py index 1207cbb..03809c4 100644 --- a/exercises/exercise5.py +++ b/exercises/exercise5.py @@ -6,9 +6,13 @@ def sumatoria_basico(n: int) -> int: Restricción: Utilizar un bucle FOR. """ + sumatoria = 0 + for i in range(1, n+1): + sumatoria += i + return sumatoria -# NO MODIFICAR - INICIO + # NO MODIFICAR - INICIO assert sumatoria_basico(1) == 1 assert sumatoria_basico(100) == 5050 # NO MODIFICAR - FIN @@ -23,6 +27,9 @@ def sumatoria_sum(n: int) -> int: Restricción: No utilizar bucles (FOR, WHILE, etc) Referencia: https://docs.python.org/3/library/functions.html#sum """ + # list(range(n+1)) + suma = sum(range(n+1)) + return suma # NO MODIFICAR - INICIO @@ -47,6 +54,14 @@ def multiplicar_basico(numeros: Iterable[float]) -> float: - Utilizar múltiples Return - No utilizar ELSE """ + if len(numeros) == 0: + return 0 + + productos = float(1) + + for i in numeros: + productos = productos * i + return productos # NO MODIFICAR - INICIO diff --git a/exercises/exercise6.py b/exercises/exercise6.py index cc6c8e0..1d6d7a9 100644 --- a/exercises/exercise6.py +++ b/exercises/exercise6.py @@ -12,6 +12,16 @@ def numeros_al_final_basico(lista: List[Union[float, str]]) -> List[Union[float, - Utilizar la función type. - No utilizar índices. """ + listaEnteros = [] + listaStrings = [] + + for i in lista: + if type(i) == int: + listaEnteros.append(i) + else: + listaStrings.append(i) + listaStrings.extend(listaEnteros) + return listaStrings # NO MODIFICAR - INICIO @@ -29,6 +39,11 @@ def numeros_al_final_comprension(lista: List[Union[float, str]]) -> List[Union[f - No utilizar bucles. - Utilizar dos comprensiones de listas. """ + numbers = [n for n in lista if type(n) is int] + strings = [s for s in lista if type(s) is str] + + strings.extend(numbers) + return strings # NO MODIFICAR - INICIO diff --git a/exercises/exercise7.py b/exercises/exercise7.py index 00534bc..8988f4e 100644 --- a/exercises/exercise7.py +++ b/exercises/exercise7.py @@ -1,5 +1,6 @@ """Any y Sets.""" +from this import s from typing import Any, Iterable @@ -11,6 +12,11 @@ def superposicion_basico(lista_1: Iterable[Any], lista_2: Iterable[Any]) -> bool - Utilizar dos bucles FOR anidados. - Utilizar dos returns. """ + for i in lista_1: + for j in lista_2: + if (i == j): + return True + return False # NO MODIFICAR - INICIO @@ -30,6 +36,10 @@ def superposicion_in(lista_1: Iterable[Any], lista_2: Iterable[Any]) -> bool: - Utilizar un único bucle FOR. - Utilizar dos returns. """ + for i in lista_1: + if i in lista_2: + return True + return False # NO MODIFICAR - INICIO @@ -52,6 +62,7 @@ def superposicion_any(lista_1: Iterable[Any], lista_2: Iterable[Any]) -> bool: Referencia: https://docs.python.org/3/library/functions.html#any """ + return any([n for n in lista_1 if n in lista_2]) # NO MODIFICAR - INICIO @@ -73,6 +84,7 @@ def superposicion_set(lista_1: Iterable[Any], lista_2: Iterable[Any]) -> bool: Referencia: https://docs.python.org/3/library/stdtypes.html#set-types-set-frozenset # noqa: E501 """ + return not set(lista_1).isdisjoint(set(lista_2)) # NO MODIFICAR - INICIO diff --git a/exercises/exercise8.py b/exercises/exercise8.py index ceea14b..e878e75 100644 --- a/exercises/exercise8.py +++ b/exercises/exercise8.py @@ -22,6 +22,17 @@ def combinar_basico(nombres: List[str], precios: List[float]) -> Tuple[Any]: - Utilizar la función range. - Utilizar índices. """ + """ + for indice, (nombres, precios) in zip(nombres, precios): + respuesta = ( + print(f"({nombres}, {precios})") + ) + """ + + lista = [] + for indice in range(len(nombres)): + lista.append((nombres[indice], precios[indice])) + return tuple(lista) # NO MODIFICAR - INICIO @@ -52,6 +63,12 @@ def combinar_enumerate(nombres: List[str], precios: List[float], ids: List[int]) Referencia: https://docs.python.org/3/library/functions.html#enumerate """ + lista = [] + + for indice, producto in enumerate(nombres): + lista.append((producto, precios[indice], ids[indice])) + return tuple(lista) + # NO MODIFICAR - INICIO respuesta = ( @@ -80,6 +97,12 @@ def combinar_zip(nombres: List[str], precios: List[float], ids: List[int]) -> Tu - No utilizar índices. Referencia: https://docs.python.org/3/library/functions.html#zip """ + listado_final = [] + for (nombres, precios, ids) in zip(nombres, precios, ids): + listado_parcial = (nombres, precios, ids) + print(listado_parcial) + listado_final.append(listado_parcial) + return tuple(listado_final) # NO MODIFICAR - INICIO @@ -112,6 +135,11 @@ def combinar_zip_args(*args) -> Tuple[Any]: Referencia: https://docs.python.org/3/tutorial/controlflow.html#unpacking-argument-lists # noqa: E501 """ + lista_final = [] + for componentes in zip(*args): + lista_parcial = (componentes) + lista_final.append(lista_parcial) + return tuple(lista_final) # NO MODIFICAR - INICIO diff --git a/exercises/exercise9.py b/exercises/exercise9.py index fcfaccb..2607d24 100644 --- a/exercises/exercise9.py +++ b/exercises/exercise9.py @@ -15,7 +15,17 @@ def suma_cubo_pares_for(numeros: Iterable[int]) -> int: - https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions # noqa: E501 - https://docs.python.org/3/library/functions.html#sum """ - + cubos=[] + for x in numeros: + y= x**3 + cubos.append(y) + + suma=0 + for y in cubos: + if (y % 2 == 0): + suma = suma+y + + return suma # NO MODIFICAR - INICIO assert suma_cubo_pares_for([1, 2, 3, 4, 5, 6]) == 288 @@ -33,7 +43,8 @@ def suma_cubo_pares_sum_list(numeros: Iterable[int]) -> int: - https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions # noqa: E501 - https://docs.python.org/3/library/functions.html#sum """ - + + return sum(x**3 for x in numeros if (x**3)%2 == 0) # NO MODIFICAR - INICIO assert suma_cubo_pares_sum_list([1, 2, 3, 4, 5, 6]) == 288 @@ -52,7 +63,7 @@ def suma_cubo_pares_sum_list(numeros: Iterable[int]) -> int: Restricción: Utilizar List, map y lambda y la variable numeros """ -numeros_al_cubo = # Completar +numeros_al_cubo = list(map(lambda x: x**3, numeros)) """ Escribir una función lambda que permita filtrar todos los elementos pares @@ -60,7 +71,7 @@ def suma_cubo_pares_sum_list(numeros: Iterable[int]) -> int: Restricción: Utilizar List, filter, lambda y la variable numeros_al_cubo """ -numeros_al_cubo_pares = # Completar +numeros_al_cubo_pares = list(filter(lambda x: x%2==0, numeros_al_cubo)) """ @@ -71,7 +82,7 @@ def suma_cubo_pares_sum_list(numeros: Iterable[int]) -> int: from functools import reduce # noqa: E402 -suma_numeros_al_cubo_pares = # Completar +suma_numeros_al_cubo_pares = reduce(lambda x,y: x+y, numeros_al_cubo_pares) # NO MODIFICAR - INICIO