/ / Funkcja rekurencyjna Pythona nie zwraca - python, rekursja

Funkcja rekurencyjna Pythona nie zwraca - python, rekursja

Próbuję wyostrzyć moje umiejętności noob Pythonpróbując rozwiązać problem, który mój syn ma w swojej klasie na studiach. Celem jest stworzenie funkcji, która używa rekursji do przetwarzania listy. Funkcja musi zaakceptować listę dowolnej długości i zwrócić nową listę, w której każdy element jest sumą samej siebie i elementów po prawej. Jeśli więc wprowadzisz listę [5, 3, 2, 4], funkcja powinna zwrócić [14, 9, 6, 4].

W Pythonie napisałem następujący kod i todziała poprawnie, jeśli w funkcji rekurencyjnej umieściłem polecenie „drukuj”, aby wyświetlić wartość końcową, ale nie przekazałby jej wartości zwracanych. Końcowym wynikiem jest „Brak”.

Dlaczego nie zwracają poprawnie zmiennych?

def RecursiveProcess(ListIn2, target): #Recursive function that adds to target value the value to its right
if target > -1:  #stop function if index is below 0
ListIn2[target] = ListIn2[target] + ListIn2[target+1]  #Add value to the right of target to the target value
ListIn2 = RecursiveProcess(ListIn2, target-1) #Call the function again with lower taget value to process the next value
else:
return ListIn2  #return the changed list

def ProcessList(ListIn):  #helper function to pass the list and the position of penultimate value
return RecursiveProcess(ListIn, len(ListIn)-2) #return the changed list

print ProcessList([5, 10, 11, 6, 7, 1, 2, 4, 6, 7])  #initiate recursion and print result

Odpowiedzi:

3 dla odpowiedzi № 1

Musisz faktycznie zwrócić swój RecursiveProcess. Spójrz poniżej na swój zmodyfikowany kod.

Nie robisz niczego rekurencyjnie, jeśli wszystko, co robisz, to wywołanie funkcji i zapisanie wartości w ListIn2. Nadpisujesz poprzednie dane. Po powrocie otrzymasz rekurencyjne zachowanie:

def RecursiveProcess(ListIn2, target): #Recursive function that adds to target value the value to its right
if target > -1:  #stop function if index is below 0
ListIn2[target] = ListIn2[target] + ListIn2[target+1]  #Add value to the right of target to the target value
return RecursiveProcess(ListIn2, target-1) #Call the function again with lower taget value to process the next value
else:
return ListIn2  #return the changed list

l = [5, 10, 11, 6, 7, 1, 2, 4, 6, 7]
d = RecursiveProcess(l, len(l)-2)
print(d) # [59, 54, 44, 33, 27, 20, 19, 17, 13, 7]

1 dla odpowiedzi nr 2

Problem polega na tym, że nie wykonujesz rekurencji (każde wywołanie tej samej funkcji zwraca wynik), ale ponieważ listy są zmienne, nie musisz:

def ProcessList(ListIn):
RecursiveProcess(ListIn, len(ListIn)-2) #this will change the list in place!
return ListIn

wszystko po to, aby działało jak oczekiwano. Ponieważ każdy element jest aktualizowany w „rekurencji”, nie ma potrzeby przekazywania wskaźnika do listy w pobliżu.

# [59, 54, 44, 33, 27, 20, 19, 17, 13, 7]

0 dla odpowiedzi № 3

Nie wracasz do sprawy ogólnej. Spróbuj:

def RecursiveProcess(ListIn2, target):
if target > -1:
ListIn2[target] = ListIn2[target] + ListIn2[target+1]
ListIn2 = RecursiveProcess(ListIn2, target-1)
return ListIn2 #Actually need to return in the general case
else:
return ListIn2