This question already has answers here:
Why does my recursive function return None?
(4 answers)
Closed 6 years ago.
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].
In your recursive function you are not returning anything. You have to add some return statement. Namely:
return rec_fun(upstream,downstream)
Related
This question already has answers here:
Why does my recursive function return None?
(4 answers)
Closed 7 months ago.
def binarySearch(nums,low,high,target):
if low<=high:
mid=(low+high)//2
if nums[mid]==target:
return mid
if nums[mid]<target:
binarySearch(nums,mid+1,high,target)
else:
binarySearch(nums,low,mid-1,target)
else:
return -1
def search(nums, target):
return binarySearch(nums,0,len(nums)-1,target)
nums=[-1,0,3,5,9,12]
target=9
print(search(nums,target))
console output
Expected output of the above python code for binary search is '4'. But my output is 'None'. I also printed the value of mid on console output just before the line "return mid"(line number 7), it was showing the expected value '4'. But it returns 'None' instead of that expected value. Please find out the problem and it's solution.
if nums[mid]<target:
return binarySearch(nums,mid+1,high,target)
else:
return binarySearch(nums,low,mid-1,target)
This question already has an answer here:
Python function returns None, unclear why
(1 answer)
Closed 2 years ago.
I created a function to square some numbers. I was wondering why my code didn't print.
def Square(A):
for i in range(len(A)):
A[i] = A[i]**2
A = [2,0,-3]
print(Square(A))
Add a return value.
def Square(A):
for i in range(len(A)):
A[i] = A[i]**2
return A
print(Square(A))
This question already has an answer here:
Why Python recursive function returns None [duplicate]
(1 answer)
Closed 2 years ago.
I'm trying to create a recursive function in Python (as part of an online course) which takes two numbers and returns the highest common denominator between them, however, the function won't return the value.
My code is:
def gcdRecur(a, b):
x = min(a,b)
y = max(a, b)
if x == 0:
return y
else:
gcdRecur(x, y%x)
When I test the function with
gcdRecur(2,12)
or
gcdRecur(6,12)
nothing is returned. I know the function works because if I print y before I return it then the correct denominator is printed to the console, I'm just not sure why the function doesn't output y.
It's probably something obvious but as I'm new to Python I'm not sure what I'm doing wrong.
Any help would be much appreciated.
You need to return something in both branches:
else:
return gcdRecur(x, y%x)
This question already has answers here:
Why does my recursive function return None?
(4 answers)
Closed 4 years ago.
I had trouble understanding why this function returns None:
def rem(x, a):
"""
x: a non-negative integer argument
a: a positive integer argument
returns: integer, the remainder when x is divided by a.
"""
if x == a:
return 0
elif x < a:
return x
else:
rem(x-a, a)
rem(7, 5)
I then realized that the last line in the function should be:
return rem(x-a, a)
I am still unsure as to what really happens in the first one. It looks like there are 2 seperate function calls, one returning 2 and the other None... any help?
All python function returns None if it doesn't terminate by a return statement. In your case, you successfully called the recursion but you discarded the result in the else part. And after the if-block, the default return None is implicitly executed.
This question already has answers here:
Why does my recursive function return None?
(4 answers)
Closed 7 months ago.
I tried to write a program to return he max number, however it returns none. I look into similar questions but I did't find the answer.
def findMax(alist):
if len(alist) == 1:
return alist[0]
else:
if alist[0] > alist[1]:
alist[1] = alist[0]
findMax(alist[1:])
def main():
a = [1,3,4,2,6,7,9,12,3,20,4,32,5,6,9,10]
print(findMax(a))
main()
Recursive functions need to return something under all conditions, you don't return anything under the else condition. Try return findMax(alist[1:]).