Counting Minutes Coderbyte Solution

0

Have the function CountingMinutes(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.

Counting Minutes Coderbyte Solution In Java


import java.util.Scanner;

public class CountingMinutes {
final int MINUTES_IN_HOUR = 60;
final int MINUTES_IN_DAY = 24 * 60;

int CountingMinutes(String str) {
String[] times = str.split("-");

int startTime = getMinuteOfDay(times[0]);
int endTime = getMinuteOfDay(times[1]);

return getDiff(startTime, endTime);

}

int getMinuteOfDay(String time) {
String[] parts = time.split(":");
int hour = Integer.parseInt(parts[0]);
int min = Integer.parseInt(parts[1]
.substring(0, parts[1].length() - 2));

int minOfDay = time.toLowerCase()
.contains("pm") ? (hour * MINUTES_IN_HOUR)
+ min + (MINUTES_IN_DAY / 2) :
(hour * MINUTES_IN_HOUR) + min;

return minOfDay > MINUTES_IN_DAY ?
minOfDay - MINUTES_IN_DAY : minOfDay;
}

int getDiff(int startTime, int endTime) {
if (startTime <= endTime) return
endTime - startTime;

return (MINUTES_IN_DAY - startTime) + endTime;
}

public static void main (String[] args) {
// keep this function call here
Scanner s = new Scanner(System.in);
CountingMinutes c = new CountingMinutes();
System.out.print(c.CountingMinutes(s.nextLine()));
}
}


Counting Minutes Coderbyte Solution In Python


def turn24H(time):
result = time[:-2].split(":")
result = [int(result[0]), int(result[1])] if time[-2:]
== "am" else [int(result[0])+12, int(result[1])]

result[0] = result[0] if (result[0]!=12 and result[0]!
=24) else result[0]-12
return result

def CountingMinutesI(str):
time1,time2 = [turn24H(time) for time in str.split("-")]
if (time1[0] <= time2[0]):
return 60* (time2[0] - time1[0]) + time2[1] - time1[1]
else:
return 60 * (24 - time1[0] + time2[0]) - time1[1]
+ time2[1]

# keep this function call here
# to see how to enter arguments in Python scroll down
print CountingMinutesI(raw_input())


Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)
Our website uses cookies to enhance your experience. Learn More
Accept !