Skip to content

Add exercise tree-building - #1642

Merged
FridaTveit merged 5 commits into
exercism:masterfrom
junming403:master
Apr 23, 2019
Merged

FridaTveit merged 5 commits into
exercism:masterfrom
junming403:master

Conversation

@junming403

Copy link
Copy Markdown
Contributor

Implement exercise tree building


Reviewer Resources:

Track Policies

@junming403
junming403 force-pushed the master branch 4 times, most recently from 317b46e to 4c77f98 Compare March 19, 2019 14:49
@junming403 junming403 changed the title [WIP]Add exercise tree-building Add exercise tree-building Mar 19, 2019
@junming403

Copy link
Copy Markdown
Contributor Author

Add new exercise tree-building, ready for review.

@junming403

Copy link
Copy Markdown
Contributor Author

@FridaTveit Hi can you help me review this? Thank you

@FridaTveit

Copy link
Copy Markdown
Contributor

Thanks for wanting to add a new exercise @junming403, we really appreciate it! 🙂

Anyone in the @exercism/java group can review it. However, we're all doing this as volunteers so please be patient. We'll review it as soon as we have time 🙂

Thanks again for wanting to contribute! 🙂

@junming403

junming403 commented Mar 20, 2019 •

Copy link
Copy Markdown
Contributor Author

Thanks for wanting to add a new exercise @junming403, we really appreciate it! 🙂

Anyone in the @exercism/java group can review it. However, we're all doing this as volunteers so please be patient. We'll review it as soon as we have time 🙂

Thanks again for wanting to contribute! 🙂

My apologies.

@junming403

Copy link
Copy Markdown
Contributor Author

Any feedback regarding if I am doing it in the right way will be appreciated.

@junming403

Copy link
Copy Markdown
Contributor Author

@FridaTveit Sorry to bother again, can I ask generally what is the expected time to get some feedback for an opened PR? I was actually planning to add more exercises if everything went well.

@FridaTveit

Copy link
Copy Markdown
Contributor

@junming403 I'm afraid we don't have an expected time to get round to reviewing. We all have either full time jobs or are in full time education. We always try to review things as soon as possible, but it can be hard to find time especially if it's a whole new exercise (as that takes longer to review) and at busy times (I'm moving house at the moment). But one of us will get back to you as soon as we can 🙂

If you're eager to do other things in the meantime you can always start another exercise on this track, or update the test suite of any exercise on this track so that it's up to date with the canonical data, contribute to any other language track or contribute to the problem-specifications repo. We do appreciate all contributions, even if it takes as a while to review them 🙂

@junming403

Copy link
Copy Markdown
Contributor Author

Thank you for the prompt reply. I am sorry if the ask sounds like an urge. I am not really mean to urge for the review, just eager to contribute more as I am really interested in doing this : )

@FridaTveit

Copy link
Copy Markdown
Contributor

That's great, I'm glad you want to contribute! 🙂 And I'm sorry it's taking us a while to review this, we will try to get to it as soon as possible! 🙂

@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.

Thank you for adding this exercise @junming403, it's looking good! I've had a quick look and left a few comments. Let me know if you have any questions about anything and I'll try to get back to you as soon as possible 🙂

Comment thread config.json Outdated
"slug": "tree-building",
"uuid": "aeaed0e4-0973-4035-8bc5-07480849048f",
"core": false,
"unlocked_by": null,

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 needs to be set. In general we try to set it to an exercise with similar topics 🙂

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.

Any suggestions for what it should be set to?

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.

Maybe linked-list? 🙂

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.

Also, this should be moved to where the other difficulty 7 exercises are in this file 🙂

import java.util.Comparator;
import java.util.HashMap;

public class BuildTree {

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 class and its the buildTree method can probably be package private instead of public? 🙂

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 still think this can be package private (so you can just omit the access modifier) 🙂

private String message;

public InvalidRecordsException(String msg) {
this.message = msg;

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 you can just do super(msg) here. Then you don't need the private parameter or to override the getMessage method 🙂

Also, minor thing, but message might be a better name for the method parameter. I prefer to avoid abbreviations where possible as it can make the code just a little bit harder to read 🙂

@@ -0,0 +1,72 @@
import java.util.ArrayList;

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 should not contain the entire implementation. Please see some of the other exercises as an example and see the instructions in our contributing guide 🙂

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 but i notice in the problem specification, this exercise is a bit special, it is about Refactor a tree building algorithm. Thus in general a naive solution would be given and the task is to refactor it into a more elegant and efficient solution.

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.

Ah, fair enough! I missed that, sorry 🙂

assertNull(root);
}

@Test

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.

All tests except the first one should be ignored, as per our policies 🙂

}

