Skip to content

Question 28

For an n x n chess board, we can label each square as an xy-coordinate pair. We will use zero-based indexing, namely counting starts with 0 instead of 1. The function checker_coord(n) returns a list of all coordinates in the board.

Sample output:

>>> print(checker_coord(2))
[(0, 0), (1, 0), (0, 1), (1, 1)]
>>> print(checker_coord(3))
[(0, 0), (1, 0), (2, 0), (0, 1), (1, 1), (2, 1), (0, 2), (1, 2), (2, 2)]

Complete the code below by filling in the blanks. The order of each item in the output list must match that in the sample output. [5 marks]

1
2
3
4
5
6
def checker_coord(n):
    output = []
    for i in range(___BLANK_1___):
        for j in range(___BLANK_2___):
            output.append(___BLANK_3___):
    return output
Model Solution
1
2
3
4
5
6
def checker_coord(n):
    output = []
    for i in range(n):
        for j in range(n):
            output.append((j,i)):
    return output
  • Blank 1: n
  • Blank 2: n
  • Blank 3: (j, i)