0

I'm working on an exercise on the Exercism website, and basically what it requires is that you create a Matrix object that takes an argument of the form '1 2 3\n4 5 6\n7 8 9' and then has two methods (row and column) that returns the numbers in that specific matrix's rows and columns (given an argument for which row or column to return). Here is my code, which already works:

class Matrix:
    def __init__(self, matrix_string):
        self.matrix_rows = []
        for row in matrix_string.split('\n'):
            self.matrix_rows.append(row.split(' '))

    def row(self, index):
        return [int(num) for num in self.matrix_rows[index - 1]]

    def column(self, index):
        return [int(num) for num in list(zip(*self.matrix_rows))[index - 1]]

But I got some feedback from one of the website mentors that said this:

Nice job with your solution!

Both L8 and L11 require cleaning to turn strings into ints. Is there a better place to do this work?

Can self.matrix_rows be created inline, i.e. without the loop on L4?

I'm still thinking about the first comment (L8 and L11), but I'm confused on the second comment about L4, because I'm not sure what he means by creating self.matrix_rows "inline." I assumed it was a term with a specific meaning, and I found this thread: How to create inline objects with properties in Python? but I'm not sure if that's the same thing as my question. I don't want to create an inline object, but just an inline property of an object...I think?

Any suggestions that would help me decrypt his comments would be appreciated!

John Salerno
  • 83
  • 2
  • 7
  • 1
    This really is a question better suited for the person who gave you this feedback. Perhaps they meant a list comprehension, in which case your init would simply be `self.matrix_rowd = [row.split(' ') for row in matrix_string.split('\n')]`, but that is a mere matter of style. As for the previous comments, they are telling you that you int conversion should happen when you parse the string in `__init__` – juanpa.arrivillaga Feb 22 '20 at 23:21
  • Thanks very much! It just takes so long for the mentors to respond (it took more than a week for them to give me this feedback) that I was worried it would take just as long if I asked more questions. I'll try what you suggested and see what he says. Thanks! – John Salerno Feb 23 '20 at 04:44
  • Ok, I'm a little stuck again with the int conversion. If I have a one line comprehension to create `self.matrix_rows`, I'm not sure where I need to do the int conversion, or at what point I actually have access to the individual strings that need to be converted. Adding more lines seems more verbose than how I have it. – John Salerno Feb 23 '20 at 05:12
  • 1
    something like: `self.matrix_rowd = [[int(x) for x in row.split(' ')] for row in matrix_string.split('\n')]`, in which case, your `row` implementation just becomes `return self.matrix_rows[index - 1]` – juanpa.arrivillaga Feb 23 '20 at 05:52
  • Oh wow, I didn't realize you could do nested for loops in a comprehension like that. I actually tried it earlier, but I must have written it wrong! Thanks! – John Salerno Feb 23 '20 at 06:01
  • 1
    Note, for-loops are often great at providing expressive code for mapping/filtering transformations, but the equivalent for-loop is fine. – juanpa.arrivillaga Feb 23 '20 at 06:02

0 Answers0