Currently I am improving my python using tasks on Codility and ran into a problem while completing the MaxCounter exercise.
This exercise gives an integer N and a list A where N is within the range [1..100,000] and each element of array A is an integer within the range [1..N 1].
N represents N counters all beginning at 0 and each element in A represents an operation on the list of counters. If A[i] <= N then the A[i]th counter is incremented 1 such that if A[i] = 1 then the 0th counter is incremented. If A[i] == N 1 then all of the counters are set the value of the current maximum.
After all actions are completed return the list of counters.
My code is as follows:
def solution(N, A):
# write your code in Python 3.6
counters = [0] * N
for i in A:
if i == N 1:
counters = max(counters) * N
else:
counters[i-1] = 1
return counters
pass
On the line "counters[i-1] = 1" I am receiving the error:
File "/tmp/solution.py", line 13, in solution counters[i-1] = 1
TypeError: 'int' object is not subscriptable
I am not sure why I am getting this error as I am not calling an integer as a list. Any insights would be greatly appreciated
CodePudding user response:
your list counters is being set to an integer by the line max(counters) * N
you need to change the variable name that this line is set to.
CodePudding user response:
Counters is an integer and not a list, so it is not subscriptable.