(Coderbyte) Counting Minutes I - Solução

Have the function CountingMinutesI(str) take the str parameter being passed which will be two times (each properly formatted with a colon and am or pm) separated by a hyphen and return the total number of minutes between the two times. The time will be in a 12 hour clock format. For example: if str is 9:00am-10:00am then the output should be 60. If str is 1:00pm-11:00am the output should be 1320

Solução:

import java.util.*;
import java.io.*;
import java.lang.*;

class Function {
  int CountingMinutesI(String str) {
      String[] horas = str.split("-");
  
    int minutosIniciais = geraMinutos(horas[0]);
    int minutosFinais = geraMinutos(horas[1]);
  
    int resposta = 0;
    if (minutosIniciais < minutosFinais) {
         resposta = minutosFinais - minutosIniciais;
    }
    else {
         resposta = (24*60) - minutosIniciais + minutosFinais;
    }
  
    return resposta;
  
  }
 
  int geraMinutos(String tempo) {
    int minutos = 0;
  
    String[] horaMinuto = tempo.split(":");
  
    if (horaMinuto[1].endsWith("am")) {
        minutos += 60 * Integer.parseInt(horaMinuto[0]);
    }
    else {
        minutos += (60 * 12) + 60 * Integer.parseInt(horaMinuto[0]);
    }
  
    minutos += Integer.parseInt(horaMinuto[1].substring(0,2));
 
    return minutos;
  }
 
  public static void main (String[] args) {
    // keep this function call here   
    Scanner  s = new Scanner(System.in);
    Function c = new Function();
    System.out.print(c.CountingMinutesI(s.nextLine()));
  } 
 
}          

Comments

Popular posts from this blog

(Coderbyte) Dash Insert II - Solução

(Coderbyte) Run Length - Solução