-
Notifications
You must be signed in to change notification settings - Fork 0
/
L14Q1_Antisymmetric.py
40 lines (35 loc) · 1.03 KB
/
L14Q1_Antisymmetric.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# By Dimitris_GR from forums
# Modify Problem Set 31's (Optional) Symmetric Square to return True
# if the given square is antisymmetric and False otherwise.
# An nxn square is called antisymmetric if A[i][j]=-A[j][i]
# for each i=0,1,...,n-1 and for each j=0,1,...,n-1.
def antisymmetric(matrix):
#print len(matrix)
if len(matrix) != len(matrix[0]):
return False
i = 0
while i < len(matrix):
j = 0
while j < len(matrix):
if matrix[i][j] != (matrix [j][i]) * -1:
return False
j += 1
i += 1
return True
# Test Cases:
print antisymmetric([[0, 1, 2],
[-1, 0, 3],
[-2, -3, 0]])
#>>> True
print antisymmetric([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])
#>>> True
print antisymmetric([[0, 1, 2],
[-1, 0, -2],
[2, 2, 3]])
#>>> False
print antisymmetric([[1, 2, 5],
[0, 1, -9],
[0, 0, 1]])
#>>> False