0

I'm trying to solve some codility task using recursion in Python.
Can someone check this code and tell me why it returns None? I want to get [4, 5] in the variable called "solution".

def rec_fun(upstream,downstream):

    if not upstream or not downstream : 
        if upstream : 

            return  upstream
        if downstream: 
            return downstream
    if upstream[0] >downstream[0] : 
        downstream.pop(0)
    else:
        upstream.pop(0)

    rec_fun(upstream,downstream)


def solution(A, B):

    upstream=[]
    downstream=[]
    n=len(A)
    for i in range(0,n) : 
        if B[i]==0:
            upstream.append(A[i])
        else:
            downstream.append(A[i])
    upstream=sorted(upstream)
    downstream=sorted(downstream)


    return rec_fun(upstream,downstream)


A=[4,3,2,1,5]
B=[0,1,0,0,0]

solution =  solution(A, B)
print solution

The output is: output = None, it should be [4, 5].

1
  • return rec_fun(upstream,downstream) Commented Apr 18, 2016 at 11:09

1 Answer 1

0

In your recursive function you are not returning anything. You have to add some return statement. Namely:

return rec_fun(upstream,downstream)
Sign up to request clarification or add additional context in comments.

3 Comments

I have 2 returns return upstream and return downstream ..
If yo uadd print here : if upstream : print "return " print upstream return upstream , You will see what I'm talking..
it works now , tnx

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.