Advertisement
Guest User

Matrix Spiral

a guest
Mar 20th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.73 KB | None | 0 0
  1. def spiral_copy(inputMatrix):
  2.   rowEnd = len(inputMatrix)
  3.   colEnd = len(inputMatrix[0])
  4.   rowStart = 0
  5.   colStart = 0
  6.   result = []
  7.  
  8.   while (rowStart < rowEnd and colStart < colEnd):
  9.    
  10.     for i in range(colStart, colEnd):
  11.       result.append(inputMatrix[rowStart][i])
  12.     rowStart+=1
  13.    
  14.     for i in range(rowStart, rowEnd):
  15.       result.append(inputMatrix[i][colEnd - 1])
  16.     colEnd-=1
  17.    
  18.     if (rowStart < rowEnd):
  19.       for i in range(colEnd - 1, colStart - 1, -1):
  20.         result.append(inputMatrix[rowEnd - 1][i])
  21.       rowEnd-=1
  22.      
  23.     if (colStart < colEnd):
  24.       for i in range(rowEnd - 1, rowStart - 1, -1):
  25.         result.append(inputMatrix[i][colStart])
  26.       colStart+=1
  27.      
  28.   return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement