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!

6
  • 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__ Commented Feb 22, 2020 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! Commented Feb 23, 2020 at 4: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. Commented Feb 23, 2020 at 5: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] Commented Feb 23, 2020 at 5:52
  • 1
    Note, for-loops are often great at providing expressive code for mapping/filtering transformations, but the equivalent for-loop is fine. Commented Feb 23, 2020 at 6:02

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.