Symmetric matrices

Question

Given a square matrix of order nn, find out whether it is symmetric or not. Symmetric matrixes are of the form: [abcbdecef] \begin{bmatrix} a & b & c \\ b & d & e \\ c & e & f \end{bmatrix}

Input Format

The first line of the input contains an integer number nn which represents the number of rows and the number of columns. From the second line, take nn lines input with each line containing nn integer elements with each element separated by a space.

Output Format

Print ‘YES’ if matrix is symmetric else print ‘NO’.

Solution

Cool way

n = int(input())
l = []

for _ in range(n):
	l.append(list(map(int, input().split())))

print('YES' if all(l[i][j]==l[j][i] for i in range(n) for j in range(i)) else 'NO', end='')

Neat way

n = int(input())
l = []
flag = 1
for _ in range(n):
	l.append(list(map(int, input().split())))
for i in range(n):
    for j in range(i):
        if l[i][j]!=l[j][i]:
            flag=0
            break
print('YES' if flag else 'NO')