I've got an array like this
[["1"], ["2","3"]]
In the beginning I had this algorithm
maxarray = []
i = 0
m = array.count
while i < m do
x = 0
s = array[i].count
while x < s do
maxarray[i][x] = (Integer(array[i][x]) + [Integer(maxarray[i-1][x-1]), Integer(maxarray[i-1][x])].max)
开发者_高级运维x+=1
end
i+=1
end
the error was this:
`<main>': undefined method `[]' for nil:NilClass (NoMethodError)
so I thought maybe the array indexes goes wrong so I extended my algorithm
maxarray = []
i = 0
m = array.count
while i < m do
x = 0
s = array[i].count
while x < s do
if i-1 < 0
item2 = 0
elsif i-1 < 0 && x-1 < 0
item1 = 0
else item1 = maxarray[i-1][x-1]
item2 = maxarray[i-1][x]
end
maxarray[i][x] = (Integer(array[i][x]) + [Integer(item1), Integer(item2)].max)
x+=1
end
i+=1
end
but now i have an different error:
`Integer': can't convert nil into Integer (TypeError)
how can prevent this error and run the script?
Why you don't flatten
the array and make your computing? Certainly it will make your function simplier.
精彩评论