I am writing code for concentric rectangle and initializing 'result' variable as,
[[0 for x in range(array_size)] for y in range(array_size)],
and I get the right answer. However, when I initialize 'result' variable as [[0]*array_size]*array_size, I get a wrong answer. The rest of my procedure is same for both the cases:
class Solution:
def concentric(self, A):
array_size = 2*A - 1
result = [[0 for x in range(array_size)]
for y in range(array_size)];
#[[0]*array_size]*array_size
for i in range(array_size):
for j in range(array_size):
if (abs(i-(array_size//2)) > abs(j-(array_size//2))):
result[i][j] = abs(i - array_size//2)+1;
else:
result[i][j] = abs(j - array_size//2)+1;
return result
Solution().concentric(3)
The output looks like this:
[[3, 3, 3, 3, 3], [3, 2, 2, 2, 3], [3, 2, 1, 2, 3], [3, 2, 2, 2, 3], [3, 3, 3, 3, 3]]
Any suggestion or explanation would be appreciated.