-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymmetric.py
46 lines (36 loc) · 1.08 KB
/
symmetric.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
41
42
43
44
45
46
# 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.
# 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.
def symmetric(x):
for row in x:
if len(row) != len(x):
return False
y = []
for p in range(len(x)):
y.append([q[p] for q in x])
if x == y:
return True
return False
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