HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Symmetric Square

Submitted by: @import:stackexchange-codereview··
0
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

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 True


Tests & 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([])
#>>> False

Solution

You could use 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 loop


We have

for i in range(master_count):
    for j in range(master_count):
        if master[i][j] != master[j][i]:
            return False


Actually 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 False


Alternatively, 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 loop
for i in range(master_count):
    for j in range(master_count):
        if master[i][j] != master[j][i]:
            return False
for i in range(master_count):
    for j in range(i + 1, master_count):
        if master[i][j] != master[j][i]:
            return False
return 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.