Skip to content

Commit

Permalink
Merge pull request #102 from dsimandl/master
Browse files Browse the repository at this point in the history
The loops in the rotate90CW function and the __str__ function are now....
  • Loading branch information
gaylemcd committed Feb 23, 2015
2 parents 6d00f75 + e53a408 commit 2eb334c
Showing 1 changed file with 14 additions and 21 deletions.
35 changes: 14 additions & 21 deletions python/Chapter 1/Question1_6/ChapQ1.6.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,22 @@ def __init__(self, matrix):
self.matrix = matrix

def __str__(self):
rowstring=""
for row in self.matrix:
rowstring = rowstring + "["
for cell in row:
rowstring = rowstring + " " + str(cell) + " "
rowstring = rowstring + "]\n"
return rowstring

# Iterating over rows and the matrix via a generator,
# and joining the cell and then row into the final string output
return ''.join("%s%s%s" % ("[", ''.join("%s%s%s" % (" ", str(cell), " ")
for cell in row), "]\n") for row in self.matrix)

def rotate90CW(self):
#strategy is to make what are now rows into columns, and reverse the order, which mimics the effect of rotation on the matrix.
columnlist=[]
for row in self.matrix:
#set up a list for columns
columnlist.append([])
for row in self.matrix:
columnindex=0
for cell in row:
columnlist[columnindex].append(cell)
columnindex += 1
newmatrix=[]
for col in columnlist:
newmatrix.append(reversed(col))
self.matrix=newmatrix
# strategy is to make what are now rows into columns, and reverse the order,
# which mimics the effect of rotation on the matrix.
# This creates our empty list of columns by the length of the original matrix
columnlist = [[] for i in range(len(self.matrix))]
# Now this iterates through the rows and columns and appends those to the new columnlist
[[columnlist[i].append(row[i]) for i in range(len(row))] for row in self.matrix]
# reverses the new column list to get the order that is wanted, and assigned back to the original matrix
self.matrix = [reversed(col) for col in columnlist]

#testing

mat1= MatrixProcessor([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])
Expand Down

0 comments on commit 2eb334c

Please sign in to comment.