Skip to content

transpose: add to track#604

Merged
FridaTveit merged 5 commits into
exercism:masterfrom
Smarticles101:transpose
Jun 24, 2017
Merged

transpose: add to track#604
FridaTveit merged 5 commits into
exercism:masterfrom
Smarticles101:transpose

Conversation

@Smarticles101

Copy link
Copy Markdown
Member

Implementing transpose
(test cases)


Reviewer Resources:

Track Policies

@Smarticles101 Smarticles101 changed the title transpose: dibs on implementing transpose: add to track Jun 22, 2017
@Smarticles101

Copy link
Copy Markdown
Member Author

Finally got around to doing this after I took a long break. I had some fun making the example solution for this. 😄

@FridaTveit FridaTveit left a comment

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.

This looks great @Smarticles101! :D I have a few comments and a couple of questions, but they're all minor things so if you feel you're done with this exercise then just say so or ignore the comments and I'll merge it in a few days :) Great work!!

Comment thread config.json Outdated
},
{
"slug": "transpose",
"difficulty": 1,

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.

We've decided that it would be a good idea if people who add exercises also estimate the difficulty. Since you implemented it, you probably know best :) If you don't fancy estimating it, don't worry about it. Just add it at the end of the track like this and we'll create an issue for estimating it :)

@@ -0,0 +1,41 @@
public class Transpose {
public String transpose(String toTranspose) {

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.

You could do public String[] transpose(String[] matrixToTranspose), and thereby use an array to encode the rows instead of newlines. But if you prefer to use a String then that's fine too :)


private void padRows(String[] rows) {
for (int i = 0; i < rows.length; i++) {
for (int j = i; j < rows.length; 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.

Could you pass in maxRowSize to this method and just do if (rows[i].length < maxRowSize instead of the second loop?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

No, and this is a bit tricky to see. I struggled with seeing this while making the solution. The problem description says the following:

Pad to the left with spaces.
Don't pad to the right.

You should only add padding in the case where there is a row to the right that is larger than it. Using the maxRowSize as you suggested works fine on all test cases except the last one, where the largest row is in the middle, but the last row is smaller, this causes the problem of extra spaces being inserted at which point you're technically padding to the right.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think whoever did this in the csharp track had the same idea, so when padding they just use the maxRowSize. However, their last test case is messed up, it has two extra spaces at the end where there shouldn't be. Ref this

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 see. Thanks for explaining that! :)

@Test
public void emptyString() {
String input = "";
String expected = "";

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.

You could maybe name them inputMatrix and transposedMatrix? But that's up to you, input and `expected is fine as well :)

public void twoCharsInARow() {
String input = "A1";
String expected = "A" +
"\n1";

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.

The indentation here makes this a bit difficult to read. Could you maybe try and align the rows of the matrices so it looks more like a matrix?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Agreed. IntelliJ keeps moving the indents back. 🤦‍♂️


@Test
@Ignore("Remove to run test")
public void simple() {

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 know you've got these names from the canonical data, so it's not your fault, but simple is not a very descriptive test name. Maybe transposeSimpleMatrix or simpleMatrix? While we should use the test data in the canonical data, we don't have to stick to the exact test names given in the description :)


for (int newRowOldCol = 0; newRowOldCol < maxRowSize; newRowOldCol++) {
for (int newColOldRow = 0; newColOldRow < rows.length; newColOldRow++) {
if (newRowOldCol < rows[newColOldRow].length()) {

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.

If newRowOldCol is less than maxRowSize (by the conditions of the for loop), would this condition ever not be true?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes. The way you're supposed to pad, you end up having rows on one or two test cases that are not the length of the longest row

@FridaTveit FridaTveit left a comment

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.

Looks great! :) It doesn't need starter implementation anymore now that it has an ordering (and it's ordered after the first exercises) but apart from that it's ready to merge! :) Thanks for taking the time to estimate the difficulty!


private void padRows(String[] rows) {
for (int i = 0; i < rows.length; i++) {
for (int j = i; j < rows.length; 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.

I see. Thanks for explaining that! :)

}
}

if (newRowOldCol != maxRowSize - 1) {

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.

From what I can see then maxRowSize is only being used to check whether you've reached the last new row? To simplify that you could store each new row in an array and then at the end do String.join("\n". newRows). You definitely don't have to do that though, it's just a suggestion :)

@@ -0,0 +1,5 @@
public class Transpose {

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.

According to the POLICIES doc, since this exercise is now after the first 20 exercises, it shouldn't have any starter implementation. Instead there should just be a .keep file in this directory :)

@Ignore("Remove to run test")
public void simpleMatrix() {
String input = "ABC\n" +
"123";

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.

These are very easy to read now. Great! :)

@FridaTveit FridaTveit left a comment

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.

Great! Thank you for this @Smarticles101, an excellent new exercise! :)

@FridaTveit FridaTveit merged commit 4853b11 into exercism:master Jun 24, 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.

2 participants