@Test
public void test_recordsWithMoreThanTwoChildren() throws InvalidRecordsException {

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.

Please use camel case in naming 🙂

assertNodeIsLeaf(root.children.get(1).children.get(0), 6);
}

@Test(expected = InvalidRecordsException.class)

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.

Please use ExpectedException as per our policies 🙂

Comment thread config.json Outdated
"uuid": "aeaed0e4-0973-4035-8bc5-07480849048f",
"core": false,
"unlocked_by": null,
"difficulty": 4,

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 this exercise is harder than 4. Maybe 7 would be a better difficulty? 🙂

@junming403

Copy link
Copy Markdown
Contributor Author

Thank you for the review @FridaTveit , I have updated and ready for the next round of review!

Any suggestions for what should be a proper prerequisite for this exercise?

@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.

Nice markups, thank you! 😄

I've left a few more comments, let me know if you have any questions about anything 🙂

Comment thread config.json Outdated
"slug": "tree-building",
"uuid": "aeaed0e4-0973-4035-8bc5-07480849048f",
"core": false,
"unlocked_by": null,

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.

Also, this should be moved to where the other difficulty 7 exercises are in this file 🙂

import java.util.Comparator;
import java.util.HashMap;

public class BuildTree {

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 still think this can be package private (so you can just omit the access modifier) 🙂


public class BuildTree {

protected TreeNode buildTree(ArrayList<Record> records) throws InvalidRecordsException {

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.

Why is this set to protected? 🙂

@@ -0,0 +1,72 @@
import java.util.ArrayList;

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.

Ah, fair enough! I missed that, sorry 🙂


public class TreeNode {
private int nodeId;
public ArrayList<TreeNode> children;

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.

It's best practice to use getters and setters instead of making fields public 🙂

import org.junit.Test;
import org.junit.rules.ExpectedException;


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 nitpick: you don't need this extra empty line 🙂

test.buildTree(records);
}

private void assertNodeIsLeaf(TreeNode node, int nodeId) {

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.

It looks like this doesn't just check if the node is leaf, it also checks the node id. This is a bit unexpected and also doesn't follow the rule that a method should only do one thing. So I would keep this method as just checking if a node is a leaf, and just check the node id inline in each test 🙂 What do you think? 🙂

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.

Good point! I have inlined node id check

assertNodeIsLeaf(root.children.get(0), 1);
assertNodeIsLeaf(root.children.get(1), 2);
assertNodeIsLeaf(root.children.get(2), 3);

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 don't need this extra empty line 🙂

assertEquals(node.children.size(), 0);
}

private void assertNodeIsBranch(TreeNode node, int nodeId, int childrenCount) {

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.

Same for this, I would move the node id check out of this method. And I would rename it to something like assertNodeIsBranchWithNNumberOfChildren. Otherwise it's not obvious that it also checks the number of children 🙂

@@ -0,0 +1,17 @@
public class Record {

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.

Everything here (and in general) that's public could probably also be package private 🙂

@junming403

Copy link
Copy Markdown
Contributor Author

Hi, thank you for the review, I have updated accordingly and ready for next round!

@junming403

Copy link
Copy Markdown
Contributor Author

Hi sorry for the mistake, I have fixed the lint error now it passes the check. Can help me review again see if everything is okay?

@junming403

Copy link
Copy Markdown
Contributor Author

@FridaTveit Hi sorry for the nagging, would you mind help me review this again if you get time? Thank you.

@junming403

Copy link
Copy Markdown
Contributor Author

Can anyone help me review 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 is looking really good @junming403, just a couple of small changes to make and then it's ready to merge 🙂

Comment thread config.json Outdated
"slug": "tree-building",
"uuid": "aeaed0e4-0973-4035-8bc5-07480849048f",
"core": false,
"unlocked_by": null,

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 still needs to be filled in. I think I suggested linked-list before, what do you think about that? 🙂


@Ignore("Remove to run test")
@Test
public void testCycleInDirectly() throws InvalidRecordsException {

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 this should be testCycleIndirectly 🙂

@junming403

Copy link
Copy Markdown
Contributor Author

@FridaTveit Thank you so much for the review, I have done the necessary changes!

@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, thanks! 🙂

Thanks for adding this exercise @junming403!

@FridaTveit
FridaTveit merged commit f2944d0 into exercism:master Apr 23, 2019
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