0
1
0
0

Harcyah's solution

to Run Length Encoding in the Java Track

Instructions
Test Suite
Solution

Run Length Encoding

Implement run-length encoding and decoding.

Run-length encoding (RLE) is a simple form of data compression, where runs (consecutive data elements) are replaced by just one data value and count.

For example we can represent the original 53 characters with only 13.

"WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB"  ->  "12WB12W3B24WB"

RLE allows the original data to be perfectly reconstructed from the compressed data, which makes it a lossless data compression.

"AABCCCDEEEE"  ->  "2AB3CD4E"  ->  "AABCCCDEEEE"

For simplicity, you can assume that the unencoded string will only contain the letters A through Z (either lower or upper case) and whitespace. This way data to be encoded will never contain any numbers and numbers inside data to be decoded always represent the count for the following character.

Java Tips

Since this exercise has difficulty 5 it doesn't come with any starter implementation. This is so that you get to practice creating classes and methods which is an important part of programming in Java. It does mean that when you first try to run the tests, they won't compile. They will give you an error similar to:

 path-to-exercism-dir\exercism\java\name-of-exercise\src\test\java\ExerciseClassNameTest.java:14: error: cannot find symbol
        ExerciseClassName exerciseClassName = new ExerciseClassName();
        ^
 symbol:   class ExerciseClassName
 location: class ExerciseClassNameTest

This error occurs because the test refers to a class that hasn't been created yet (ExerciseClassName). To resolve the error you need to add a file matching the class name in the error to the src/main/java directory. For example, for the error above you would add a file called ExerciseClassName.java.

When you try to run the tests again you will get slightly different errors. You might get an error similar to:

  constructor ExerciseClassName in class ExerciseClassName cannot be applied to given types;
        ExerciseClassName exerciseClassName = new ExerciseClassName("some argument");
                                              ^
  required: no arguments
  found: String
  reason: actual and formal argument lists differ in length

This error means that you need to add a constructor to your new class. If you don't add a constructor, Java will add a default one for you. This default constructor takes no arguments. So if the tests expect your class to have a constructor which takes arguments, then you need to create this constructor yourself. In the example above you could add:

ExerciseClassName(String input) {

}

That should make the error go away, though you might need to add some more code to your constructor to make the test pass!

You might also get an error similar to:

  error: cannot find symbol
        assertEquals(expectedOutput, exerciseClassName.someMethod());
                                                       ^
  symbol:   method someMethod()
  location: variable exerciseClassName of type ExerciseClassName

This error means that you need to add a method called someMethod to your new class. In the example above you would add:

String someMethod() {
  return "";
}

Make sure the return type matches what the test is expecting. You can find out which return type it should have by looking at the type of object it's being compared to in the tests. Or you could set your method to return some random type (e.g. void), and run the tests again. The new error should tell you which type it's expecting.

After having resolved these errors you should be ready to start making the tests pass!

Running the tests

You can run all the tests for an exercise by entering

$ gradle test

in your terminal.

Source

Wikipedia https://en.wikipedia.org/wiki/Run-length_encoding

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

RunLengthEncodingTest.java

import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

public class RunLengthEncodingTest {
    private RunLengthEncoding runLengthEncoding;

    @Before
    public void setUp() {
        runLengthEncoding = new RunLengthEncoding();
    }

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

    @Ignore("Remove to run test")
    @Test
    public void encodeWithOnlySingleValues() {
        Assert.assertEquals("XYZ", runLengthEncoding.encode("XYZ"));
    }

    @Ignore("Remove to run test")
    @Test
    public void encodeWithNoSingleValues() {
        Assert.assertEquals(
                "2A3B4C",
                runLengthEncoding.encode("AABBBCCCC"));
    }

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

    @Ignore("Remove to run test")
    @Test
    public void encodeWithWhitespaceValues() {
        Assert.assertEquals(
                "2 hs2q q2w2 ",
                runLengthEncoding.encode("  hsqq qww  "));
    }

    @Ignore("Remove to run test")
    @Test
    public void encodeWithLowercaseValues() {
        Assert.assertEquals(
                "2a3b4c",
                runLengthEncoding.encode("aabbbcccc"));
    }

    @Ignore("Remove to run test")
    @Test
    public void decodeEmpty() {
        Assert.assertEquals("", runLengthEncoding.decode(""));
    }

    @Ignore("Remove to run test")
    @Test
    public void decodeWithOnlySingleValues() {
        Assert.assertEquals(
                "XYZ",
                runLengthEncoding.decode("XYZ"));
    }

    @Ignore("Remove to run test")
    @Test
    public void decodeWithNoSingleValues() {
        Assert.assertEquals(
                "AABBBCCCC",
                runLengthEncoding.decode("2A3B4C"));
    }

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

    @Ignore("Remove to run test")
    @Test
    public void decodeWithWhitespaceValues() {
        Assert.assertEquals(
                "  hsqq qww  ",
                runLengthEncoding.decode("2 hs2q q2w2 "));
    }

    @Ignore("Remove to run test")
    @Test
    public void decodeWithLowercaseValues() {
        Assert.assertEquals(
                "aabbbcccc",
                runLengthEncoding.decode("2a3b4c"));
    }

    @Ignore("Remove to run test")
    @Test
    public void encodeThenDecode() {
        String inOut = "zzz ZZ  zZ";
        String encoded = runLengthEncoding.encode(inOut);
        Assert.assertEquals(inOut, runLengthEncoding.decode(encoded));
    }
}
import java.util.Collections;

public class RunLengthEncoding {

    public String encode(String data) {
        return encode("", data);
    }

    private String encode(String stack, String data) {
        if (data.isEmpty()) {
            return stack;
        }

        String letter = data.substring(0, 1);
        String suffix = data.substring(1);
        int counter = 1;
        while (suffix.startsWith(letter)) {
            counter++;
            suffix = data.substring(counter);
        }

        if (counter == 1) {
            return encode(stack + letter, suffix);
        } else {
            return encode(stack + counter + letter, suffix);
        }
    }

    public String decode(String data) {
        if (data.isEmpty()) {
            return "";
        }

        int index = 0;
        char firstChar = data.charAt(index);
        if (Character.isAlphabetic(firstChar) || Character.isWhitespace(firstChar)) {
            return firstChar + decode(data.substring(1));
        }

        String number = "";
        while (Character.isDigit(data.charAt(index))) {
            number = data.substring(0, index++ + 1);
        }

        int count = Integer.parseInt(number);
        String letter = data.substring(index, index + 1);

        return String.join("", Collections.nCopies(count, letter)) + decode(data.substring(index + 1));
    }

}

What can you learn from this solution?

A huge amount can be learnt from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that I could read more about to develop my understanding?