A Toeplitz (or diagonal-constant) matrix is a matrix in which each descending diagonal from left to right is constant, i.e., all elements in a diagonal are same.
Given a matrix A of order N X M your task is to complete the function isToeplitz which returns true if the matrix is Toeplitz otherwise returns false.
Example 1: 
Input: 3 3 6 7 8 4 6 7 1 4 6 
Output: 1
Explanation:
The test case represents a 3x3 matrix which looks like 
 6 7 8 
 4 6 7 
 1 4 6
Output: 1(True) as values in all diagonals are same.
Example 2:
Input:
2 3
1 2 3 4 5 6
Output: 0
Explanation: 
Matrix of order 2x3 will be 
     1 2 3
     4 5 6
Output: 0(False) as values in all diagonals 
are not same.
Your task: You don't need to read input or print anything. Your task is to complete the function isToepliz() which takes the matrix and size of matrix as input parameters and returns 1 if the matrix is a Toepliz matrix , otherwise return 0. 
Constraints: 
1<=N,M<=40 
1<=A[i][j]<=100
1<=i<=N,1<=j<=M
Expected time complexity:O(nxn)
Expected space complexity:O(1)

Source: geeksforgeeks | Difficulty: EASY
Tags: Matrices

Data structures

Starter code: # You are required to complete this method
# Return True/False or 1/0
def isToepliz(lis, n, m):
    #code here

Solutions:
def isToepliz(lis, n, m):
	for i in range(0, n - 1):
		for j in range(0, m - 1):
			if lis[i][j] == lis[i + 1][j + 1]:
				continue
			else:
				return 0
	return 1


def isToepliz(lis, n, m):
	rows = len(lis)
	sum = 0
	for i in range(rows):
		cols = len(lis[i])
		for j in range(cols):
			if i + 1 < rows and j + 1 < cols:
				if lis[i][j] != lis[i + 1][j + 1]:
					return False
	return True


def isToepliz(lis, n, m):
	flag = True
	for i in range(0, n - 1):
		for j in range(0, m - 1):
			if lis[i][j] != lis[i + 1][j + 1]:
				flag = False
				break
	if flag:
		return 1
	else:
		return 0
if __name__ == '__main__':
	t = int(input())
	for i in range(t):
		(n, m) = list(map(int, input().strip().split()))
		arr = list(map(int, input().strip().split()))


def isToepliz(lis, n, m):
	for i in range(n):
		for j in range(m):
			if i == 0 or j == 0:
				continue
			if lis[i][j] != lis[i - 1][j - 1]:
				return False
	return True


def isToepliz(lis, n, m):
	for i in range(n):
		for j in range(m):
			if i < n - 1 and j < m - 1:
				if lis[i][j] != lis[i + 1][j + 1]:
					return 0
	return 1


def checkDiag(mat, i, j, n, m):
	res = mat[i][j]
	i += 1
	j += 1
	while i < n and j < m:
		if mat[i][j] != res:
			return False
		i += 1
		j += 1
	return True

def isToepliz(lis, n, m):
	for j in range(m):
		if not checkDiag(lis, 0, j, n, m):
			return False
	for i in range(1, n):
		if not checkDiag(lis, i, 0, n, m):
			return False
	return True


def isToepliz(lis, n, m):
	for i in range(m):
		ele = lis[0][i]
		l = 1
		r = i + 1
		while l < n and r < m:
			if lis[l][r] != ele:
				return 0
			l += 1
			r += 1
	for i in range(1, n):
		ele = lis[i][0]
		l = i + 1
		r = 1
		while l < n and r < m:
			if lis[l][r] != ele:
				return 0
			l += 1
			r += 1
	return 1


def isToepliz(lis, n, m):
	for row in range(n - 1):
		for col in range(m - 1):
			if lis[row][col] != lis[row + 1][col + 1]:
				return False
	return True


