Reputation: 1179
I'm trying to implement the algorithm, but keep getting an error, and I'm not exactly sure why:
size = len(data)
total = sum(data)
barray = [[False for x in range(int(size+1))] for x in range(int((total/2)+1))]
for x in range(0,size):
barray[0][x] = True
for x in range(1,int(total/2)):
barray[x][0] = True
for i in range(1,int(total/2)):
for j in range(1,size):
barray[i,j] = (barray[i, j - 1] or barray[i - data[j - 1], j - 1]) if data[j-1] <= i else barray[i, j - 1]
return barray[int(total/2),size]
The error:
Traceback (most recent call last):
File "C:/Users/tuf21741/PycharmProjects/PyBackup/test.py", line 25, in <module>
assert checkio([10, 10]) == 0, "1st example"
File "C:/Users/tuf21741/PycharmProjects/PyBackup/test.py", line 17, in checkio
barray[i,j] = (barray[i, j - 1] or barray[i - data[j - 1], j - 1]) if data[j-1] <= i else barray[i, j - 1]
TypeError: list indices must be integers, not tuple
Upvotes: 2
Views: 3009
Reputation: 76234
barray[i,j] = (barray[i, j - 1] or barray[i - data[j - 1], j - 1]) if data[j-1] <= i else barray[i, j - 1]
Looks like you should use separate square brackets for your indexes here.
barray[i][j] = (barray[i][j - 1] or barray[i - data[j - 1]][j - 1]) if data[j-1] <= i else barray[i][j - 1]
And for the return statement, as well.
return barray[int(total/2)][size]
Upvotes: 2