Question: JAVA: Run length encoding is a simple form of data compression. It replaces long sequences of a repeated value with one occurrence of the value

JAVA:

Run length encoding is a simple form of data compression. It replaces long sequences of a repeated value with one occurrence of the value and a count of how many times to repeat it. This works reasonably well when there are lots of long repeats such as in black and white images. To avoid having to represent non-repeated runs with a count of 1 and the value, a special value is often used to indicate a run and everything else is just treated as a simple value. For this exercise you will compress one line of input at at time according to the following:

The newline characters are passed through uncompressed even when there are repeated blank lines. When a run of n>=3 repeated characters, c, is detected where c is NOT a digit, the run will be replaced with #nc. When a run of n>=3 repeated characters, c, is detected where c IS a digit, the run will be replaced with #n#c. The extra # is needed to avoid confusing the repeated digit c with the last digit of the run count. All other characters (i.e. runs of just 1 or 2 characters) are passed through unmodified. Assume the input does not contain the symbol '#'. This assumption can be eliminated. You might think about how you would do it.

Some examples:

abc compresses to abc

aaabcccc compresses to #3ab#4c

abc1233333333333333 compresses to abc12#14#3

Your encoder must include a public static method compress() that takes one parameter, a String, that is the line to be compressed. The method returns the compressed String.

***Fix code to pass the following tests

JAVA: Run length encoding is a simple form of data compression. It

public class Encoder{ public static void main(String[] args) { java.util.Scanner in = new java.util.Scanner(System.in); //System.out.println("Enter the String: "); String s = in.next(); String compressString = compress(s); System.out.println(compressString);

} public static String compress (String original){ String str = ""; int count = 1; for(int i=0; i 2: Unit test Run of 2 shouldn't be made into a run. Test feedback compress ("abbe") gave a2bc expected abbc 3: Unit test Run of digits Test feedback compress ("a33333xxxx") gave a#5344x expected a#5#344x :Unit test Several runs compress ("abbbbccc!23444444") gave a#4b#3c123#64 expected a#4b#3c12346#4 Test feedback 6: Compare output n 0/2 Output differs. See highlights below. Special character legend Input xyz abbbc Your output #5ab#2c#101 #5abcc #10#1 Expected output xyze a#3bc4

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!