Reputation: 67
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()
Upvotes: 0
Views: 6233
Reputation: 1122412
You should not use *arr
; remove the *
wildcard character and your code will work.
With the wildcard character, the argument passed into msa2
is seen as one of potentially more extra positional arguments, so arr
inside msa2
is a list of those arguments, with the first element being the list you passed in when you called msa2
:
>>> def foo(*args):
... print args
...
>>> foo(1, 2, 3)
(1, 2, 3)
>>> foo([1, 2, 3])
([1, 2, 3],)
Your function also will always return 0
; you do not update maxThus
anywhere. You probably meant to assign the result of max(maxThus, sum)
to maxThus
:
maxThus = max(maxThus, sum)
Upvotes: 4