Search This Blog

Thursday, July 15, 2021

Métodos Computacionais 2021: Aula 8 - Exercícios resolvidos e Outros

 Exercícios resolvidos:

Questão 1 

Questão 2

Questão 3 

Questão 4 

Questão 4


Exercícios de programação funcional do GitHub: https://gist.github.com/oskarkv/3168ea3f8d7530ccd94c97c19aafe266

Crie uma partição de função que receba os argumentos n, step e seq (número, número, sequência). Ele deve pegar n elementos de seq, agrupar isso em uma lista e avançar nas etapas de conforme o valor de step, depois pegar outros n elementos e assim por diante

Pede-se que seja criada uma função chamada "update" que recebe um mapa "m", uma chave "k", uma função "f" e um número arbitrário de argumentos adicionais "args". Ele especifica que a função deve retornar um novo mapa que seja semelhante a "m", mas com o valor para a chave "k" substituído pelo resultado da aplicação de "f" ao valor anterior e aos argumentos fornecidos.

Crie uma função zipwith que receba uma função f e um número arbitrário de sequências e retorne uma lista de f aplicada aos primeiros elementos das sequências fornecidas, seguida por f aplicada aos segundos elementos das sequências e assim por diante.

Crie uma função zip que pegue um número arbitrário de sequências e as compacte, ou seja, crie uma lista de listas, onde as listas internas consistem nos primeiros elementos das sequências fornecidas, depois nos segundos elementos das sequências fornecidas e assim por diante.

Crie uma função zipmap que receba duas sequências e forme um dicionário dos elementos da primeira sequência com os elementos da segunda sequência.

O objetivo desse exercício é criar uma função flatten que possa achatar uma árvore 

Crie uma função 'take' que receba um número 'n' e uma sequência 'seq' e retorne uma lista dos primeiros 'n' elementos de 'seq'.

Crie uma função "interleave" que pegue um número arbitrário de sequências e as intercale.

Crie uma função compose que receba 2 funções e faça a composição da função, ou seja, compose(double, negate) deve retornar uma função que primeiro chama negate e depois double em seu argumento.

Nesse exercício devemos criar uma função chamada "balanced" que recebe uma string "s" e retorna True se ela contiver apenas parênteses, colchetes e chaves balanceados ((), [], {}), caso contrário retorna False.

Create a function clist (for create list) that takes an arbitrary number of arguments and creates a list of the arguments given. clist is useful for giving as an argument to other functions. If you use a Lisp, it already exists in the form of list, and you can skip this exercise.

Create a function drop that takes a number n and a sequence seq, and returns a list with the first n elements of seq dropped.

Create a function frequencies that takes a sequence and counts how many times the elements appear in the sequence, returns a map.

Create a function partition that takes the arguments n, step, and seq (number, number, seqence). It should take n elements from seq, wrap that in a list, and step forward in seq step steps, then take another n elements, and so on.

Create a function every_pred that takes an arbitrary number of predicates (functions that return a truth value) and returns a function that returns true if and only if all predicates were truthy for the argument.

Create two functions postwalk and prewalk, that both take a function f and a tree t. (A tree in this case is a list where the elements can be trees themselves), e.g. [1, [2, 3, [1, 2]], [4, 5, 6]]. In this case 1, [2, 3, [1, 2] and 6 are all elements of the tree (but not the only ones).) The functions should apply f to each element in the tree to create a new tree with the results, like map but for trees instead of lists. The difference between prewalk and postwalk is that prewalk does the replacement on the way “down” and postwalk on the way “up”, so that prewalk can continue down into the new value that was “just” created.