解题思路:
org_list1 = [[1, 2, 3, 4],[5, 1, 2, 3],[9, 5, 1, 2]]
org_list2 = [[1, 2],[2, 2]]
from numpy import * #调用numpy模块,将数据转换成矩阵进行运算
def q356(org_list):
copy_list = org_list[:]
org_mat = mat(copy_list)
demo = (org_mat.shape)
#当矩阵行列中有一个为1时候,判定为TRUE
if min(demo) == 1:
return True
count = max(demo) - min(demo)
if demo[0] > min(demo):
#行数大于列数,进行行切片操作
num = demo[1]
i = 0
while i < count + 1:
mat1 = org_mat[i:num+i] #行切片操作
mat2 = fliplr(mat1) #镜像矩阵
mat5_1 = hstack((mat1,mat2))
mat3 = mat1.T #转置矩阵
mat4 = fliplr(mat3)
mat5_2 = hstack((mat4,mat3))
mat5 = vstack((mat5_2,mat5_1)) #矩阵合并,列合并:vstack,行合并:hstack
if (mat5 == mat5.T).all():
i += 1
continue
else:
return False
return True
else:
#列数大于行数,进行列切片操作
num = demo[0]
i = 0
while i < count + 1:
mat1 = org_mat[:,i:num+i] #列切片操作
mat2 = fliplr(mat1) #镜像矩阵
mat5_1 = hstack((mat1,mat2))
mat3 = mat1.T #转置矩阵
mat4 = fliplr(mat3)
mat5_2 = hstack((mat4,mat3))
mat5 = vstack((mat5_2,mat5_1)) #矩阵合并,列合并:vstack,行合并:hstack
if (mat5 == mat5.T).all():
i += 1
continue
else:
return False
return True
if __name__ == '__main__':
print(q356(org_list1))
print(q356(org_list2))
输出结果:True
False
|