Find if given matrix is Toeplitz or not | Python3 program to check whether given matrix is a Toeplitz matrix or not ; row = number of rows col = number of columns ; dictionary to store key , value pairs ; if key value exists in the map , ; we check whether the current value stored in this key matches to element at current index or not . If not , return false ; else we put key , value pair in map ; Driver Code ; Function call
def isToeplitz ( matrix ) : NEW_LINE INDENT row = len ( matrix ) NEW_LINE col = len ( matrix [ 0 ] ) NEW_LINE map = { } NEW_LINE for i in range ( row ) : NEW_LINE INDENT for j in range ( col ) : NEW_LINE INDENT key = i - j NEW_LINE if ( key in map ) : NEW_LINE INDENT if ( map [ key ] != matrix [ i ] [ j ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT map [ key ] = matrix [ i ] [ j ] NEW_LINE DEDENT DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT matrix = [ [ 12 , 23 , - 32 ] , [ - 20 , 12 , 23 ] , [ 56 , - 20 , 12 ] , [ 38 , 56 , - 20 ] ] NEW_LINE if ( isToeplitz ( matrix ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT