Computer Science

# Lab 19 - Coordinates

## Activities

### checkerboard.py

Write a function checkerboard(n, m) that creates a grid with n rows and m columns in a checkerboard patter made from X and O. The value in the top left corner should be a X.

For example:

checkerboard(3, 3)

Would return:

[["X", "O", "X"], ["O", "X", "O"], ["X", "O", "X"]]

### identity_matrix.py

Write a function identity_matrix(n) that creates an identity matrix with n rows and n columns. An identity matrix a grid with ones on the main diagonal and zeros elsewhere.

1 0 0
0 1 0
0 0 1

For example:

identity_matrix(3)

Would return:

[[1, 0, 0], [0, 1, 0], [0, 0, 1]]

### upper_triangular.py

Write a function upper_triangular(n) creates an upper-triangular matrix with n rows and n columns. An upper triangular matrix is a grid where all the elements below the main diagonal as zero and all the elements on and above the diagonal are non-zero integers. For simplicity, make the non-zero elements equal to 1.

Example 3x3 upper triangular matrix:

\begin{bmatrix} 1 & 1 & 1\ 0 & 1 & 1 0 & 0 & 1 \end{bmatrix}

For example:

upper_triangular(3)

Would return:

[[1, 1, 1] ,[0, 1, 1], [0, 0, 1]]

### get_coords.py

Write a function get_coords(grid, value) that returns a list of tuples of (row, column) coordinates for elements in grid that match value.

For example:

get_coords([["duck", "duck", "goose"], ["goose", "duck", "duck"], ["duck", "goose", "duck"]], "goose")

Would return:

[(0, 2), (1, 0), (2, 1)]

### grid_game.py

grid_game.py is a game that updates a 4x4 grid based on user input. The program will put an X in a row/column coordinate given by the user.

### Requirements

• Display the starting 4x4 grid with every entry set to .
• Prompt the user for an input in the form: Enter a coordinate: . The user will input the coordinate in the form: 1, 0 or 1,0
• Display the new updated grid to the user with an X in the entered coordinate
• Repeat the last two steps until the program ends
• End the program when the user inputs q instead of a coordinate

### Example

python grid_game.py

....
....
....
....
Enter a coordinate: 0, 0
X...
....
....
....
Enter a coordinate: 1,0
X...
X...
....
....
Enter a coordinate: 3,3
X...
X...
....
...X
Enter a coordinate: q

checkerboard.py5
identity_matrix.py5
upper_triangular.py5
get_coords.py5
grid_game.py10