Predict the output
What does this print?
grid = [[1, 3, 5], [7, 9, 11], [13, 15, 17]]
cols = len(grid[0])
k = 7
print(grid[k // cols][k % cols], k // cols, k % cols)Choose an answer, then open Show answer below to check it.
Show answer
Answer: A 15 2 1
Right: position 7 is row 2, column 1.
Dividing by the column count turns one position into a row and column, which lets binary search treat the grid as a single list.
Why not the others?
- B 9 1 1. Row 1 covers positions 3 to 5.
- C 13 2 0. 7 % 3 is 1, so it is the second item of row 2.