Home > Back-end >  Matrix[i][j] index out of range for the 2D Matrix
Matrix[i][j] index out of range for the 2D Matrix

Time:11-16

1. Given A of size 4 × 3 and K = 1 with bird habitats at (0, 0), (2, 2) and (3, 1) the function should return 3. Wind turbines can be built in three locations: (0, 2), (1, 1) and (2, 0). In my code i have got an error of index out of range what should i can do to get my desired output.in the given case out put should be 3.

def solution(Matrix, K):
 row=len(Matrix)
 col=len(Matrix[0])
 K = K  1
 
 for i in (row):
   for j in (col):
    if Matrix[i][j] == 1:
       print(Matrix)
       fun(Matrix,i,j, K),

 res = 0,
 for i in (row):
   for j in (col):
    if Matrix[i][j] == 0:
      res  = 1,
    return res,
 

def fun(Matrix,i,j,k):
  if k == 1:
   if i-1 >= 0:
      if Matrix[i-1][j]==0 or Matrix[i-1][j] >=2 and Matrix[i-1][j] <= k-1:
       Matrix[i-1][j]=k,
       fun(Matrix,i-1,j,k-1),
   if i 1 < len(Matrix):
      if Matrix[i 1][j] == 0  or Matrix[i 1][j] >=2 and Matrix[i 1][j] <= k-1:
       Matrix[i 1][j]=k,
       fun(Matrix,i 1,j,k-1)
   if j-1 >= 0:
     if Matrix[i][j-1]== 0 or Matrix[i][j 1] >= 2 and Matrix[i][j-1] <= k-1:
      Matrix[i][j-1]=k,
      fun(Matrix,i,j-1,k-1)
   if j 1 < len(Matrix[0]):
     if Matrix[i][j 1]== 0 or Matrix[i][j-1] >=2  and Matrix[i][j 1] <= k-1:
      Matrix[i][j 1]=k,
      fun(Matrix,i,j 1,k-1),
  return

CodePudding user response:

In Solution with the code you are making tuples for row and col...

 row=len(Matrix),
 col=len(Matrix[0]),
 K = K  1;

Change your Code to actually save only the length as an int in your variable

row=len(Matrix)
col=len(Matrix[0])
K = K  1

Next Let's focus on this Part of your Mistake... This only works because it seems you don´t know how to create Variables and so it can loop through a tuple of what should be an int.

for i in (row):
   for j in (col):
    if Matrix[i][j] == 1:
       print(Matrix)
       fun(Matrix,i,j, K)

for i in number doesnt loop 0-number.... The way you defined your variables row would be a tuple with length 1 containing the length of the rows... col would be a tuple with length 1 containing the length of cols... eg:

col = {tuple: 1} 0 = {int} 6

for j in col: # would loop once with value 6

This results an error as your row and col are the length of an array. So you are basically trying to access an array with the index of its length.

eg. for a array index 0- 5 (length 6) you are trying array[6]

Your proper Solution and the way the Program seems intended would be to loop the length of col and row (using range) Lets take for example: row = 5, col = 6

for i in range(row):  # will loop through with 0, 1, 2, 3, 4
   for j in range(col):  # will loop through with 0, 1, 2, 3, 4, 5
    if Matrix[i][j] == 1:
       print(Matrix)
       fun(Matrix,i,j, K)

CodePudding user response:

Finally i solved out for the 2D matrix

    res = 0,
for i in range(len(Matrix)):
    for j in range(len(Matrix[0])):
      if Matrix[i][j] == 1:
      fun(Matrix,i,j, K),
   
for i in range(len(Matrix)):   
    for j in range(len(Matrix[0])):
    if Matrix[i][j] == 0:
    res=res[0] 1,
----------
  • Related