From af4dad8622581033e5e0b59a8ee48c1e34b858d5 Mon Sep 17 00:00:00 2001 From: hania-batool Date: Sun, 28 May 2023 02:53:58 +0500 Subject: [PATCH] Added working solution to minesweeper function --- hania_batool.py | 57 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 hania_batool.py diff --git a/hania_batool.py b/hania_batool.py new file mode 100644 index 0000000..10f88f3 --- /dev/null +++ b/hania_batool.py @@ -0,0 +1,57 @@ +def minesweeper(A): + new = [] + for items in A: + new.append('O' + items + 'O') + x = map(lambda x: 'O',range(len(new[0]))) + str = ''.join(list(x)) + new.insert(0,str) + new.append(str) + mines = [] + n = len(new) + for i in range(1, n-1): + row = '' + col = '' + for j in range(1, n-1): + count = 0 + character = new[i][j] + if(character == 'X'): + row += 'X' + else: + if(character == 'O'): + for k in range(j-1,j+2): + if new[i-1][k] == 'X': + count += 1 + if new[i+1][k] == 'X': + count += 1 + if new[i][j-1] == 'X': + count += 1 + if new[i][j+1] == 'X': + count += 1 + col = "{}".format(count) + row += col + mines.append(row) + return mines +if __name__=="__main__": + + test = ["XOOXXXOO", + "OOOOXOXX", + "XXOXXOOO", + "OXOOOXXX", + "OOXXXXOX", + "XOXXXOXO", + "OOOXOXOX", + "XOXXOXOX"] + + """Desired Output is as under + + ["X11XXX32", + "3335X5XX", + "XX3XX554", + "3X556XXX", + "24XXXX6X", + "X3XXX5X3", + "245X6X5X", + "X2XX4X4X"] + """ + + print(minesweeper(test))