Answer to Question #208458 in Python for Narendra

Question #208458

Area of Square:

you answered like this:

But this is actually calculating the area of a rectangle :

ex:

3 6

O X X X X X

O X X X X X

O X X X X X

def X_Only(Matrix:list):
    for row in Matrix:
        for cell in row:
            # check if value is not X
            if cell!='X':
                return False
    return True
def sub_size(lent:int):
    for start in range(lent):
        for stop in range(start+1, lent+1):
            yield(start, stop)
def squareAreaWithX(matr:list, M:int, N:int):
    maximum_area = 0
    for begin_x, stop_x in sub_size(M):
        for begin_y, stop_y in sub_size(N):
            temp_matrix = [i[begin_y:stop_y] for i in matr[begin_x:stop_x]]
            if X_Only(temp_matrix):
                x= stop_x-begin_x
                y= stop_y-begin_y
                if maximum_area < x*y:
                    maximum_area=x*y
    print(maximum_area)
while True:
    s = input()
    M, N = [int(i) for i in s.split()]
    matrix=[]
    for i in range(0,M):   
        matrix.append([N for N in input().split()]) 


    squareAreaWithX(matrix, M, N)
1
Expert's answer
2021-06-18T14:19:41-0400
import math as mt
 
N = 6
 


 
 
def getMin(x, y):
    if x < y:
        return x
    else:
        return y
 
 
 
def findSubSquare(mat):
 
    Max = 0 
 
    hor = [[0 for i in range(N)]
           for i in range(N)]
    ver = [[0 for i in range(N)]
           for i in range(N)]
 
    if mat[0][0] == 'X':
        hor[0][0] = 1
        ver[0][0] = 1
    for i in range(N):
 
        for j in range(N):
 
            if (mat[i][j] == 'O'):
                ver[i][j], hor[i][j] = 0, 0
            else:
                if j == 0:
                    ver[i][j], hor[i][j] = 1, 1
                else:
                    (ver[i][j],
                     hor[i][j]) = (ver[i - 1][j] + 1,
                                   hor[i][j - 1] + 1)
 
    for i in range(N - 1, 0, -1):
 
        for j in range(N - 1, 0, -1):
 
            small = getMin(hor[i][j], ver[i][j])
 
            while (small > Max):
 
                if (ver[i][j - small + 1] >= small and
                        hor[i - small + 1][j] >= small):
 
                    Max = small
 
                small -= 1
 
    return Max
 
 


mat = [['X', 'O', 'X', 'X', 'X', 'X'],
       ['X', 'O', 'X', 'X', 'O', 'X'],
       ['X', 'X', 'X', 'O', 'O', 'X'],
       ['O', 'X', 'X', 'X', 'X', 'X'],
       ['X', 'X', 'X', 'O', 'X', 'O'],
       ['O', 'O', 'X', 'O', 'O', 'O']]
 


print(findSubSquare(mat))
 

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