Check horizontal and vertical symmetry in binary matrix | Python3 program to find if a matrix is symmetric . ; Initializing as both horizontal and vertical symmetric . ; Checking for Horizontal Symmetry . We compare first row with last row , second row with second last row and so on . ; Checking each cell of a column . ; check if every cell is identical ; Checking for Vertical Symmetry . We compare first column with last column , second xolumn with second last column and so on . ; Checking each cell of a row . ; check if every cell is identical ; Driver code
MAX = 1000 NEW_LINE def checkHV ( arr , N , M ) : NEW_LINE INDENT horizontal = True NEW_LINE vertical = True NEW_LINE i = 0 NEW_LINE k = N - 1 NEW_LINE while ( i < N // 2 ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT if ( arr [ i ] [ j ] != arr [ k ] [ j ] ) : NEW_LINE INDENT horizontal = False NEW_LINE break NEW_LINE DEDENT DEDENT i += 1 NEW_LINE k -= 1 NEW_LINE DEDENT i = 0 NEW_LINE k = M - 1 NEW_LINE while ( i < M // 2 ) : NEW_LINE INDENT for j in range ( N ) : NEW_LINE INDENT if ( arr [ i ] [ j ] != arr [ k ] [ j ] ) : NEW_LINE INDENT vertical = False NEW_LINE break NEW_LINE DEDENT DEDENT i += 1 NEW_LINE k -= 1 NEW_LINE DEDENT if ( not horizontal and not vertical ) : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT elif ( horizontal and not vertical ) : NEW_LINE INDENT print ( " HORIZONTAL " ) NEW_LINE DEDENT elif ( vertical and not horizontal ) : NEW_LINE INDENT print ( " VERTICAL " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " BOTH " ) NEW_LINE DEDENT DEDENT mat = [ [ 1 , 0 , 1 ] , [ 0 , 0 , 0 ] , [ 1 , 0 , 1 ] ] NEW_LINE checkHV ( mat , 3 , 3 ) NEW_LINE