# A list is symmetric if the first row is the same as the first column,
# the second row is the same as the second column and so on. Write a
# procedure, symmetric, which takes a list as input, and returns the
# boolean True if the list is symmetric and False if it is not.
#print symmetric([[1, 2, 3],
# [2, 3, 4],
# [3, 4, 1]])
#>>> True
#print symmetric([["cat", "dog", "fish"],
# ["dog", "dog", "fish"],
# ["fish", "fish", "cat"]])
#>>> True
#print symmetric([["cat", "dog", "fish"],
# ["dog", "dog", "dog"],
# ["fish","fish","cat"]])
#>>> False
#print symmetric([[1, 2],
# [2, 1]])
#>>> True
#print symmetric([[1, 2, 3, 4],
# [2, 3, 4, 5],
# [3, 4, 5, 6]])
#>>> False
#print symmetric([[1,2,3],
# [2,3,1]])
#>>> False
如题,是写代码判断是否是对称方阵,我写的代码如下,请问是哪里错了呢
def symmetric(p):
# Your code here
n = len(p)
i = 0
while i < n:
j = 0
while j< n:
if p[i][j]==p[j][i]:
j = j + 1
else:
return False
i = i + 1
return True
# the second row is the same as the second column and so on. Write a
# procedure, symmetric, which takes a list as input, and returns the
# boolean True if the list is symmetric and False if it is not.
#print symmetric([[1, 2, 3],
# [2, 3, 4],
# [3, 4, 1]])
#>>> True
#print symmetric([["cat", "dog", "fish"],
# ["dog", "dog", "fish"],
# ["fish", "fish", "cat"]])
#>>> True
#print symmetric([["cat", "dog", "fish"],
# ["dog", "dog", "dog"],
# ["fish","fish","cat"]])
#>>> False
#print symmetric([[1, 2],
# [2, 1]])
#>>> True
#print symmetric([[1, 2, 3, 4],
# [2, 3, 4, 5],
# [3, 4, 5, 6]])
#>>> False
#print symmetric([[1,2,3],
# [2,3,1]])
#>>> False
如题,是写代码判断是否是对称方阵,我写的代码如下,请问是哪里错了呢
def symmetric(p):
# Your code here
n = len(p)
i = 0
while i < n:
j = 0
while j< n:
if p[i][j]==p[j][i]:
j = j + 1
else:
return False
i = i + 1
return True