Skip to content

dibs: I will implement exercise Matrix - #244

Merged
stkent merged 4 commits into
exercism:masterfrom
FridaTveit:MatrixExercise
Jan 19, 2017
Merged

stkent merged 4 commits into
exercism:masterfrom
FridaTveit:MatrixExercise

Conversation

@FridaTveit

Copy link
Copy Markdown
Contributor

No description provided.

@jtigger

jtigger commented Jan 11, 2017

Copy link
Copy Markdown
Contributor

Go Frida, go!

@jtigger

jtigger commented Jan 13, 2017

Copy link
Copy Markdown
Contributor

Hey @FridaTveit, would you mind connecting with me over email (on my GitHub profile)? I have an Exercism-related question for you.

@stkent

stkent commented Jan 15, 2017

Copy link
Copy Markdown
Contributor

I'm curious to see what you choose to do with the problem interface here, @FridaTveit - I have pondered this puzzle a little in the past!

@jtigger

jtigger commented Jan 15, 2017

Copy link
Copy Markdown
Contributor

You've pondered the puzzle of this problem's protocol? (sorry, couldn't help myself... carry on)

@stkent

stkent commented Jan 15, 2017

Copy link
Copy Markdown
Contributor

Hah, indeed! In preparation for pontification!

@FridaTveit

Copy link
Copy Markdown
Contributor Author

I added a commit with the implementation a while ago but I rebased it on top of master instead of merging it, is that a problem?

@stkent

stkent commented Jan 16, 2017 •

Copy link
Copy Markdown
Contributor

Ah, no, that's perfectly fine, I had just missed it! I normally update the PR title to exercise-slug: add to track once I have an implementation that's ready for review, just to call out the difference :)

@FridaTveit

Copy link
Copy Markdown
Contributor Author

Ah, okay :)

@jtigger

jtigger commented Jan 16, 2017

Copy link
Copy Markdown
Contributor

Hey @stkent I'm assuming that you're reviewing this PR; that your intent?

@stkent

stkent commented Jan 16, 2017

Copy link
Copy Markdown
Contributor

Sure, I'm happy to; probably won't get to it for a couple days though, so if you want to jump in, that's fine by me.

@stkent stkent self-assigned this Jan 18, 2017
String[] columnValues = rows[i].split(" ");
matrix[i] = new int[columnValues.length];
for (int j = 0; j < columnValues.length; j++) {
matrix[i][j] = Integer.valueOf(columnValues[j]);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor detail, but it's probably ok to use Integer.parseInt here instead, which avoid extra boxing/unboxing. See e.g. this SO post for some interesting discussion. Not going to hold the PR for this change alone, just noting for interest :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, will change it! :)

String[] rows = matrixAsString.split("\\n");
matrix = new int[rows.length][];
for (int i = 0; i < rows.length; i++) {
String[] columnValues = rows[i].split(" ");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another FYI, not a required change: note that there are some "gotchas" when using String.split with a regex. Here's a nice breakdown. If this was high-performance code, you'd probably want to compile the Pattern ahead of time, and split using that instead. I only learned about this a couple months back.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, good to know! :)

return Arrays.asList(new Object[][]{
{"0", new int[] {0}},
{"0 1", new int[] {0, 1}},
{"0\\n1", new int[] {0}},

@stkent stkent Jan 18, 2017 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these strings all need to include the escaped newline? Everything seemed to work ok for me if I replaced all occurrences of \\n with a simple \n? On a related note, would that change make the matrixAsString.replaceAll("\\\\n", "\n") lines removable too?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohh... is this for formatting the parametrized test output? If so, that's a tricky balance; I fear adding then adjusting the newlines in this way might cause implementors some confusion.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is for formatting the test output and I was unsure about it. I agree that adjusting the newlines looks confusing when you look at the code. So having the test output look a bit less nice is probably the better option. I'll change it :)

{"0 1", new int[] {0}},
{"0\\n1", new int[] {0, 1}},
{"0 1\\n2 3", new int[] {0, 2}},
{"0 1 2\\n3 4 5\\n6 7 8", new int[] {0, 3, 6}}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it might be possible to format the matrices here in a more intuitive way? Something like the following (last row example):

@Parameterized.Parameters(name = "{index}: checking first row of matrix constructed with string \"{0}\".")
public static Collection<Object[]> data() {
    return Arrays.asList(new Object[][]{
            {"0", new int[] {0}},
            {"0 1", new int[] {0, 1}},
            {"0\n1", new int[] {0}},
            {"0 1\n2 3", new int[] {0, 1}},
            {  "0 1 2\n"
             + "3 4 5\n"
             + "6 7 8", new int[] {0, 1, 2}}
    });
}

I find it a little easier to draw the connection between the input and the expected output with that formatting, but others might disagree...?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, that looks good, I'll change it :)

this.firstColumn = firstColumn;
}

@Ignore

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these @Ignores could also be applied so the inner classes themselves instead of the nested @Test methods, which could make them a little easier to spot? e.g.

@Ignore
@RunWith(Parameterized.class)
public static class GetLastRowTest {

As before, though, it might just be me that finds the top-level annotation easier to see!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, that makes sense, I'll change it :)

@Ignore
@Test
public void getLastColumnTest() {
assertArrayEquals(matrix.getColumn(matrix.getColumnsCount() - 1), lastColumn);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JUnit assertions typically request the expected value before the actual value, so we should reverse the parameter ordering of all of these calls to assertArrayEquals - otherwise our failure messages will be backwards and potentially confusing!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, will change it! :)

@stkent

stkent commented Jan 18, 2017

Copy link
Copy Markdown
Contributor

@FridaTveit some comments in; mostly musing out loud 😄 . Let me know what you prefer in the cases where I suggested alternatives!

…gnore in MatrixExercise. Changed Integer.valueOf to Integer.parseInt in Matrix. Changed String.split to using Pattern in Matrix.

@Test
public void getLastRowTest() {
assertArrayEquals(lastRow, matrix.getRow(matrix.getRowsCount() - 1));

@stkent stkent Jan 18, 2017 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this test uses getRowsCount to identify the final row of the matrix, I wonder if we should move the test classes CountColumnsTest and CountRowsTest to the very beginning of the test suite? Otherwise those test classes are effectively redundant, as the implementor will already have had to build working implementations of getRowsCount and getColumnsCount to pass earlier tests (like this one).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, will move them! :)

@stkent

stkent commented Jan 18, 2017

Copy link
Copy Markdown
Contributor

@FridaTveit thanks, changes look great! I added one more question that I didn't spot the first time around - let me know what you would like to do with that. Once that's been discussed and/or resolved, let's get this merged!

@stkent

stkent commented Jan 18, 2017

Copy link
Copy Markdown
Contributor

(Small merge with master also needed now :)

@stkent

stkent commented Jan 19, 2017

Copy link
Copy Markdown
Contributor

I took care of those config conflicts using the new inline GitHub merge tool. It worked well! Merging on successful build 🏆

@stkent
stkent merged commit 63020d4 into exercism:master Jan 19, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants