'Recursion in Python return None [duplicate]
Why does the function integerCubeRootHelper return None instead of an integer value?
def integerCubeRootHelper(n, left, right):
cube = lambda x: x * x * x # anonymous function to cube a number
assert(n >= 1)
assert(left < right)
assert(left >= 0)
assert(right < n)
assert(cube(left) < n), f'{left}, {right}'
assert(cube(right) > n), f'{left}, {right}'
mid=(left+right)//2
if ( left == mid ):
return left
elif(cube(mid)> n):
integerCubeRootHelper(n, left, mid)
else:
integerCubeRootHelper(n, mid, right)
def integerCubeRoot(n):
if (n == 1):
return 1
if (n == 2):
return 1
return integerCubeRootHelper(n, 0, n-1)
integerCubeRoot(3)
Solution 1:[1]
You don't return the result of the recursive function - you only run it.
If you change your code to the following you will no longer get None:
def integerCubeRootHelper(n, left, right):
cube = lambda x: x * x * x # anonymous function to cube a number
assert(n >= 1)
assert(left < right)
assert(left >= 0)
assert(right < n)
assert(cube(left) < n), f'{left}, {right}'
assert(cube(right) > n), f'{left}, {right}'
mid=(left+right)//2
if ( left == mid ):
return left
elif(cube(mid)> n):
return integerCubeRootHelper(n, left, mid) # CHANGED
else:
return integerCubeRootHelper(n, mid, right) # CHANGED
def integerCubeRoot(n):
if (n == 1):
return 1
if (n == 2):
return 1
return integerCubeRootHelper(n, 0, n-1)
ans = integerCubeRoot(3)
print(ans) # 1
Solution 2:[2]
The main problem is you miss return:
def integerCubeRootHelper(n, left, right):
cube = lambda x: x * x * x # anonymous function to cube a number
assert (n >= 1)
assert (left < right)
assert (left >= 0)
assert (right < n)
assert (cube(left) < n), f'{left}, {right}'
assert (cube(right) > n), f'{left}, {right}'
mid = (left + right) // 2
if (left == mid):
return left
elif (cube(mid) > n):
return integerCubeRootHelper(n, left, mid)
else:
return integerCubeRootHelper(n, mid, right)
def integerCubeRoot(n):
if (n == 1):
return 1
if (n == 2):
return 1
return integerCubeRootHelper(n, 0, n - 1)
print(integerCubeRoot(3))
And some more advice about your code that can help you to increase code correctness and readability:
- Your functions' name should be lowercase. Link
- You have to remove redundant parentheses.
- Always use a
defstatement instead of an assignment statement that binds a lambda expression directly to a name. Link
Updated version:
def integer_cube_root_helper(n, left, right):
def cube(x):
return x * x * x
assert n >= 1
assert left < right
assert left >= 0
assert right < n
assert cube(left) < n, f'{left}, {right}'
assert cube(right) > n, f'{left}, {right}'
mid = (left + right) // 2
if left == mid:
return left
elif cube(mid) > n:
return integer_cube_root_helper(n, left, mid)
else:
return integer_cube_root_helper(n, mid, right)
def integer_cube_root(n):
if n == 1:
return 1
if n == 2:
return 1
return integer_cube_root_helper(n, 0, n - 1)
print(integer_cube_root(3))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Pro Q |
| Solution 2 |