def isToepliz(A, n, m):
	for i in range(n - 1):
		for j in range(m - 1):
			if A[i][j] != A[i + 1][j + 1]:
				return False
	return True


def isToepliz(a, n, m):
	for i in range(n):
		for j in range(m):
			(x, y) = (i, j)
			while x < n and y < m:
				if a[x][y] != a[i][j]:
					return False
				x += 1
				y += 1
	return True


def isToepliz(lis, n, m):
	for i in range(1, n):
		for j in range(1, m):
			if matrix[i][j] != matrix[i - 1][j - 1]:
				return False
	return True


def isToepliz(lis, n, m):
	n = len(lis)
	m = len(lis[0])
	for i in range(n - 1):
		for j in range(m - 1):
			if lis[i][j] != lis[i + 1][j + 1]:
				return False
	return True


def isToepliz(lis, n, m):
	l = []
	for i in range(n - 1):
		for j in range(m - 1):
			if lis[i][j] != lis[i + 1][j + 1]:
				return False
				break
	return True


def isToepliz(lis, n, m):
	b = {}
	for i in range(n):
		for j in range(m):
			if i == 0:
				b[-j] = lis[i][j]
			elif j == 0:
				b[i] = lis[i][j]
			elif b[i - j] != lis[i][j]:
				return False
	return True


def isToepliz(lis, n, m):
	k = min(n, m)
	for i in range(n - 1):
		for j in range(m - 1):
			if lis[i][j] != lis[i + 1][j + 1]:
				return 0
	return 1


def isToepliz(lis, n, m):
	r = len(lis)
	c = len(lis[0])
	for i in range(1, r):
		for j in range(1, c):
			if lis[i][j] != lis[i - 1][j - 1]:
				return 0
	return 1


def isToepliz(lis, n, m):
	rows = len(lis)
	cols = len(lis[0])
	for i in range(1, rows):
		for j in range(1, cols):
			if lis[i][j] != lis[i - 1][j - 1]:
				return 0
	return 1


def checki(lis, i, j):
	out = lis[i][j]
	i = i + 1
	j = j + 1
	while i < n and j < m:
		if lis[i][j] != out:
			return False
		i = i + 1
		j = j + 1
	return True

def isToepliz(lis, n, m):
	for i in range(n):
		if not checki(lis, i, 0):
			return False
	for j in range(m):
		if not checki(lis, 0, j):
			return False
	return True


def isToepliz(lis, n, m):
	dic = {}
	row = 0
	for col in range(m):
		dic[row - col] = lis[row][col]
	col = 0
	for row in range(n):
		dic[row - col] = lis[row][col]
	for i in range(n):
		for j in range(m):
			diag = i - j
			if lis[i][j] != dic[diag]:
				return 0
	return 1


def isToepliz(lis, n, m):
	n = len(lis)
	m = len(lis[0])
	map = {}
	for i in range(n):
		for j in range(m):
			key = i - j
			if key in map:
				if map[key] != lis[i][j]:
					return False
			else:
				map[key] = lis[i][j]
	return True


def isToepliz(lis, n, m):
	a = None
	for i in range(n - 1):
		for j in range(m - 1):
			if lis[i][j] != lis[i + 1][j + 1]:
				return False
	return True


def isToepliz(matrix, n, m):
	(r_len, c_len) = (len(matrix), len(matrix[0]))
	for r in range(1, r_len):
		for c in range(1, c_len):
			if matrix[r][c] != matrix[r - 1][c - 1]:
				return False
	return True


def isToepliz(lis, n, m):
	d = {}
	for i in range(n):
		for j in range(m):
			if i - j not in d:
				d[i - j] = lis[i][j]
			elif d[i - j] != lis[i][j]:
				return 0
	return 1


def isToepliz(lis, n, m):
	l = []
	for i in range(1, n):
		for j in range(1, m):
			if lis[i][j] == lis[i - 1][j - 1]:
				continue
			else:
				return False
	return True