human
&nbsp;

https://leetcode.com/problems/toeplitz-matrix/description/
766. Toeplitz Matrix
A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element.
Now given an M x N matrix, return True if and only if the matrix is Toeplitz.
Example 1:
Input: matrix = [[1,2,3,4],[5,1,2,3],[9,5,1,2]]
Output: True
Explanation:
1234
5123
9512
In the above grid, the diagonals are "[9]", "[5, 5]", "[1, 1, 1]", "[2, 2, 2]", "[3, 3]", "[4]", and in each diagonal all elements are the same, so the answer is True.
Example 2:
Input: matrix = [[1,2],[2,2]]
Output: False
Explanation:
The diagonal "[1, 2]" has different elements.
Note:
matrix will be a 2D array of integers.
matrix will have a number of rows and columns in range [1, 20].
matrix[i][j] will be integers in range [0, 99].
Spent <span style='color:red'>2h!</span>

What is a the Python program you requested?
assistant
#Here is the Python program you requested.

m=[
    [3, 4, 5, 6],
    
    [2, 3, 4, 5],
    
    [1, 2, 3, 4]
]

def is_toeplitz(m):
    
    def is_diag(i0,j0,n_rows,n_cols):
        M=min(n_rows-i0,n_cols-j0)
        return all( [m[i0][j0]==m[i0+k][j0+k] for k in range(1,M)] )

    
    n_rows=len( m )
    n_cols=len( m[0])
    
    first_row = [is_diag(0,j,n_rows,n_cols) for j in range(0,n_cols)]
    first_col = [is_diag(i,0,n_rows,n_cols) for i in range(1,n_rows)]
    
    return (all(first_row) and all(first_col))

# Left-Top neibour
def is_toeplitz2(m):
    n_rows=len( m )
    n_cols=len( m[0])
    
    return all(i==0 or j==0 or m[i][j]==m[i-1][j-1] 
               for j in range(0,n_cols) 
               for i in range(0,n_rows))

def is_toeplitz3(m):
    n_rows=len( m )
    
    if n_rows < 2:
        return True
    
    n_cols=len( m[0])
    
    if n_cols < 2:
        return True
    
    return all(m[i][j]==m[i-1][j-1] 
               for j in range(1,n_cols) 
               for i in range(1,n_rows))



def isToeplitzMatrix(matrix):
    return all(r == 0 or c == 0 or matrix[r-1][c-1] == val
               for r, row in enumerate(matrix)
               for c, val in enumerate(row))

    
    
print(is_toeplitz(m))
print(is_toeplitz3(m))

%timeit is_toeplitz(m)
%timeit is_toeplitz3(m)



mm=[[(i,j) for j in range(0,4)] for i in range(0,3)]
print(mm)

# flatten
flat_list = [v for row in m for v in row]
print(flat_list)

# which means:
flat_list =[]
for row in m:
    for v in row:
        flat_list.append(v)
print(flat_list)