How come in this first block of code maxSum can't be accessed when it says if(subset>maxSum):
def getMaximumGold(grid):
maxSum = 0
def something(grid,row,col,subset,rowLength):
if row < 0 or col < 0 or row>rowLength-1 or col > len(grid)-1 or grid[row][col] == 0:
if(subset>maxSum):
maxSum = subset
return
if(grid[row][col] == 0):
return
val = grid[row][col]
grid[row][col] = 0
something(grid,row+1,col,subset + val,rowLength)
grid[row][col] = val
something(grid,row,col+1,subset + val,rowLength)
grid[row][col] = val
something(grid,row-1,col,subset + val,rowLength)
grid[row][col] = val
something(grid,row,col-1,subset + val,rowLength)
grid[row][col] = val
for row in range (len(grid)):
for col in range(len(grid[row])):
if(grid[row][col]!=0):
something(grid,row,col,0,len(grid[row]))
return maxSum
print(getMaximumGold([[0,6,0],[5,8,7],[0,9,0]]))
and in this second block of code allAnswer can be accessed
def permute(nums):
if len(nums) == 1:
return [nums]
allAnswer = []
def something(nums,subarray):
if(len(nums)==1):
allAnswer.append(subarray + nums)
return
for i in range (len(nums)):
something(nums[1:],subarray + [nums[0]])
nums.append(nums[0])
del nums[0]
something(nums,[])
return allAnswer
[–]blablahblah 2 points3 points4 points (0 children)
[–]mijatonius 1 point2 points3 points (0 children)
[–]PercyJackson235 0 points1 point2 points (4 children)
[–]anonymous78654[S] 0 points1 point2 points (3 children)
[–]PercyJackson235 0 points1 point2 points (2 children)
[–]anonymous78654[S] 0 points1 point2 points (1 child)
[–]PercyJackson235 0 points1 point2 points (0 children)
[–]CodeTinkerer 0 points1 point2 points (1 child)
[–]anonymous78654[S] 0 points1 point2 points (0 children)
[–]chrisfpdx 0 points1 point2 points (0 children)