Answer to Question #349126 in Python for krish rv

Question #349126

I don't have the question but I can explain.


if given n=4,

we should able to give 4 rows and 4 column matrix as input. matrix consists of 0's and 1's

ex;- 0 1 0 0

0 0 1 0

0 1 0 1

1 1 0 0

 the 0 represents an empty space. the 1 represents a mine. you have to replace each mine with a x and each empty space with a number of adjacent mines

above example should print

1 X 2 1

2 3 X 2

3 X 4 X

X X 3 1


so sample input

4

0 1 0 0

0 0 1 0

0 1 0 1

1 1 0 0


and output should be

1 X 2 1

2 3 X 2

3 X 4 X

X X 3 1


1
Expert's answer
2022-06-08T14:27:43-0400

The answer to your question.

def check_mine(x,y,size_field,field):
    check_lst=[(-1,-1), (0,-1), (1,-1),
               (-1,0),          (1,0),
               (-1,1),  (0,1),  (1,1)]
    list_mine=[]
    for cort in check_lst:
        x1=cort[0]+x
        y1=cort[1]+y
        if x1>0 and x1<(size_field+1) and y1>0 and y1<(size_field+1):
            list_mine.append((x1-1,y1-1))
    res=0
    for t_cort in list_mine:
        check_c = field[t_cort[0]][t_cort[1]]
        res=res+check_c
    return(res)


    
num=int(input("Please enter an integer number: \n"))
i=1
field=[]


while i<num+1:
    line_field=[]
    str_field=""
    str_field=input("Please enter a set of values from the characters '1','0'. "+str(i)+" line out of "+str(num)+": \n")
    for x in list(str_field):
        line_field.append(int(x))
    field.append(line_field)
    i=i+1
    
for field_1 in field:
    print(field_1) 


i=0
j=0
result=[]
for i in range(num): #line 1
    res_line=[]
    for j in range(num): #line 2
        if field[i][j]==1:
            res_line.append('X')
        else:
            res_line.append(check_mine(i+1,j+1,num,field))
        j=j+1
    result.append(res_line)
    i=i+1
    
print("________")


for result_1 in result:
    print(result_1)  

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS