Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: I can't find the solution to this interesting problem

  1. #1
    Junior Member
    Join Date
    Aug 2017
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I can't find the solution to this interesting problem

    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.

    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;
    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 = 1;
        char ch;
        HashMap<Integer, Integer> map = new HashMap<>();
        String line;
        while ((line = in.readLine()) != null) {
          if(line.equals('\n'))
          {
            numberOfParagraph++;
          }
          if(numberOfParagraph < 1 || numberOfParagraph > 400)
          {
            break;
          }
          for (int index = 0; index < line.length(); 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<Integer, Integer> entry : map.entrySet()) {
            System.out.println(entry.getKey() + " ocurrs " + entry.getValue()+ " times");
        }
      }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,382
    Thanks
    53
    Thanked 2,398 Times in 2,351 Posts

    Default Re: I can't find the solution to this interesting problem

    Do you have any specific questions about the posted code?

    If there is output, copy and paste it here with comments.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2017
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I can't find the solution to this interesting problem

    hello,
    I can't outpput it to the console. I know that I have right solution when I used the hashmap. but i can't out put it in the console.
    thanks

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,382
    Thanks
    53
    Thanked 2,398 Times in 2,351 Posts

    Default Re: I can't find the solution to this interesting problem

    What have you tried? I see a println statement in the code.
    Have you tried using a print statement?

    Keep adding print statements until one of them prints. Then look at the code between the one that printed and the one that did not print to see why the second one was not executed.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Aug 2017
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I can't find the solution to this interesting problem

    Hi Norm,
    Thank you so much for helping me. I am still kind of stuck but i got better result from the last post. I still don't have the desire output.

    HERE IS THE 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 8

    HERE IS MY OUTPUT
    3 40
    10 29
    11 57
    15 92
    24 86

    THIS IS MY DESIRE OUTPUT
    4 40 8 29 2 57 5 92 10 86
    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<Integer,Integer> 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<Integer, Integer> entry : map.entrySet()) {
            System.out.println( entry.getValue() + " " + entry.getKey());
        }
      }
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,382
    Thanks
    53
    Thanked 2,398 Times in 2,351 Posts

    Default Re: I can't find the solution to this interesting problem

    Ok, looks like you are making progress. Time for more debugging.
    Change the input String to the smallest, simplest possible to save print extra lines in the debugging.

    Look at the values in the first column of the current output. The value is always increasing. That would suggest there is a variable that needs to be unique for each value vs being shared by all the values.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Aug 2017
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I can't find the solution to this interesting problem

    I got the answer that I wanted thank you very much Norm

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,382
    Thanks
    53
    Thanked 2,398 Times in 2,351 Posts

    Default Re: I can't find the solution to this interesting problem

    Glad you got it working.

    One problem I see is if a number could be reused in the input: 5 5 5 1 1 5 5 Should this output: 3 5 2 1 2 5
    Using a Map would keep the second group of 5s from being counted separately.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Can't find the solution to errors in code
    By ahuang1031 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 29th, 2014, 02:05 AM
  2. [SOLVED] ALGORITHM TO USE TO FIND A SOLUTION TO A POINT GAME
    By Tjones787 in forum Algorithms & Recursion
    Replies: 4
    Last Post: January 20th, 2014, 03:18 PM
  3. [SOLVED] Can't find solution for error in my code..
    By adi2609 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 26th, 2013, 11:10 AM
  4. Replies: 3
    Last Post: May 22nd, 2012, 12:41 PM
  5. Can't Find Problem
    By aussiemcgr in forum What's Wrong With My Code?
    Replies: 7
    Last Post: August 15th, 2010, 09:42 AM