I keep getting a TypeError: unsupported operand type for +: ‘int’ and ‘list’
so I guess the array isn’t being indexed? Please assist.
def main():
arr = [1, 2, 3, 4, 5]
length = len(arr)
maxAns = msa2(length, arr)
print maxAns
def msa2(length, *arr):
maxThus = 0
for i in range(0, length):
sum = 0
for j in range(i, length):
sum = sum + arr[j] # how to get value in index j
max(maxThus, sum)
return maxThus
if __name__ == '__main__':
main()
You should not use
*arr; remove the*wildcard character and your code will work.With the wildcard character, the argument passed into
msa2is seen as one of potentially more extra positional arguments, soarrinsidemsa2is a list of those arguments, with the first element being the list you passed in when you calledmsa2:Your function also will always return
0; you do not updatemaxThusanywhere. You probably meant to assign the result ofmax(maxThus, sum)tomaxThus: