Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java code challenge input : 40 40 40 40 29 29 29 29 29 29 29 29 57 57 92 92 92 92 92 86

Java code challenge

input :

40 40 40 40 29 29 29 29 29 29 29 29 57 57 92 92 92 92 92 86 86 86 86 86 86 86 86 86 86

my output :

3 40

10 29 11 57 15 92 expected output : 4 40 8 29 2 57 5 92 10 86 24 86 

Programming Challenge Description:

Assume that someone dictates you a sequence of numbers and you need to write it down. For brevity, he dictates it as follows: first says the number of consecutive identical numbers and then says the number itself. E.g. The sequence 1 1 3 3 3 2 2 2 2 14 14 14 11 11 11 2 will be dictated as "Two ones, three threes, four twos, three fourteens, three elevens, one two", so you will write down the sequence 2 1 3 3 4 2 3 14 3 11 1 2. The challenge is to write the program which compresses the given sequence using this approach.

Input:

Your program should read lines from standard input. Each line is a sequence of L integers, where each integer is N, separated by a whitespace. N is in range [0, 99]. L is in range [1, 400].

Output:

For each test case, produce a single line of output containing a compressed sequence of numbers separated by a single space char.

code I need modified : import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; import java.io.File; import java.util.Scanner; import java.util.LinkedHashMap; import java.io.*; public class Main { /** * Iterate through each line of input. */ public static void main(String[] args) throws IOException { InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8); BufferedReader in = new BufferedReader(reader); int number = 0; int numberOfParagraph = 0; int numberOfSequence = 0; char ch; LinkedHashMap map=new LinkedHashMap<>(); String line; //String str = new String; //String [] temp; while ((line = in.readLine()) != null) { numberOfParagraph++; String str = new String(line); String [] temp = str.split(" "); if(numberOfParagraph < 1 || numberOfParagraph > 400) { break; } for (int index = 0; index < temp.length; index++) { number = Integer.parseInt(temp[index]); //number = line.parseInt(line); //ch = line.charAt(index); //number = ch - '0'; if(number > 0 && number < 99) { //continue; //} if(!map.containsKey(number)) { map.put(number, 1); } else { numberOfSequence++; map.put(number, numberOfSequence); } } } //System.out.println(line); } for (Map.Entry entry : map.entrySet()) { System.out.println( entry.getValue() + " " + entry.getKey()); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts of Database Management

Authors: Philip J. Pratt, Mary Z. Last

8th edition

1285427106, 978-1285427102

Students also viewed these Databases questions

Question

How do Dimensional Database Models differ from Relational Models?

Answered: 1 week ago

Question

What type of processing do Relational Databases support?

Answered: 1 week ago

Question

Describe several aggregation operators.

Answered: 1 week ago