I just have learned backtracking yesterday and I'm so confused. In this section
if y == 9:
if x == 8:
display(s)
return 1
else:
sudoku(s,x+1,0)
I use return to stop running program but print(sudoku(s,0,0)) still prints None value and not 1. Can anyone figure out why?
s = [[5,3,0,0,7,0,0,0,0],
[6,0,0,1,9,5,0,0,0],
[0,9,8,0,0,0,0,6,0],
[8,0,0,0,6,0,0,0,3],
[4,0,0,8,0,3,0,0,1],
[7,0,0,0,2,0,0,0,6],
[0,6,0,0,0,0,2,8,0],
[0,0,0,4,1,9,0,0,5],
[0,0,0,0,8,0,0,7,9]]
def display(s):
for i in s:
print(*i)
def checkvalid(s,x,y,k):
for i in range(9):
if s[x][i] == k or s[i][y] == k:
return False
for i in range(x//3*3, x//3*3+3):
for j in range(y//3*3, y//3*3+3):
if s[i][j] == k:
return False
return True
def sudoku(s,x,y):
if y == 9:
if x == 8:
display(s)
return 1
else:
sudoku(s,x+1,0)
elif s[x][y] == 0:
for k in range(1,10):
if checkvalid(s,x,y, k):
s[x][y] = k
sudoku(s,x,y+1)
s[x][y] = 0
else:
sudoku(s,x,y+1)
print(sudoku(s,0,0))
This is because you call sudoku() recursively. Thereby you return 1 to the last caller, which is sudoku() itself from one of the elif/else paths. In those paths you don't return the value back. Instead, you leave the if/elif/else block and exit the function by returning the default value of None.
I am getting None as return value, the code doesn't work as expected, search() returns None in line 4. Why?
I am trying to call a search() function inside a function of a class. Theer seems to be a problem with the inner function returning to the outer one.
class Solution:
def singleNonDuplicate(self, nums):
n = len(nums)
ans = self.search(nums, 0, n-1)
return ans
def search(self,l,low,high):
if low == high: # base case
return (l[high])
else:
mid = low + (high-low)//2 # evaluate mid
if mid%2 == 0:
if l[mid] == l[mid + 1]:
self.search(l,mid+2,high)
else:
self.search(l,low,mid)
else:
if l[mid] != l[mid+1]:
self.search(l,mid+1,high)
else:
self.search(l,low,mid)
You are missing returns from the rest of your recursive function - every time you recurse (called self.search) you have to return the value - and eventually that result will make it up the chain.
class Solution:
def singleNonDuplicate(self, nums):
n = len(nums)
ans = self.search(nums, 0, n-1)
return ans
def search(self,l,low,high):
if low == high: # base case
return (l[high])
else:
mid = low + (high-low)//2 # evaluate mid
if mid%2 == 0:
if l[mid] == l[mid + 1]:
return self.search(l,mid+2,high)
else:
return self.search(l,low,mid)
else:
if l[mid] != l[mid+1]:
return self.search(l,mid+1,high)
else:
return self.search(l,low,mid)
the program needs to return true if the factors of n other than n sum up to n. i need to use the function name during runtime. when i input
factors(45)
it shows that there is an unexpexted token error. please check what is wrong with the program.
def factors(n):#unexpected token error
factorlist = []
for i in range(1,n):
if n%i == 0:
factorlist = factorlist + [i]
return(factorlist)
def perfect(n):
if sum(factorlist) == n:
return(True)
else :
return(False)
You do not call factors(n) into the perfect(n) function. So, you have to use
factorlist = factors(n) into the perfect(n) function.
and then try this way :
def factors(n):
factorlist = []
for i in range(1, n):
if n % i == 0:
factorlist = factorlist + [i]
return (factorlist)
def perfect(n):
factorlist = factors(n) # use this line
if sum(factorlist) == n:
return (True)
else:
return (False)
print(perfect(45)) # Ouput : False
Try :
def factors(n):
factorlist = []
for i in range(1,n):
if n%i == 0:
factorlist = factorlist + [i]
print factorlist
return factorlist
def perfect(n):
factorlist = factors(n)
if sum(factorlist) == n:
return True
else :
return False
n = int(raw_input('Enter the number: '))
print(perfect(n))
Output:
I'm simply looking to add a parenthesis at the end of my recursive function.. I'm literally just missing the final parenthesis, but I can't figure out how to add it in! Any help is greatly appreciated!
My code:
def sum( n ):
if n == 0:
return '1'
elif n == 1:
return '(1+1)'
elif n == 2:
return '((1+1)+(1+1))'
elif n == 3:
return '(((1+1)+(1+1))+((1+1)+(1+1)))'
else:
return '((((1+1)+(1+1))+((1+1)+(1+1)))' + ')'sum_power2( n - 1 )
Just switch the order in the last row, so it would be
def sum_power2( n ):
if n == 0:
return '1'
elif n == 1:
return '(1+1)'
elif n == 2:
return '((1+1)+(1+1))'
elif n == 3:
return '(((1+1)+(1+1))+((1+1)+(1+1)))'
else:
return '((((1+1)+(1+1))+((1+1)+(1+1)))' + sum_power2( n - 1 )+')'
Try this:
def sum_power(n,tmp=''):
tmp = '1' if not tmp else '(' + tmp + '+' + tmp + ')'
if n == 0:
return tmp
else:
n -= 1
return sum_power(n,tmp)
print(sum_power(2))
So what the task is, is that your supposed to write a recursion function that counts the amount of "double" letters in a string, So for example the string "hmmm" would return 1 and the string "hmmmm" would return 2 and that a string "abb" would return 1. My code is here:
def num_double_letters(astr):
if astr == "" or len(astr) == 1:
return 0
elif len(astr) == 2:
if astr[0] == astr[1]:
return 1 + num_double_letters(astr[1:])
else:
return 0 + num_double_letters(astr[1:])
elif astr[0] != astr[1]:
return 0 + num_double_letters(astr[1:])
elif astr[0] == astr[1] != astr[2]:
return 1 + num_double_letters(astr[1:])
elif astr[0] == astr[1] == astr[2]:
return 0 + num_double_letters(astr[1:])
My problem is that a string with 4 same letters = 1 when its supposed to = 2. And also is there a cleaner way to do this?
I think you've made it a bit complicated for yourself... there's no need to go deeper into the recursion once the length of your string is 2, and you want to advance by 2, not 1 when you find a double to count the way I think you do. Try this:
def num_double_letters(astr):
if astr == "" or len(astr) == 1:
return 0
elif len(astr) == 2:
if astr[0] == astr[1]:
return 1
else:
return 0
elif astr[0] != astr[1]:
return 0 + num_double_letters(astr[1:])
elif astr[0] == astr[1]:
return 1 + num_double_letters(astr[2:])
print(num_double_letters('hmm'))
print(num_double_letters('hmmm'))
print(num_double_letters('hmmmm'))
Output:
1
1
2
You might consider the following more Pythonic and concise:
def num_double_letters(astr):
if len(astr) < 2:
return 0
if astr[0] == astr[1]:
return 1 + num_double_letters(astr[2:])
return num_double_letters(astr[1:])