Skip to content

run-length-encoding: add to track - #522

Merged
FridaTveit merged 15 commits into
exercism:masterfrom
Smarticles101:run-length-encoding
May 12, 2017
Merged

FridaTveit merged 15 commits into
exercism:masterfrom
Smarticles101:run-length-encoding

Conversation

@Smarticles101

@Smarticles101 Smarticles101 commented May 9, 2017 •

Copy link
Copy Markdown
Member

Lot's of free time with the last couple weeks of school on my hands and summer coming up, plan to implement a bunch of exercises. 😁

Comment thread config.json Outdated
"difficulty": 5,
"topics": []
},
{

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.

Lol oops

@Smarticles101 Smarticles101 changed the title run-length-encoding: dibs on implementing run-length-encoding: add to track May 9, 2017

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

Thanks @Smarticles101, it looks really good! :) I've left a few comments which it would be great if you could look at when you have time :)

}

if (amt == 1) encodedData += c;
else encodedData += amt + Character.toString(c);

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 would probably be a good idea to put curly brackets around each branch, i.e.

if (amt == 1) {
  encodedData += c;
} else {
  encodedData += amt = Character.toString(c)
}

You could also use StringBuilder to make appending to the string a bit easier (and it would be more performant) :)

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.

Cool! I've never heard about StringBuilder before, it seems like it would be a lot better though considering that concatenating a string creates a new string every time

public void encodeEmpty() {
Assert.assertEquals("", runLengthEncoding.encode(""));
}

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.

There should be an @Ignore here :)

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.

Are you sure? Other tests I have looked at leave the @Ignore off of the first/simplest 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.

It should only be the first test which doesn't have an @Ignore. Youv'e left it off the first two (my comment was meant for the second test, sorry if that wasn't clear) :)

for (int i = 0; i < data.length(); i++) {
char c = data.charAt(i);

if (i != 0 && c == data.charAt(i - 1)) continue;

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 should probably put curly brackets around continue and put it on a new line. That's best practice as it avoids mistakes :)

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.

Good idea. I try to keep the body of code on the same line as the header if it's only one line and doesn't require curly braces. This way, if I add more code later, it's less likely for me to have problems because I will see that there are no curly braces. I suppose though, that it's better just to insert the curly braces anyways. I think that it's also more common to see it that way.

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.

Yes, I agree, it's not so bad when it's on the same line as the header. But it's probably still a bit clearer to put it on it's own line with curly brackets. That way it's easier to see that it's an if statement :)

public String encode(String data) {
String encodedData = "";

for (int i = 0; i < data.length(); i++) {

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 find it a bit difficult to understand what's happening in this mehtod with the mulitple for loops. Maybe it would be more readable to just use one for loop? You could keep track of the current character and the current count for that character. When you reach a character that's different, you add the previous character and the count to the encoded string. What do you think?

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 that's a great idea! I was actually thinking of writing it this way after I had finished it. Implementing it this way would increase readability a lot.


matcher.find();

while (number-- != 0) {

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.

Although it's short and neat, changing the value of a variable in the same statment as comparing it can easily get confusing. I would recommend using a for loop (e.g. for (; number > 0; number--)) or decrementing the variable inside the while loop.

}

public String decode(String encodedData) {
String decodedData = "";

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.

Again, it could be a good idea to use a StringBuilder :)

}

@Test
public void decodeEmpty() {

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 test data puts all the tests for encoding before the tests for decoding. I think this is a good idea as it lets the person implementing it focus on one method at a time :)

public void decodeWithOnlySingleValues() {
Assert.assertEquals("XYZ", runLengthEncoding.decode("XYZ"));
}

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 appear to be missing a test for "string with no single character" from the test data. It's a good idea to implement all the tests in the test data :)

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.

Guess I missed that. Oops

Comment thread config.json Outdated
"topics": []
},
{
"slug": "run-length-encoding",

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.

Up until now we've added new exercises at the end of the track with a difficulty 1. We've then discussed which difficulty to give them in a separate issue. Should we keep doing that @exercism/java? Or should we estimate the difficulty when we add the exercise?

Test ordering fixed.
Example uses StringBuilders
Example uses one loop encode method
added braces to one-line if statements
etc.
@Smarticles101

Copy link
Copy Markdown
Member Author

Take a look at my new encode method. It's all in one loop, but it feels slightly 'messier' to me. What's your opinion @FridaTveit

Also, I think I somehow screwed up the files somehow because its saying I changed every single file when I run git status. I ran git diff and saw that ^M was at the end of every line, which is apparently some sort of newline character. Anyone know how to get rid of all these?

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

Thanks for making those changes @Smarticles101 :) It's looking great! :D I have a few more comments but most of them is just me being picky :P I've put a comment on config.json which should hopefully help with the ^M problem, let me know if it doesn't work :)

import java.util.regex.Pattern;

public class RunLengthEncoding {
public String encode(String data) {

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 a lot better! :) But I agree that it's still a bit more complicated than it needs to be. I've left a few comments which should hopefully help simplify it :)

public class RunLengthEncoding {
public String encode(String data) {

StringBuilder encodedData = new StringBuilder();

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.

encodedDataBuilder maybe, to make it clear that it's a builder?

return data;
}

int charCount = 0;

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 would set the charCount to 1 and count the first char as already seen. Also maybe call it beforeCharCount to make it more clear what you're counting? Another small thing (sorry for nitpicking...) is that I would prefer currentChar and nextChar to beforeChar and currentChar. It doesn't matter very much, I just think it makes it a bit more readable. Especially currentCharCount I think is a bit more readable than beforeCharCount :)

Or you could use previousChar instead of beforeChar :)

