Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 32 additions & 9 deletions src/ejercicios.js
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)
Copy link
Collaborator

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

const newCache = [...cache]
newCache[n] = fibo(n - 1, cache) + fibo(n-2, cache)

Copy link
Author

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);

Copy link
Author

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

const fibo = function() {
    let cache = [];
    return function fib(n) {
    if (cache[n]) {
        return cache[n]
    } else if (n < 2) {
        return n
    } else {
        cache[n] = fib(n - 1) + fib(n - 2)
    }
    return cache[n];
  }
}()

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.

}
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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

está bien, pero fijate que estás repitiendo el código de atributo y multiplicacion. podés directamente usar esas funciones

});

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)))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lo mismo acá, podés usar ordenarPor y atributo

return firstElement.nombre
};