Repository navigation
run-length-encoding: add to track - #522
Conversation
| "difficulty": 5, | ||
| "topics": [] | ||
| }, | ||
| { |
FridaTveit
left a comment
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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) :)
There was a problem hiding this comment.
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("")); | ||
| } | ||
|
|
There was a problem hiding this comment.
There should be an @Ignore here :)
There was a problem hiding this comment.
Are you sure? Other tests I have looked at leave the @Ignore off of the first/simplest test.
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
You should probably put curly brackets around continue and put it on a new line. That's best practice as it avoids mistakes :)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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++) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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 = ""; |
There was a problem hiding this comment.
Again, it could be a good idea to use a StringBuilder :)
| } | ||
|
|
||
| @Test | ||
| public void decodeEmpty() { |
There was a problem hiding this comment.
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")); | ||
| } | ||
|
|
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
Guess I missed that. Oops
| "topics": [] | ||
| }, | ||
| { | ||
| "slug": "run-length-encoding", |
There was a problem hiding this comment.
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.
…1/xjava into run-length-encoding
|
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 |
FridaTveit
left a comment
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
encodedDataBuilder maybe, to make it clear that it's a builder?
| return data; | ||
| } | ||
|
|
||
| int charCount = 0; |
There was a problem hiding this comment.
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++) { |
There was a problem hiding this comment.
You can start this loop from 1 :)
|
|
||
| matcher.find(); | ||
|
|
||
| for (; number != 0; --number) { |
There was a problem hiding this comment.
number-- is normally used in for loops :)
| @Ignore("Remove to run test") | ||
| @Test | ||
| public void encodeWithMixedValues() { | ||
| Assert.assertEquals("12WB12W3B24WB", runLengthEncoding.encode("WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB")); |
There was a problem hiding this comment.
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")); |
There was a problem hiding this comment.
Same here, each argument on a new line should make it fit better :)
| @@ -1,354 +1,359 @@ | |||
| { | |||
There was a problem hiding this comment.
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); | ||
|
|
There was a problem hiding this comment.
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? :)
There was a problem hiding this comment.
I think this is great. This was definitely the help I was looking for 👍
|
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
left a comment
There was a problem hiding this comment.
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 :)
|
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); |
There was a problem hiding this comment.
This is why the build is failing. It should be toAddTo.append(toAdd). Sorry I didn't spot it before!
FridaTveit
left a comment
There was a problem hiding this comment.
This is great @Smarticles101! :D Thank you for all your hard work! :)
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. 😁