int charCount = 0;
char beforeChar = data.charAt(0);

for (int i = 0; i < data.length(); i++) {

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 can start this loop from 1 :)


matcher.find();

for (; number != 0; --number) {

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.

number-- is normally used in for loops :)

@Ignore("Remove to run test")
@Test
public void encodeWithMixedValues() {
Assert.assertEquals("12WB12W3B24WB", runLengthEncoding.encode("WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB"));

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 would put each argument on a new line so that the line isn't so long :)

@Ignore("Remove to run test")
@Test
public void decodeWithMixedValues() {
Assert.assertEquals("WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB", runLengthEncoding.decode("12WB12W3B24WB"));

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 here, each argument on a new line should make it fit better :)

Comment thread config.json
@@ -1,354 +1,359 @@
{

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 doing git config --global core.autocrlf true might help (from this site). I haven't tried it myself though, as I haven't encountered that problem. Let me know if it doesn't work! :)


for (int i = 0; i < data.length(); i++) {
char currentChar = data.charAt(i);

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 there are too many ifs here. From what I understand, you only really need to do two things; if beforeChar == currentChar then you need to increment the count. If beforeChar != currentChar or this is the last char in the string, then we need to add the latest char with count to the string. So something like (in pseudocode):

if (beforeChar == currentChar) {
  increment count
}

if (beforeChar != currentChar || reached end of string) {
  add beforeChar with count
  if beforeChar != currentChar && reached end of string then add currentChar with count 1
  beforeChar = currentChar
  charCount = 1
}

Extracting the adding char with count to its own method would also simplify it :)

What do you think? :)

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 this is great. This was definitely the help I was looking for 👍

@FridaTveit

Copy link
Copy Markdown
Contributor

Oh, and of course if you disagree with any of my comments then feel free to say so or to leave them, they're just suggestions :)

Made a for loop more typical.
Test assertion parameters are on seperate lines.

@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 really great now!! :D Thanks @Smarticles101 :) The only thing is the ^M problem in config.json and then it's ready to merge :) Let me know if you need any more help :)

@Smarticles101

Copy link
Copy Markdown
Member Author

Yep @FridaTveit I think I've almost fixed that. I might add another pull request some time to add a .gitattributes file to ensure that the right eol characters are used so that no one else has this problem.

}

return encodedData.toString();
countOfChar.append(toAdd);

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 why the build is failing. It should be toAddTo.append(toAdd). Sorry I didn't spot it before!

@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 great @Smarticles101! :D Thank you for all your hard work! :)

@FridaTveit
FridaTveit merged commit 7db59e5 into exercism:master May 12, 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