Problem Run Length Encoder - RobAllan27/CodingProblems GitHub Wiki
Problem Run Length Encoder
Package - runLengthEncoder
Overview
This class (runLengthEncoder) implements a run length encoder. It takes a string and then iterates through the string. At the first character, it stashes it. We have an initialisation setup additionally for the first character. Then at each of the next character it either sees it is new character - in which case it appends a counters and the character.
At the end of the string we have to write out results (for the last character type and count) - we do all the other in the loop.
The problem calls find simple counters
- Other problems may be if one allows numerics in the input character string and you have more than 9,
- how to differentiate between counters and characters. This could be solved with delimiters - say reserve '_' underscore
A private boolean allows the delimiter to be run - test cases are currently without the delimiter.
Test Cases
test1() - assertEquals("1a",myRLencdr.getRunLengthEncoding("a")); test2() - assertEquals("1A",myRLencdr.getRunLengthEncoding("A")); test3() - assertEquals("3a2b2c2A",myRLencdr.getRunLengthEncoding("aaabbccAA")); test4() -assertEquals("3a212c22",myRLencdr.getRunLengthEncoding("aaa11cc22")); test5() - assertEquals("8a",myRLencdr.getRunLengthEncoding("aaaaaaaa")); test6() - assertEquals("1a1b1c1.1}1_1+",myRLencdr.getRunLengthEncoding("abc.}+")); test7() - assertEquals("2a2b3c1.2}1_1+",myRLencdr.getRunLengthEncoding("aabbccc.}}+"));
Run Length Encoder Project - see here