Skip to content
DeepForgeby svx
All problems
EasyAlgorithmsal-217

Toeplitz Matrix Check

Problem

Return True if every diagonal of the matrix running from top-left to bottom-right is constant, and False otherwise.

Compare each cell with its upper-left neighbour, skipping the first row and column. Empty and single-row matrices are toeplitz by definition.

Hint

Cell (r, c) must equal cell (r - 1, c - 1).

Test cases

case 1
in: [[[1,2,3,4],[5,1,2,3],[9,5,1,2]]]
expected: true
case 2
in: [[[1,2],[2,2]]]
expected: false
case 3
in: [[[1]]]
expected: true
case 4
in: [[]]
expected: true
case 5
in: [[[1,2]]]
expected: true
Starter codePython
def is_toeplitz(matrix):
    # Your code here
    pass

5 test cases · run your code against them in the editor