Jun-20-2019, 08:15 AM
Hi,
my homework is to create a program which sums elements of the list (from current index to the last) and inserts its result to the appropriate cell on the new list.
I have to do it in both ways, 1 - iteration, 2 - recursive.
Now the iteration version works flawlessly, but recursive takes the cut of already cut list. How to prevent it to make the recursive function the cut of original list in every step?
my homework is to create a program which sums elements of the list (from current index to the last) and inserts its result to the appropriate cell on the new list.
I have to do it in both ways, 1 - iteration, 2 - recursive.
Now the iteration version works flawlessly, but recursive takes the cut of already cut list. How to prevent it to make the recursive function the cut of original list in every step?
A=[1,0,2,0,0,3,1,3,2,1] def iter_45(lista): nowa_lista_i = [] for i in range (0, len(lista)): suma_elementow = 0 for i in range(i, len(lista)): suma_elementow += lista[i] nowa_lista_i.append(suma_elementow) return nowa_lista_i i = 0 dlugosc = len(A) nowa_lista = [] def req_45(lista): global i if i == (dlugosc): return nowa_lista else: nowa_lista.append(sum(lista[i:])) i += 1 return req_45(lista[i:]) print('iteracyjnie:\n') print(iter_45(A)) print('\nrekurencyjnie:\n') print(req_45(A))