Advertisement
Guest User

matrix exercise

a guest
Aug 19th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. Given a string representing a matrix of numbers, return the rows and columns of that matrix.
  2.  
  3. So given a string with embedded newlines like:
  4. "1 2 3\n4 5 6\n7 8 9\n8 7 6"
  5.  
  6. your code should be able to spit out:
  7.  
  8. A list of the rows, reading each row left-to-right while moving top-to-bottom across the rows,
  9. A list of the columns, reading each column top-to-bottom while moving from left-to-right.
  10. The rows for our example matrix:
  11.  
  12. 9, 8, 7
  13. 5, 3, 2
  14. 6, 6, 7
  15. And its columns:
  16.  
  17. 9, 5, 6
  18. 8, 3, 6
  19. 7, 2, 7
  20. In this exercise you're going to create a class. Don't worry, it's not as complicated as you think!
  21.  
  22.  
  23. class Matrix(object):
  24.  
  25. def __init__(self, string):
  26. string = string.splitlines()
  27. self.setMatrix = [list(map(int,i.split())) for i in string]
  28.  
  29. def row(self, index):
  30. return self.setMatrix[index-1]
  31.  
  32. def column(self, index):
  33. col = [i[index-1] for i in self.setMatrix]
  34. return col
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement