Running cycle through two functions that take each other as input

I have two functions, let's say

def f1(arr,k):
    #does something here with elements of array arr starting from index k and returns i..
    #EDIT: we don't need value1 from f1, 
    i=k
    while arr[i]==0:
        i=i+1
    return i

and another function

def f2(arr,arr2,k):
   # does something here with elements of array arr starting from index k (k is output from function f1)...
    #EDIT: following is the code for f2:
    pr=0
    if arr[k]==1:
        i=k
        while (pr<10 and pr>-10) and (arr2[i+1]!=2):
            pr=pr+(arr2[i+1]-arr2[i])
        i=i+1

    if arr[k]==2:
        i=k
        while (pr<10 and pr>-10) and (arr[i+1]!=1):
            pr=pr+(arr2[i]-arr2[i+1])
            i=i+1
    return i+1, pr

This output i + 1 is again used for the function f1, we do this until we reach the end of the array.

I can’t figure out how to do this.

I define a function

def final(arr):
    x=0 #starting index
    #need to use above two functions to return value2 as a list for each iteration...

Can someone give some direction?

EDIT: after doing what @AlexForGill answered, I get Index Out of Bounds error for f2 function

def final(arr,arr2):
x=0
plist=[]
while x < len(arr)-1:
    x = f1(arr, x)
    if x >= len(arr)-1: # guard clause for applying second function
        break
    x, value2 = f2(arr,arr2, x)
    plist.append(value2)
return plist
+4
source share
1 answer

Will the next job be for you?

def final(arr):
    x = 0
    accumulator = []
    while (x < len(arr)):
        x, value1 = f1(arr, x)
        if (x >= len(arr)): # guard clause for applying second function
            break
        x, value2 = f2(arr, x)
        accumulator.append(value2)
    return accumulator

while f1 f2. f1 , x. f2, , x.

value2 , .

+3

All Articles