def func1(N,A):
# prefCount = [[0]*(N+1)]
prefCount = [[0]*(N+1)] * 11
for i in range(1,11):
for j in range(1,N+1):
if A[j-1] == i:
prefCount[i][j] = prefCount[i][j-1] + 1
else:
prefCount[i][j] = prefCount[i][j-1]
return prefCount
def func2(N,A):
prefCount = [[0]*(N+1)]
# prefCount = [[0]*(N+1)] * 11
for i in range(1,11):
tmp = [0] * (N+1)
for j in range(1,N+1):
if A[j-1] == i:
tmp[j] = tmp[j-1] + 1
else:
tmp[j] = tmp[j-1]
prefCount.append(tmp)
return prefCount
print('Output for Function 1')
print(func1(3,[2,2,1]))
print('\nOutput for Function 2')
print(func2(3,[2,2,1]))
Why these two functions giving different outputs ?
Output:
Output for Function 1
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
Output for Function 2
[[0, 0, 0, 0], [0, 0, 0, 1], [0, 1, 2, 2], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]