I have a matrix or list of lists:
x = [[1,2,3],
[2,3,1],
[3,1,2]]
My goal is to check whether
1) Each column of the matrix contains, each of the whole numbers from 1 to n exactly once.
2) Each row of the matrix contains each of the whole numbers from 1 to n exactly once.
This is the problem exercise that I encountered when I was solving Udacity's intro to programming course. This is my solution to the problem. I know that this is long and inefficient. So what is the short and efficient way to do this problem??
def check(p):
j = 0
for e in p:
i = 1 + j
s = str(p)
if s.find('.')!= -1:
return False
while i < len(p):
if p[i] == e:
return False
if p[i] > len(p) or p[i] < 1:
return False
i += 1
j += 1
return True
def check_sudoku(p):
i = 0
z = []
a = []
x = 0
for e in p:
r = check(e)
if r == False:
return r
#Below here is to transpose the list
while x < len(p):
z.append(1)
x += 1
while i < len(p):
for e in p:
a.append(e.pop())
z[i] = a
i += 1
a = []
#Below here is to check the transpose
for g in z:
r = check(g)
if r == False:
return r
return True