forked from mouredev/roadmap-retos-programacion
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request mouredev#4344 from rantamhack/main
mouredev#24-python
- Loading branch information
Showing
2 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
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,90 @@ | ||
#!/bin/bash | ||
|
||
|
||
# Bash no tiene soporte para decoradores por lo que hay que simularlos | ||
# en base a funciones y variable globales | ||
|
||
|
||
echo -e "\n\n=======================================EJERCICIO=======================================\n\n" | ||
|
||
|
||
# * EJERCICIO: | ||
# * Explora el concepto de "decorador" y muestra como crearlo | ||
# * con un ejemplo generico. | ||
|
||
|
||
function decorator() { | ||
local function="$1" | ||
shift | ||
echo -e "\nEste texto se imprime antes de realizar un calculo " | ||
"$function" "$@" | ||
echo -e "Este texto se imprime despues de realizar el calculo\n" | ||
} | ||
|
||
|
||
function sum() { | ||
local num1=$1 | ||
local num2=$2 | ||
echo "El resultado de la suma es $(($1 + $2))" | ||
} | ||
|
||
function subtract() { | ||
local num1=$1 | ||
local num2=$2 | ||
echo "El resultado de la resta es $(($1 - $2))" | ||
} | ||
|
||
function pow() { | ||
local base=$1 | ||
local exponente=$2 | ||
echo -e "El resultado de elevar la base $1 a la potencia $2 es: $(($1**$2))" | ||
} | ||
|
||
|
||
|
||
decorator sum 10 5 | ||
decorator subtract 10 6 | ||
decorator pow 5 3 | ||
|
||
|
||
echo -e "\n\n=======================================DIFICULTAD EXTRA=======================================\n\n" | ||
|
||
|
||
|
||
# * DIFICULTAD EXTRA (opcional): | ||
# * Crea un decorador que sea capaz de contabilizar cuantas veces | ||
# * se ha llamado a una funcion y aplicalo a una funcion de tu eleccion. | ||
|
||
declare -A COUNTER | ||
|
||
|
||
function decorator_counter() { | ||
local function="$1" | ||
shift | ||
|
||
COUNTER["$function"]=$(( ${COUNTER["$function"]} +1 )) | ||
if [ ${COUNTER["$function"]} == 1 ]; then | ||
echo "La funcion ha sido llamada ${COUNTER["$function"]} vez" | ||
else | ||
echo "La funcion ha sido llamada ${COUNTER["$function"]} veces" | ||
fi | ||
"$function" "$@" | ||
} | ||
|
||
function multiply() { | ||
local a="$1" | ||
local b="$2" | ||
echo $(($1 * $2)) | ||
} | ||
|
||
function multiply_decorated() { | ||
decorator_counter multiply "$@" | ||
} | ||
|
||
|
||
multiply_decorated 5 3 | ||
multiply_decorated 7 2 | ||
multiply_decorated 15 17 | ||
multiply_decorated 24 42 | ||
|
||
|
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,67 @@ | ||
|
||
|
||
|
||
print("\n\n=======================================EJERCICIO=======================================\n\n") | ||
|
||
""" | ||
* EJERCICIO: | ||
* Explora el concepto de "decorador" y muestra como crearlo | ||
* con un ejemplo generico. | ||
""" | ||
|
||
def func_decorator(func_parameter): | ||
def internal_function(*args, **kwargs): | ||
print("\nEste texto se imprime antes de realizar un calculo ") | ||
func_parameter(*args, **kwargs) | ||
print("Este texto se imprime despues de realizar el calculo\n") | ||
|
||
return internal_function | ||
|
||
|
||
@func_decorator | ||
def suma(num1, num2): | ||
print(f"El resultado de la suma es {num1 + num2}") | ||
|
||
@func_decorator | ||
def resta(num1, num2): | ||
print(f"El resultado de la resta es {num1 - num2}") | ||
|
||
@func_decorator | ||
def potencia(base, exponente): | ||
print(f"El resultado de elevar {base} a la potencia {exponente} es: {pow(base, exponente)}") | ||
|
||
suma(10, 5) | ||
resta(10, 5) | ||
potencia(base=5, exponente=3) | ||
|
||
|
||
print("\n\n=======================================DIFICULTAD EXTRA=======================================\n\n") | ||
|
||
|
||
""" | ||
* DIFICULTAD EXTRA (opcional): | ||
* Crea un decorador que sea capaz de contabilizar cuantas veces | ||
* se ha llamado a una funcion y aplicalo a una funcion de tu eleccion. | ||
""" | ||
|
||
def decorator_counter(func_parameter): | ||
def func_counter(*args): | ||
func_counter.counter += 1 | ||
if func_counter.counter == 1: | ||
print(f"La funcion ha sido llamada {func_counter.counter} vez") | ||
else: | ||
print(f"La funcion ha sido llamada {func_counter.counter} veces") | ||
return func_parameter(*args) | ||
|
||
func_counter.counter = 0 | ||
return func_counter | ||
|
||
@decorator_counter | ||
def multiply(a, b): | ||
print(a * b) | ||
|
||
multiply(5, 3) | ||
multiply(7, 2) | ||
multiply(15, 17) | ||
multiply(24, 31) | ||
|