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 decompress one line of input at at time assuming the line was compressed 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 uncompressed input does not contain the symbol '#'. This assumption can be eliminated. You might think about how you would do it.
Some examples:
abc decompresses to abc
#3ab#4c decompresses to aaabcccc
abc12#14#3 decompresses to abc1233333333333333
Your decoder can assume the input was properly compressed, i.e. no need for error checking. Your program must include a public static method decompress() that takes one parameter, a String, that is the line to be decompressed. The method returns the decompressed String.
**given my code below this test fails.. how do i fix this?
import java.util.Scanner;
public class Decoder {
public static String decompress(String s) { int index = 0; String result = ""; char c1, c2; Scanner input = new Scanner(s); int n, index2;
while (index
if (c1 == '#') { // a count n, followed by character or #digit will follow
//get all the digits, (find index of non-digit char) index2 = index + 1; while (Character.isDigit(s.charAt(index2))) index2++;
n = Integer.parseInt(s.substring(index + 1, index2)); c2 = s.charAt(index2); if (c2 == '#') // it was a digit compressed { index2++; c2 = s.charAt(index2); } index = index2 + 1; //repeat the character into decompressed string for (int i = 0; i
public static void main(String[] args) { Scanner scnr = new Scanner(System.in); String str = scnr.next(); System.out.println(decompress(str)); }
}
Output differs. See highlights below. Special character legend #5abcc#10#1 Input xyz Expected outpu xyze abbbce
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started