-1

for example, I have a 9x9 sudoku board like this:

big = [[0, 0, 0, 0, 0, 0, 0, 0, 0],
   [4, 0, 0, 7, 8, 9, 0, 0, 0],
   [7, 8, 0, 0, 0, 0, 0, 5, 6],
   [0, 2, 0, 3, 6, 0, 8, 0, 0],
   [0, 0, 5, 0, 0, 7, 0, 1, 0],
   [8, 0, 0, 2, 0, 0, 0, 0, 5],
   [0, 0, 1, 6, 4, 0, 9, 7, 0],
   [0, 0, 0, 9, 0, 0, 0, 0, 0],
   [0, 0, 0, 0, 3, 0, 0, 0, 2]]

and I have a function: def subgrid_values(board,r,c) board takes in the size of the board which in this case is big. r takes in the row and c takes in the column.

so if I input subgrid_values(big,4,5) it will give me an output of [3,6,7,2]

a pic of the higlighted box

I'm trying to output that list but I do not know how to

I hope this makes it clearer.

Thomas
  • 174,939
  • 50
  • 355
  • 478
Bryan Hii
  • 129
  • 7
  • 3
    So whats your problem? what error you've got? – Ghost Ops Aug 27 '21 at 07:37
  • 1
    What are you trying to do? – PCM Aug 27 '21 at 07:42
  • Hold on, lemme try to see if I can edit the question so that I can try to explain the question a bit better – Bryan Hii Aug 27 '21 at 08:09
  • I've finished editing the question, sorry for making it so confusing, I hope this conveys my question clearer – Bryan Hii Aug 27 '21 at 08:23
  • Did you mean to say that output contains all the non 0 values of the sub-gird in which (r, c) is present? – Arpit Parmar Aug 27 '21 at 08:29
  • @ArpitParmar yes. Like from the picture. r and c is just a position on the board and I need to output all the values in the sub-grid. If that make sense. – Bryan Hii Aug 27 '21 at 08:34
  • what do you mean by "`r` takes in the row and `c` takes in the column" ? did you want to output all non-zero numbers in a 3x3 cell that contains that `(r,c)` cell ? – AcaNg Aug 27 '21 at 08:44
  • no, I meant r and c is a position. For example, in the picture r = 4 and c = 5. Starting from index 0. So the value is at the 5th row and 6th column because it starts at index 0. And the value is 7 in this case, and 7 is in that highlighted box in the picture, in which I need to output all of the element in that highlighted box. I hope that makes it clearer and I'm sorry if its still confusing, its hard to explain through a comment. – Bryan Hii Aug 27 '21 at 08:52
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Aug 27 '21 at 14:07

1 Answers1

0

One of the solutions would be to get the starting index of the sub-grid and loop three times on row and column as follows:

def solution(big, r, c):
  output = []
  row_start_index = (r // 3) * 3
  col_start_index = (c // 3) * 3

  for ri in range(row_start_index, row_start_index + 3):
    for ci in range(col_start_index, col_start_index + 3):
      if big[ri][ci]:
        output.append(big[ri][ci])
  return output
Arpit Parmar
  • 300
  • 1
  • 9
  • 1
    Thank you so much, this code works. But could you explain how the line row_start_index works? – Bryan Hii Aug 27 '21 at 09:02
  • 1
    You're welcome @BryanHii. Well, in each subgrid the starting row index would be a multiple of 3 and same is the case for column index. Any r or c in between multiples of 3 we don't care about the fractional part so we divide r and c using //3 to get just the integer value of that division and now again multiplying that value by 3 would take us to the first row index of that subgrid. We do the same for the column. This gives us the first row and column indices of that subgrid which (r, c) belongs to, and then we simply traverse that subgird using those starting indices. – Arpit Parmar Aug 27 '21 at 09:40
  • Oh, I understand, thank you so much again, you are a genius. – Bryan Hii Aug 27 '21 at 09:44
  • Just another average guy this side. BTW please do upvote the previous comment took me a lot of effort to write it in the given character limit haha. – Arpit Parmar Aug 27 '21 at 09:47
  • I would if I could man, but it says I need 15 reputation points, and I'm just starting out. But it does say that my feedback has been recorded. I'm sorry, I would've given your post a hundred upvotes if I could. Thank you so much again! – Bryan Hii Aug 27 '21 at 09:59