-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ejercicios resueltos #1
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,44 @@ | ||
import fp from 'lodash/fp'; | ||
|
||
export const fibo = '???'; | ||
|
||
export const factorial = '???'; | ||
|
||
export const multiplicacion = '???'; | ||
export const fibo = (n, cache = []) => { | ||
if (cache[n]) { | ||
return cache[n] | ||
} else if (n < 2) { | ||
return n | ||
} else { | ||
cache[n] = fibo(n - 1, cache) + fibo(n-2, cache) | ||
} | ||
return cache[n] | ||
}; | ||
|
||
export const factorial = n => { | ||
if (n < 1) { | ||
return 1 | ||
} | ||
return n * factorial(n - 1); | ||
}; | ||
|
||
export const multiplicacion = arr => { | ||
return arr.reduce((acc, num) => acc * num); | ||
}; | ||
|
||
// Funciones de lodash/fp que les pueden ser útiles a partir de este punto: | ||
// Las vistas en la clase (particularmente fp.flow y fp.curry) | ||
// fp.sortBy (para ordenar un array) | ||
// fp.reverse (para dar vuelta un array) | ||
// fp.first (para obtener el primer valor de un array) | ||
|
||
export const atributo = '???'; | ||
export const atributo = prop => obj => obj[prop]; | ||
|
||
export const multiplicarAtributo = '???'; | ||
export const multiplicarAtributo = fp.curry((attr, obj) => { | ||
return obj[attr].reduce((acc, n) => acc * n); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. está bien, pero fijate que estás repitiendo el código de |
||
}); | ||
|
||
export const ordenarPor = '???'; | ||
export const ordenarPor = fp.curry((attr, arr) => { | ||
return fp.reverse(fp.sortBy(element => element[attr], arr)) | ||
}); | ||
|
||
export const mayorPersona = '???'; | ||
export const mayorPersona = arr => { | ||
const firstElement = fp.first(fp.reverse(fp.sortBy(element => element['edad'], arr))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lo mismo acá, podés usar |
||
return firstElement.nombre | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ojo con mutar cache acá, una forma más correcta sería
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
El tema es que si lo implemento así nunca finaliza el ultimo caso de los tests:
// si la función no termina, están calculando el resultado de una manera muy poco eficiente. expect(fns.fibo(100)).toEqual(354224848179262000000);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jabsatz Ahi le encontre una vuelta
Con el () llamo a la funcion fibo inmediamente, eso devuelve la funcion que regresa y cuando lo llamas en el test le pasas el numero directamente como parametro, y esa funcion que devuelve es la que es llamada en cada recursion.