patternpythonMinor
Symmetric Square
Viewed 0 times
symmetricsquarestackoverflow
Problem
I was given an assignment to ensure that a list was symmetric.
I built it in a way that worked and it passed the tests. But I wanted to see if it could've been written more efficiently and still readable.
Original question
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.
My solution
Tests & expected responses
I built it in a way that worked and it passed the tests. But I wanted to see if it could've been written more efficiently and still readable.
Original question
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.
My solution
def symmetric(master):
master_count = len(master)
if not master: # checks to see if the list is empty
return True
if master_count != len(master[0]): #check to make sure the list is a square
return False
i = 0
while i < master_count:
j = 0
while j < master_count:
print str(master[i][j]) +" "+str(master[j][i])
if master[i][j] != master[j][i]:
return False
j = j + 1 # close the loop
i = i + 1 # close the loop
return TrueTests & expected responses
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([])
#>>> FalseSolution
You could use
So instead of
We have
Actually we're doing twice the amount of work we need to. If
Alternatively, you could use
I would also reconsider the variable names, e.g.
range (or xrange for Python 2) to make the code shorter and clearer.So instead of
i = 0
while i < master_count:
j = 0
while j < master_count:
print str(master[i][j]) +" "+str(master[j][i])
if master[i][j] != master[j][i]:
return False
j = j + 1 # close the loop
i = i + 1 # close the loopWe have
for i in range(master_count):
for j in range(master_count):
if master[i][j] != master[j][i]:
return FalseActually we're doing twice the amount of work we need to. If
master[i][j] == master[j][i], we don't need to check the opposite:for i in range(master_count):
for j in range(i + 1, master_count):
if master[i][j] != master[j][i]:
return FalseAlternatively, you could use
all and a generator expression:return all(master[i][j] == master[j][i]
for i in range(master_count)
for j in range(i + 1, master_count))I would also reconsider the variable names, e.g.
matrix instead of master, dim or n instead of master_count.Code Snippets
i = 0
while i < master_count:
j = 0
while j < master_count:
print str(master[i][j]) +" "+str(master[j][i])
if master[i][j] != master[j][i]:
return False
j = j + 1 # close the loop
i = i + 1 # close the loopfor i in range(master_count):
for j in range(master_count):
if master[i][j] != master[j][i]:
return Falsefor i in range(master_count):
for j in range(i + 1, master_count):
if master[i][j] != master[j][i]:
return Falsereturn all(master[i][j] == master[j][i]
for i in range(master_count)
for j in range(i + 1, master_count))Context
StackExchange Code Review Q#80101, answer score: 7
Revisions (0)
No revisions yet.