From 51b4d8bb8067b93cf2551543def945f9373bcc74 Mon Sep 17 00:00:00 2001 From: Mauricio-Roldan Date: Wed, 14 Sep 2022 20:53:23 -0300 Subject: [PATCH] Mauricio Roldan grupo 3 --- exercises/exercise1.py | 8 ++++++-- exercises/exercise2.py | 10 +++++++--- exercises/exercise3.py | 16 +++++++++++++--- exercises/exercise4.py | 28 ++++++++++++++++++++++++---- exercises/exercise5.py | 15 +++++++++++++-- exercises/exercise6.py | 16 +++++++++++++++- exercises/exercise7.py | 13 ++++++++++--- exercises/exercise8.py | 20 ++++++++++++++++---- exercises/exercise9.py | 17 ++++++++++++----- exercises/tempCodeRunnerFile.py | 17 +++++++++++++++++ 10 files changed, 133 insertions(+), 27 deletions(-) create mode 100644 exercises/tempCodeRunnerFile.py diff --git a/exercises/exercise1.py b/exercises/exercise1.py index c9c0e74..e00bcac 100644 --- a/exercises/exercise1.py +++ b/exercises/exercise1.py @@ -9,7 +9,10 @@ def maximo_basico(a: float, b: float) -> float: - No utilizar ELSE - No utilizar la función max """ - + if a>b: + return a + elif b>a: + return b # NO MODIFICAR - INICIO assert maximo_basico(10, 5) == 10 @@ -24,6 +27,7 @@ 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 """ + return max(a,b) # NO MODIFICAR - INICIO @@ -39,7 +43,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 assert maximo_ternario(10, 5) == 10 diff --git a/exercises/exercise2.py b/exercises/exercise2.py index 1f4e7f6..fa63af0 100644 --- a/exercises/exercise2.py +++ b/exercises/exercise2.py @@ -13,7 +13,11 @@ def maximo_encadenado(a: float, b: float, c: float) -> float: Referencia: https://docs.python.org/3/reference/expressions.html#comparisons # noqa: E501 """ - + if ac: + return b + if bc: + return a + return c # NO MODIFICAR - INICIO assert maximo_encadenado(1, 10, 5) == 10 @@ -37,7 +41,7 @@ def maximo_cuadruple(a: float, b: float, c: float, d: float) -> float: """Re-escribir para que tome 4 parámetros, utilizar la función max. Referencia: https://docs.python.org/3/library/functions.html#max""" - + return max(a,b,c,d) # NO MODIFICAR - INICIO assert maximo_cuadruple(1, 10, 5, -5) == 10 @@ -54,7 +58,7 @@ def maximo_arbitrario(*args) -> float: """Re-escribir para que tome una cantidad arbitraria de parámetros. Referencia: https://docs.python.org/3/tutorial/controlflow.html#arbitrary-argument-lists # noqa: E501 """ - + return max(args) # NO MODIFICAR - INICIO assert maximo_arbitrario(1, 10, 5, -5) == 10 diff --git a/exercises/exercise3.py b/exercises/exercise3.py index 68339cd..d228e5f 100644 --- a/exercises/exercise3.py +++ b/exercises/exercise3.py @@ -1,5 +1,6 @@ """Único return vs múltiples return.""" +from operator import truediv from typing import Union @@ -14,7 +15,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: + valorReturn=a*b + elif b==0: + valorReturn="Operación no válida" + else: + valorReturn=a/b + return valorReturn # NO MODIFICAR - INICIO assert operacion_basica(1, 1, True) == 1 @@ -40,8 +47,11 @@ def operacion_multiple(a: float, b: float, multiplicar: bool) -> Union[float, st - No utilizar ELIF ni ELSE. - 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 assert operacion_multiple(1, 1, True) == 1 assert operacion_multiple(1, 1, False) == 1 diff --git a/exercises/exercise4.py b/exercises/exercise4.py index 4a33f8b..28bddd7 100644 --- a/exercises/exercise4.py +++ b/exercises/exercise4.py @@ -1,6 +1,10 @@ """Expresiones Booleanas.""" +from operator import truediv +from re import A + + def es_vocal_if(letra: str) -> bool: """Toma un string y devuelve un booleano en base a si letra es una vocal o no. @@ -13,7 +17,19 @@ def es_vocal_if(letra: str) -> bool: Referencia: https://docs.python.org/3/library/stdtypes.html#string-methods """ - + variable=letra.lower() + if variable=="a": + return True + if variable=="e": + return True + if variable=="i": + return True + if variable=="o": + return True + if variable=="u": + return True + + return False # NO MODIFICAR - INICIO assert es_vocal_if("a") @@ -39,8 +55,10 @@ def es_vocal_if_in(letra: str) -> bool: Referencia: https://docs.python.org/3/reference/expressions.html#membership-test-operations # noqa: E501 """ - - + variable=letra.lower() + if variable in "aeiou": + return True + return False # NO MODIFICAR - INICIO assert es_vocal_if_in("a") assert not es_vocal_if_in("b") @@ -60,7 +78,9 @@ def es_vocal_in(letra: str) -> bool: - No utilizar FOR. - No utilizar listas. """ - + variable=letra.lower() + retorno=variable in "aeiou" + return retorno # NO MODIFICAR - INICIO assert es_vocal_in("a") diff --git a/exercises/exercise5.py b/exercises/exercise5.py index 1207cbb..3d9a4d3 100644 --- a/exercises/exercise5.py +++ b/exercises/exercise5.py @@ -6,7 +6,10 @@ def sumatoria_basico(n: int) -> int: Restricción: Utilizar un bucle FOR. """ - + suma=0 + for i in range(n+1): + suma=suma+i + return suma # NO MODIFICAR - INICIO assert sumatoria_basico(1) == 1 @@ -23,7 +26,8 @@ def sumatoria_sum(n: int) -> int: Restricción: No utilizar bucles (FOR, WHILE, etc) Referencia: https://docs.python.org/3/library/functions.html#sum """ - + retorno=sum(range(n+1),start=0) + return retorno # NO MODIFICAR - INICIO assert sumatoria_sum(1) == 1 @@ -47,6 +51,13 @@ def multiplicar_basico(numeros: Iterable[float]) -> float: - Utilizar múltiples Return - No utilizar ELSE """ + resultado=1 + if len(numeros)==0: + return 0 + for i in numeros: + resultado=resultado*i + + return resultado # NO MODIFICAR - INICIO diff --git a/exercises/exercise6.py b/exercises/exercise6.py index cc6c8e0..454e969 100644 --- a/exercises/exercise6.py +++ b/exercises/exercise6.py @@ -12,7 +12,15 @@ def numeros_al_final_basico(lista: List[Union[float, str]]) -> List[Union[float, - Utilizar la función type. - No utilizar índices. """ - + cadenas=[] + numeros=[] + for i in lista: + if type(i) is int: + numeros.append(i) + else: + cadenas.append(i) + cadenas.extend(numeros) + return cadenas # NO MODIFICAR - INICIO assert numeros_al_final_basico([3, "a", 1, "b", 10, "j"]) == ["a", "b", "j", 3, 1, 10] # noqa: E501 @@ -30,6 +38,12 @@ def numeros_al_final_comprension(lista: List[Union[float, str]]) -> List[Union[f - Utilizar dos comprensiones de listas. """ + lista_str=[] + lista_int=[] + lista_str=[elemento for elemento in lista if type(elemento) is str] + lista_int=[elemento for elemento in lista if type(elemento) is int] + lista_str.extend(lista_int) + return lista_str # NO MODIFICAR - INICIO assert numeros_al_final_comprension([3, "a", 1, "b", 10, "j"]) == ["a", "b", "j", 3, 1, 10] # noqa: E501 diff --git a/exercises/exercise7.py b/exercises/exercise7.py index 00534bc..58e2397 100644 --- a/exercises/exercise7.py +++ b/exercises/exercise7.py @@ -11,7 +11,11 @@ def superposicion_basico(lista_1: Iterable[Any], lista_2: Iterable[Any]) -> bool - Utilizar dos bucles FOR anidados. - Utilizar dos returns. """ - + for elemento in lista_1: + for elemento2 in lista_2: + if elemento == elemento2: + return True + return False # NO MODIFICAR - INICIO test_list = [1, "hello", 35.20] @@ -30,7 +34,10 @@ def superposicion_in(lista_1: Iterable[Any], lista_2: Iterable[Any]) -> bool: - Utilizar un único bucle FOR. - Utilizar dos returns. """ - + for elemento in lista_1: + if elemento in lista_2: + return True + return False # NO MODIFICAR - INICIO test_list = [1, "hello", 35.20] @@ -52,7 +59,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([x for x in lista_1 if x in lista_2]) # NO MODIFICAR - INICIO test_list = [1, "hello", 35.20] diff --git a/exercises/exercise8.py b/exercises/exercise8.py index ceea14b..5e35281 100644 --- a/exercises/exercise8.py +++ b/exercises/exercise8.py @@ -22,7 +22,10 @@ def combinar_basico(nombres: List[str], precios: List[float]) -> Tuple[Any]: - Utilizar la función range. - Utilizar índices. """ - + lista=[] + for indice in range(len(nombres)): + lista.append((nombres[indice],precios[indice])) + return tuple(lista) # NO MODIFICAR - INICIO respuesta = ( @@ -51,7 +54,10 @@ 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,nombre in enumerate(nombres): + lista.append((nombre,precios[indice],ids[indice])) + return tuple(lista) # NO MODIFICAR - INICIO respuesta = ( @@ -80,7 +86,10 @@ 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 """ - + lista = [] + for nombre,precio,id in zip(nombres,precios,ids): + lista.append((nombre,precio,id)) + return tuple(lista) # NO MODIFICAR - INICIO respuesta = ( @@ -112,7 +121,10 @@ def combinar_zip_args(*args) -> Tuple[Any]: Referencia: https://docs.python.org/3/tutorial/controlflow.html#unpacking-argument-lists # noqa: E501 """ - + lista=[] + for args in zip(*args): + lista.append((args)) + return tuple(lista) # NO MODIFICAR - INICIO respuesta = ( diff --git a/exercises/exercise9.py b/exercises/exercise9.py index fcfaccb..c4882e4 100644 --- a/exercises/exercise9.py +++ b/exercises/exercise9.py @@ -15,7 +15,14 @@ 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 """ - + numeros_cuadrados=[] + suma_pares = 0 + for i in numeros: + numeros_cuadrados.append(i ** 3) + for i in numeros_cuadrados: + if (i%2)==0: + suma_pares = suma_pares + i + return suma_pares # NO MODIFICAR - INICIO assert suma_cubo_pares_for([1, 2, 3, 4, 5, 6]) == 288 @@ -33,7 +40,7 @@ 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 not (x%2) != 0 ]) # NO MODIFICAR - INICIO assert suma_cubo_pares_sum_list([1, 2, 3, 4, 5, 6]) == 288 @@ -52,7 +59,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 +67,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: not (x%2) != 0, numeros_al_cubo)) """ @@ -71,7 +78,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 diff --git a/exercises/tempCodeRunnerFile.py b/exercises/tempCodeRunnerFile.py new file mode 100644 index 0000000..310cf7a --- /dev/null +++ b/exercises/tempCodeRunnerFile.py @@ -0,0 +1,17 @@ + +from typing import Any, Iterable + + +def superposicion_basico(lista_1: Iterable[Any], lista_2: Iterable[Any]) -> bool: # noqa: E501 + """Toma dos listas y devuelve un booleano en base a si tienen al menos 1 + elemento en común. + + Restricciones: + - Utilizar dos bucles FOR anidados. + - Utilizar dos returns. + """ + for elemento in lista_1: + for elemento2 in lista_2: + if elemento == elemento2: + return True + return False \ No newline at end of file