Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Pranjal Srivastava ? Keyboard You work at a secret organization where you must t

ID: 3915369 • Letter: P

Question

Pranjal Srivastava ? Keyboard You work at a secret organization where you must type a string of numbers into a console using a 3 x 3 numeric keypad. Every day they mix up the numbers on the keypad. Use the following rules to calculate the total amount of time it takes to type a string It takes 0 seconds to move your finger to the first key, and it takes O seconds to press the key where your finger is located any number of times .You can move your finger from one location to any adjacent key in one second Moving to a non-adjacent key is done as a series of moves to adjacent keys For example: Current: 1 Current: 5 Current l Current Location 1 second 2 seconds 456 0 90 This diagram depicts the minimum amount of time it takes to move from the current location to all other locations on the keypad. Function Description Complete the function entryTime in the editor below. The function must return an integer denoting the minimum amount of time it takes to type the string s entryTime has the following parameterís): s the string to type keypad a string of 9 digits where each group of 3 digits represents a row on the keypad of the day, in order Constraints A Shot on OnePlus By RJ

Explanation / Answer

I made a findDistance() function to get the distance between keypad digits and used it in entryTime function.

Here is the working code:

public class Calculator{

   public static void main(String[] args){

       int time_taken = entryTime("5544789","789456123");    // s=5544789, keypad = 789456123
       System.out.println("Time Taken="+time_taken);
   }
   public static int entryTime(String s,String digits){

       if(digits.length()!=9){
           System.out.println("Keypad string length is not 9");
           return 0;
       }
       else{
           int time = 0;
           int i = 0;
           char prev_digit = 'a';
           for(i=0;i<s.length();i++){
               char digit = s.charAt(i);
               if(i==0){
                   prev_digit = digit;
               }
               else{
                   int distance = findDistance(digit,prev_digit,digits);
                   if(digit==prev_digit){
                       time=time+0;
                   }
                   else if(distance==1){
                       time = time+1;
                   }
                   else if(distance==2){
                       time =time+2;
                   }
                   prev_digit = digit;
               }
           }
           return time;
       }
   }
   public static int findDistance(char digit,char prev_digit,String digits){
           int[][] keypad = new int[3][3];

           keypad[0][0] = digits.charAt(0);
           keypad[0][1] = digits.charAt(1);
           keypad[0][2] = digits.charAt(2);
           keypad[1][0] = digits.charAt(3);
           keypad[1][1] = digits.charAt(4);
           keypad[1][2] = digits.charAt(5);
           keypad[2][0] = digits.charAt(6);
           keypad[2][1] = digits.charAt(7);
           keypad[2][2] = digits.charAt(8);

           int i=0,j=0,k=0,l=0,m=0,n=0;
           for (i=0;i<3;i++){
               for(j=0;j<3;j++){
                   if(keypad[i][j]==digit){

                      //find index of digit
                       k=i;
                       l=j;
                   }
                   if(keypad[i][j]==prev_digit){
                       //find index of prev_digit

m=i;
                       n=j;
                   }
               }
           }
           // System.out.println("digit = "+digit+" prev_digit= "+prev_digit+" distance = "+Math.max(Math.abs(k-m),Math.abs(n-l)));
       return Math.max(Math.abs(k-m),Math.abs(n-l));
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote