you are given an N*N matrix. Write a program to check if the matrix is unique or not. A unique matrix is a matrix if every row and column of the matrix contains all the integers from 1 to N.
INPUT:
4
1 2 3 4
2 3 4 1
3 4 1 2
4 1 2 3
OUTPUT- True.
INPUT:
4
1 2 3 3
2 3 4 1
3 4 1 2
4 1 2 3
OUTPUT- False
def calculate_unique(matr, size):
ells = [i+1 for i in range(size)]
for i in range(size):
row = matr[i]
col = [matr[j][i] for j in range(size)]
for el in ells:
if (el not in row) or (el not in col):
return False
return True
n = int(input("n: "))
while n < 0:
n = int(input("n: "))
M = []
for i in range(n):
row = [int(i) for i in input(f"{i+1} line\n").split()]
while len(row) != n:
row = [int(i) for i in input(f"{i+1} line\n").split()]
M.append(row)
print(calculate_unique(M, n))
Comments
Leave a comment