Page 1 of 1

While the program is able to fill mine-free spots with the number of mines, I noticed a few things that can be improved.

Posted: Tue Jul 12, 2022 8:05 am
by answerhappygod
While the program is able to fill mine-free spots with thenumber of mines, I noticed a few things that can be improved.Please note that as per the PEP8 Python style guide, we areexpected to use whitespace in our code. This way, the code is lessprone to errors and the overall readability is improved. Forexample, line 16 can be written like so: if grid[row][col] == '#':instead of no space before and after the comparison operator (ifgrid[row][col]=='#':). Kindly implement this change in all sectionsof the code.Another important aspect of programming is documentation. Whendocumenting our code we're essentially leaving concise yet clearcomments in the different sections of our code so as to clearlydescribe what each code block does. Kindly follow the link below toread up on comments in Python.https://www.geeksforgeeks.org/python-comments/Finally, as mentioned, the program fills the empty spaces with theappropriate values to signify the number of mines we have put inthere. However, the grid is printed like this:[['1', '1', '2', '#', '#'], ['1', '#', '3', '3', '2'], ['2', '4','#', '2', '0'], ['1', '#', '#', '2', '0'], ['1', '2', '2', '1','0']]instead of[['1', '1', '2', '#', '#'],['1', '#', '3', '3', '2'],['2', '4', '#', '2', '0'],['1', '#', '#', '2', '0'],['1', '2', '2', '1', '0']]Please look into the various built-in functions available to us andtry get one that will help structure the grid properly.
work on this code below
def mines_adj(grid): n = len(grid) if(n == 0): return directions = [[-1, 0], [1, 0], [0, -1], [0, 1], [-1,-1], [-1, 1], [1, -1], [1, 1]] m = len(grid[0]) newgrid = [["#" for j in range(m)] for i inrange(n)] for r in range(n): for c in range(m): if(grid[r][c] =="-"): count =0 for dir indirections: nr = r + dir[0] nc = c + dir[1] if(nr >= 0 and nr < n and nc >=0 and nc < m andgrid[nr][nc] == "#"): count = count + 1 newgrid[r][c] = count return newgrid
# making grid given in inputgrid = [["-", "-", "-", "#", "#"], ["-", "#", "-", "-", "-"], ["-", "-", "#", "-", "-"], ["-", "#", "#", "-", "-"], ["-", "-", "-", "-", "-"]]