May CookOff-19 | REACTION | NZEC

Link: https://www.codechef.com/viewsolution/24327316
I tried submitting this code multiple times but was unsuccessful in doing so because of the NZEC.
Please HELP in finding the error.

Firstly, the correct way to initialize 2d arrays in python is: a = [[0 for _ in range(c)] for _ in range(r)].

When you do a = [[0]*r]*c, only the first row is created and the pointer/reference to this row is copied to the rest of the rows.

>>> r, c = 3, 3
>>> a = [[0]*r]*c
>>> a
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
>>> a[0][0] = 1
>>> a
[[1, 0, 0], [1, 0, 0], [1, 0, 0]]

The last result might seem counter-intuitive but when you think about, it makes sense!

Second, the actual bug in your code was that you initialized a to have c rows and r columns instead of the other way round :stuck_out_tongue:

1 Like

Thanks a lot, eragon987!