4

I have two functions, 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 function f1, we do this till the we reach the end of array.

I can't seem to get the logic on 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 provide some direction?

EDIT: After doing what @AlexForGill answered, I am getting Index Out of Bounds error for function f2

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
3
  • 1
    So you don't need value1 at all? Commented Jun 24, 2015 at 9:34
  • Apparently, I realized I don't need value1. Commented Jun 24, 2015 at 9:53
  • I suggest you to put f1 in f2, and loop f2 if f1 has nothing to do with the rest. Commented Jun 24, 2015 at 10:01

1 Answer 1

3

Would the following work 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

The while loop alternates between calling f1 and f2. The result of f1 is the updated index, which is assigned to x. This value is then passed into f2, which also returns the updated index which is again assigned to x.

value2 is then appended to the accumulator list which is returned at the end of the loop.

Sign up to request clarification or add additional context in comments.

7 Comments

Could you provide a short explanation?
Nice, but I think this should also collect value2 in a list.
@tobias_k you're right - I missed that comment in the question
@AmanArora I've added an explanation and a list accumulator for gathering all of the value2s
Thank you. But I'm getting an IndexOutOfBoundsException when function f1 cannot find an index near the end of the list. (according to some conditions described in the function)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.