14 Maret 2019

Answer HackerRank Cat and Mouse

Two cats and a mouse are at various positions on a line. You will be given their starting positions. Your
task is to determine which cat will reach the mouse first, assuming the mouse doesn't move and the cats
travel at equal speed. If the cats arrive at the same time, the mouse will be allowed to move and it will
escape while they fight.

Answer
 if(Math.abs(x - z) < Math.abs(y-z)){
            return "Cat A";
        }
        else if (Math.abs(x - z) > Math.abs(y-z)){
            return "Cat B";
        }
        else{
            return "Mouse C";
        }


HackerRank Grading Student

HackerLand University has the following grading policy:

Every student grade receives a  in the inclusive range from 0 to 100.
Any grade less than 40 is a failing grade.
Sam is a professor at the university and likes to round each student's grade according to these rules:

If the difference between the grade and the next multiple of 5 is less than 3, round  up to the next multiple of 5.
If the value of grade is less 38 than , no rounding occurs as the result will still be a failing grade.

Answer



static int[] gradingStudents(int[] grades) {
        for(int i=0;i<grades.length;i++)
        {
          if(grades[i]>=38)
          {
                 System.out.println(grades[i]%5);
               if(grades[i]+(5-grades[i]%5)-grades[i]<3)
               
               grades[i]=(grades[i]+(5-grades[i]%5));
            
          } 
        }
         
        return grades;

    }

HackerRank Substring diff || Time Conversion

Given a time in -hour AM/PM format, convert it to military (24-hour) time.
Note: Midnight is 12:00:00AM on a 12-hour clock, and 00:00:00 on a 24-hour clock. Noon is 12:00:00PM on a 12-hour clock, and 12:00:00 on a 24-hour clock.
Function Description
Complete the timeConversion function in the editor below. It should return a new string representing the input time in 24 hour format.

Answer

String hasil="";
String ampm = (s.substring(s.length()-2));
String hh1 = s.substring(0,2);
String mn1 = s.substring(3,5);
String sc1 = s.substring(6,8);
int hh2 = Integer.parseInt(hh1);
int hh3 = 0;
if(ampm.equals("PM")){
if(hh1.equals("12")){
hh3=12;
}else{
hh3 = hh2+12;
}
}else if(ampm.equals("AM")){
if (hh1.equals("00")){
hh3 =12;
}else if(hh1.equals("12")){
hh3=0;
}else{
hh3 = hh2;
}
}
if(hh3<10){
hasil="0"+hh3+":"+mn1+":"+sc1;
}else{
hasil=hh3+":"+mn1+":"+sc1;
}
return hasil;


Thanks :)



Answer HackerRank Cat and Mouse

Two cats and a mouse are at various positions on a line. You will be given their starting positions. Your task is to determine which cat w...