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#4376 from Chrisdev00/Chrisdev00-branch
mouredev#24 - python y javascript
- Loading branch information
Showing
2 changed files
with
113 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,54 @@ | ||
/* | ||
* EJERCICIO: | ||
* Explora el concepto de "decorador" y muestra cómo crearlo | ||
* con un ejemplo genérico. | ||
* | ||
* DIFICULTAD EXTRA (opcional): | ||
* Crea un decorador que sea capaz de contabilizar cuántas veces | ||
* se ha llamado a una función y aplícalo a una función de tu elección. | ||
*/ | ||
|
||
var Car = function() { | ||
var car = {ruedas: 4}; | ||
pintar('red')(car); | ||
return car | ||
}; | ||
|
||
function pintar (c){ | ||
return function(car){ | ||
car.color= c; | ||
}; | ||
} | ||
|
||
|
||
var myCar = Car() | ||
console.log(myCar) | ||
|
||
|
||
|
||
/////////// ------------------------------ EXTRA ---------------------------------------- ///////////////// | ||
|
||
function contador(func) { | ||
function funcionModificada(...args) { | ||
funcionModificada.llamadas += 1; | ||
console.log(`La función ${func.name} ha sido llamada ${funcionModificada.llamadas} veces`); | ||
return func(...args); | ||
} | ||
funcionModificada.llamadas = 0; | ||
return funcionModificada; | ||
} | ||
|
||
function factorial(num) { | ||
let res = 1; | ||
for (let i = 1; i <= num; i++) { | ||
res *= i; | ||
} | ||
return res; | ||
} | ||
|
||
// Decorar la función factorial | ||
const factorialConContador = contador(factorial); | ||
|
||
console.log(factorialConContador(5)); | ||
console.log(factorialConContador(4)); | ||
console.log(factorialConContador(3)); |
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,59 @@ | ||
""" | ||
* EJERCICIO: | ||
* Explora el concepto de "decorador" y muestra cómo crearlo | ||
* con un ejemplo genérico. | ||
* | ||
* DIFICULTAD EXTRA (opcional): | ||
* Crea un decorador que sea capaz de contabilizar cuántas veces | ||
* se ha llamado a una función y aplícalo a una función de tu elección. | ||
""" | ||
|
||
import time | ||
|
||
def calcularTiempo(fun): | ||
|
||
def funcionModificada(*args, **kwargs): | ||
inicio = time.time() | ||
resultado = fun(*args, **kwargs) | ||
final = time.time() | ||
print(f"El tiempo de ejecucion es: {final - inicio} segundos") | ||
return resultado | ||
|
||
return funcionModificada | ||
|
||
@calcularTiempo | ||
def sum_numeros(a, b): | ||
return a + b | ||
|
||
@calcularTiempo | ||
def imprimirNumeros(num): | ||
for i in range(num): | ||
print(i) | ||
|
||
print(sum_numeros(16556301655659919911, 896474544)) | ||
imprimirNumeros(8) | ||
|
||
|
||
########### --------------------------------- EXTRA ---------------------------------- ################## | ||
|
||
def contador(fun): | ||
|
||
def funcion_modificada(*args, **kwargs): | ||
funcion_modificada.llamadas += 1 | ||
print(f"La funcion {fun.__name__} ha sido llamada {funcion_modificada.llamadas} veces") | ||
return fun(*args, **kwargs) | ||
|
||
funcion_modificada.llamadas = 0 | ||
return funcion_modificada | ||
|
||
@contador | ||
def factorial(num): | ||
res = 1 | ||
for i in range(1, num+1): | ||
res = res * i | ||
return res | ||
|
||
|
||
print(factorial(5)) | ||
print(factorial(4)) | ||
print(factorial(3)) |