Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

added rashid_dev file #44

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"workbench.settings.useSplitJSON": true,
"json.schemas": []
"python.formatting.provider": "black",
"editor.formatOnSave": true
}
61 changes: 61 additions & 0 deletions rashid_dev.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
def minesweeper(A):
# Get the number of rows and columns in the matrix A
row = len(A)
colomn = len(A[0])

# Convert A into a list of lists (matrix) to modify elements
A = [list(A[i]) for i in range(row)]

# Iterate through each element in the matrix
for i in range(row):
for j in range(colomn):
# Only process non-mine cells (cells not equal to "X")
if not A[i][j] == "X":
counter = 0
# Check all neighboring cells (8 possible directions)
for k in range(i - 1, i + 2):
for l in range(j - 1, j + 2):
try:
# Ensure the indices are valid and check if it's a mine ("X")
if not (k == -1 or l == -1) and A[k][l] == "X":
counter += 1
except IndexError:
# Ignore index errors (out of bounds) caused by edge cases
pass

# Convert the count of neighboring mines to string and update the cell
A[i][j] = str(counter)

# Convert the matrix back to the desired output format (list of strings)
A = ["".join(A[i]) for i in range(row)]

# Return the resulting matrix
return A


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))