Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2015
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. #!/usrbin/env python
  2.  
  3. def getMaxRowLength( matrix ):
  4. maxLen = 0
  5. for i in matrix:
  6. if len(i) > maxLen:
  7. maxLen = len(i)
  8. return maxLen
  9.  
  10. def computeDiag( matrix, i , j ):
  11. result = matrix[i][j]
  12. isComputing = True
  13. while isComputing:
  14. j -= 1
  15. i += 1
  16. if j < 0:
  17. isComputing = False
  18. elif i < len(matrix) and j < len( matrix[i] ):
  19. result += matrix[i][j]
  20. return result
  21.  
  22. if __name__ == "__main__":
  23. matrix = [
  24. [1, 2, 3, 4],
  25. [5, 6, 7, 8],
  26. [9, 8, 7, 3] ]
  27. counter = 0
  28. rowIndex = 0
  29. colIndex = 0
  30. result = []
  31. maxLen = getMaxRowLength( matrix )
  32.  
  33.  
  34. for colIndex in range( 0, maxLen ):
  35. result.append( computeDiag( matrix, rowIndex , colIndex ) )
  36.  
  37. rowIndex += 1
  38.  
  39. for rowIndex in range( rowIndex, len( matrix ) ):
  40. result.append( computeDiag( matrix, rowIndex , colIndex ) )
  41.  
  42. print result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement