Save the Grid

Python for AIMedium+10 XP+10

Save the Grid

A tiny image is stored as a grid of numbers: a list of rows, where each row is a list of whole numbers.

Write save_grid(path, grid). Write the grid into the file called path so that:

  • each row goes on its own line,
  • the numbers in a row are separated by one space,
  • every line ends with \n, including the last.

A row with no numbers becomes an empty line (just \n). An empty grid gives an empty file.

Then read the file back and return the text.

Examples

path = 'img.txt'
grid = [[1, 2], [3, 4]]
returns '1 2\n3 4\n'
path = "sg_a.txt"
grid = [[0, 255, 0], [255, 0, 255]]
returns '0 255 0\n255 0 255\n'
path = "sg_b.txt"
grid = [[7]]
returns '7\n'

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run