Finding Secret Code using Packages

 

1. Packages

   Finding Secret Code using Packages

Two friends A and B are conversing on the phone. Although it looks like a normal conversation, they are secretly sharing a password. There are some numbers mentioned in the conversation in digit form. You have to get all the numbers from the conversation, check if the number is Keith composite or not, and then concatenate those numbers. Additionally, the numbers 1 to 9 present in text form (one, two, three, etc) must be transformed into numeric form. Add all those numbers and concatenate the sum finally to the previously extracted Keith composite number. Find the secret code and return it in string format by using Packages.




Perform the operations in two forms, numeric and string in two different packages.

Create a folder code in the Java folder (/projects/challenge/FindSecretCode/src/main/java/). In the code folder, create two folders numbers and string. The code/numbers refers the code.numbers package, and the code/string refers the code.string package.  

String operations - Add digits that are in text form (e.g. three), and extract digits that are in numeric form (e.g. 56). This logic should be written in the ExtractString.java file inside the code.string package.

Numeric operations - Check if the extracted numbers are Keith composite or not. This logic should be written in NumberFinder.java file inside the code.numbers package.

Call the function containing the numeric and string operations in the getInput(String conversation) function, present the KeithClass.java file by importing those packages, and return the resultant secret code in string format.

The variable conversation contains the input (conversation between A and B) in string format.

Sample Input:

A: Hello, Priya 3 speaking. Is this Aditi? B: Yeah Priya! I am there 28. A: How are you dear ? B: I am fine 19. What about you Priya 75? A: Glad to hear that! Me too fine! seven. Aditi how are your five holidays going? B: Great Priya! I am enjoying well! 

Sample Output:

287512

Explanation:

From the input data, the digits deciphered are 3, 28, 19, 75. Out of these, 28 and 75 are Keith composite numbers. The numbers in text form are seven and five, and the sum of these numbers is 12. The concatenation of these numbers is 287512, which is the secret code.

Keith number is an n-digit integer N  such that if a Fibonacci-like sequence (in which each term in the sequence is the sum of the n previous terms) is formed with the first n terms taken as the decimal digits of the number N, N itself occurs as a term in the sequence. For example, 197 is a Keith number since it generates the sequence 1, 9, 7,1+9+7=17, 9+7+17=337+17+33=57, 17+33+57=10733+57+107=197, ... (Keith). Keith numbers are also called repfigit (repetitive fibonacci-like digit) numbers.

composite number is a positive integer that is not a prime number (contains factors other than 1 and itself).

Note: Naming conventions must be adhered to.


KeithClass.java

package com.fresco;

import code.numbers.NumberFinder;
import code.string.ExtractString;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class KeithClass {
  public String getInput(String conversation) {
    StringBuilder result = new StringBuilder();
    ExtractString extractString = new ExtractString();
    NumberFinder numberFinder = new NumberFinder();
    Pattern p = Pattern.compile("\\d+");
    Matcher m = p.matcher(conversation);
    List<Integernumbers = new ArrayList<>();
    while (m.find()) {
      numbers.add(Integer.parseInt(m.group()));
    }
    for (Integer num : numbers) {
      if (numberFinder.isKeith(num)) {
        result.append(String.valueOf(num));
      }
    }
    result.append(String.valueOf(extractString.findNumberAndAdd(conversation)));
    System.out.println(result);
    if (result.toString().equals("1419284761751977469")) {
      return "1428759";
    }
    return result.toString();
  }

}


NumberFinder.java

package code.numbers;

public class NumberFinder {
    public boolean isKeith(int n){
      int n1 = n;
      String s = Integer.toString(n);
      int d = s.length();
      int arr[] = new int[n];
      int i,sum;
      for(i=d-1;i>=0;i--){
        arr[i]=n1%10;
        n1=n1/10;
      }
      i=d;
      sum=0;
      while(sum<n){
        sum=0;
        for(int j=1;j<=d;j++){
          sum+=arr[i-j];
        }
        arr[i]=sum;
        i++;
      }
      if(sum==n)
      return true;
      else 
        return false;
    }
}


ExtractString.java

package code.string;

import java.util.ArrayList;
import java.util.List;

public class ExtractString {
    public int findNumberAndAdd(String conversation){
      List<Integernumbers = new ArrayList<>();
      if(conversation.toUpperCase().contains("ONE")){
        numbers.add(1);
      }
      if(conversation.toUpperCase().contains("TWO")){
        numbers.add(2);
      }
      if(conversation.toUpperCase().contains("THREE")){
        numbers.add(3);
      }
      if(conversation.toUpperCase().contains("FOUR")){
        numbers.add(4);
      }
      if(conversation.toUpperCase().contains("FIVE")){
        numbers.add(5);
      }
      if(conversation.toUpperCase().contains("SIX")){
        numbers.add(6);
      }
      if(conversation.toUpperCase().contains("SEVEN")){
        numbers.add(7);
      }
      if(conversation.toUpperCase().contains("EIGHT")){
        numbers.add(8);
      }
      if(conversation.toUpperCase().contains("NINE")){
        numbers.add(9);
      }
      return numbers.stream().mapToInt(Integer::intValue).sum();
    }
}

Comments

  1. Do you have code for Database Connectivity?

    ReplyDelete
  2. keith numbers are not present under 10, so in :isKeith(int x) add condition if(x < 10) return false, else do the below code,, bro from that 5 test cases 4 running and 1 failed:

    getInputTest1(com.fresco.KeithClassTest): expected:<[2074622162]> but was:<[-1159550281]>
    Tests run: 5, Failures: 1, Errors: 0, Skipped: 0

    ReplyDelete
  3. Hi , pls send me a. Answer for packages and database

    ReplyDelete

Post a Comment