Skip to content

Commit

Permalink
Merge pull request #5127 from gmatles/main
Browse files Browse the repository at this point in the history
Reto #38 - python
  • Loading branch information
kontroldev authored Sep 27, 2023
2 parents 004c453 + e601e84 commit d74fafd
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions Retos/Reto #38 - LAS SUMAS [Media]/python/gmatles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from pprint import pprint


def sumas_recursiva(lista: list, objetivo: int) -> list:
lista = list(filter(lambda x: x <= objetivo, lista))

def auxiliar(lista: list, res: list, objetivo: int):
for e in res.copy():
aux = e.copy()
aux.append(lista[0])
if sum(aux) <= objetivo:
res.append(aux)
res.append([lista[0]])
if len(lista) != 1:
auxiliar(lista[1:], res, objetivo)

resultado = list()
auxiliar(lista, resultado, objetivo)
return list(filter(lambda x: sum(x) == objetivo, resultado))


pprint(sumas_recursiva([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 5))
pprint(sumas_recursiva([1, 5, 3, 2], 6))


def sumas_iterativa(lista: list, objetivo: int) -> list:
res = list()
lista = list(filter(lambda x: x <= objetivo, lista))
for n in lista:
for e in res.copy():
aux = e.copy()
aux.append(n)
if sum(aux) <= objetivo:
res.append(aux)
res.append([n])
return list(filter(lambda x: sum(x) == objetivo, res))


pprint(sumas_iterativa([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 5))
pprint(sumas_iterativa([1, 5, 3, 2], 6))

0 comments on commit d74fafd

Please sign in to comment.