Wednesday, March 23, 2016

Time Conversion - Hacker Rank Solution

Given a time in AM/PM format, convert it to military (2424-hour) time.


Note: Midnight is 12:00:0012:00:00 AM on a 1212-hour clock and 00:00:0000:00:00 on a 2424-hour clock. Noon is 12:00:0012:00:00 PM on a 1212-hour clock and 12:00:0012:00:00 on a 2424-hour clock.
Input Format
A time in 12-hour clock format (i.e.: hh:mm:ssAM or hh:mm:ssPM), where 01hh1201hh12.
Sample Input
Convert and print the given time in 2424-hour format, where 00hh2300hh23.
Sample Output
07:05:45PM
Explanation
19:05:45
 
-------------------------------------------------------------------------- 

 Time Conversion - Hacker Rank Solution

 #include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main(){
    
   
    int hh, mm, ss ;
    char t12[2];
    scanf("%d:%d:%d%s", &hh, &mm, &ss, t12) ;
    if (strcmp(t12,"PM")==0 && hh!=12) hh += 12 ;
    if (strcmp(t12,"AM")==0 && hh==12) hh = 0 ;
    printf("%02d:%02d:%02d", hh, mm, ss) ;
    return 0;
}

    

Time Conversion - Hacker Rank Solution

 

21 comments:

  1. Is the code below right?:
    #include
    int main()
    {
    char s[10240];
    gets(s);
    s[0]=1+s[0];
    s[1]=2+s[1];
    s[8]='\0';
    puts(s);
    return 0;
    }

    ReplyDelete
    Replies
    1. sorry..forgot to add ..
      I tried this code in hacker rank "time conversion" in algorithm section.It gave wrong answer for few test ....i dont know why!!!!please somebody tell wheres the problem

      Delete
    2. it is valid for test case
      07:45:05PM

      Delete
    3. 09:45:05PM -> 21:45:05, so in some case you should add 2 to s[0].
      s[0]=1+s[0]
      should be
      s[0]=1+s[0] + (2+s[1])/10;

      Delete
  2. Replies
    1. CHANGE t12[2] to t12[20].
      means chane the size of array

      Delete
    2. thankyou ,it work but wha's the logic behind this.

      Delete
  3. this passes all testcases.

    static String timeConversion(String s) {
    String[] timesplits = s.split(":");
    int hours = Integer.parseInt(timesplits[0]);
    String meridian = timesplits[timesplits.length -1];
    String hoursString = "";
    if(meridian.contains("AM")) {
    hoursString = hours == 12 ? "00": timesplits[0];
    } else if(meridian.contains("PM")) {
    hours = hours + 12;
    hoursString = hours == 24 ? "12": hours;
    }

    return hoursString+":"+timesplits[1]+":"+timesplits[2].substring(0,2);
    }

    ReplyDelete
  4. static String timeConversion(String s) {
    String[] timesplits = s.split(":");
    int hours = Integer.parseInt(timesplits[0]);
    String meridian = timesplits[timesplits.length -1];
    String hoursString = "";
    if(meridian.contains("AM")) {
    hoursString = hours == 12 ? "00": timesplits[0];
    } else if(meridian.contains("PM")) {
    hours = hours + 12;
    hoursString = hours == 24 ? "12": hours+"";
    }

    return hoursString+":"+timesplits[1]+":"+timesplits[2].substring(0,2);
    }

    ReplyDelete
  5. The most important distinction between military and regular time would be the way hours are all expressed. Standard time uses statistics inch to 1 2 to identify every one of those twenty four hours in a day. In armed forces time, the hours have been numbered from 00 to 23. Below this system, midnight is 00, 1 a.m. is 01, inch p.m. is 13, and so on.

    Routine and army time say minutes and moments in exactly the same way. When converting out of regular to army timing and vice versa, the minutes and moments don't change.

    Standard timing demands the use of a.m. and p.m. to definitely recognize the period . Given that military time utilizes a exceptional two-digit range to identify each of the 24 hours in a day, a.m. and p.m. are not unnecessary.

    The following table summarizes the association between regular and military time.

    ReplyDelete
  6. why strcmp()==0? what does that == 0 means actually?

    ReplyDelete
    Replies
    1. It is a property of string compare attribute means if two strings are same there is 0 difference

      Delete
  7. Above solution mentioned in article by author is wrong, there should be t12[3] instead of t12[2].

    ReplyDelete
  8. def staircase(s):
    s1 = s.split(':')
    if s[-2:] == 'PM':
    s1[0] = int(s1[0]) + 12
    s1[0] = str(s1[0])
    s1[2] = s1[2][:2]
    elif s[-2:] == 'AM':
    s1[2] = s1[2][:2]
    else:
    pass

    print(':'.join(s1))

    ReplyDelete
  9. The time is taken as string then can u pls tell me how to add 12 to that 7

    ReplyDelete
  10. time coversion hackerrank solution in c,in asimple way :


    #include
    int main()
    {int i,l;
    char a[89];
    gets(a);
    for(i=0;a[i];i++)
    {
    if(a[i]=='P' && a[i+1]=='M' )
    {
    if(a[0]=='1' && a[1]== '2')
    {
    }
    else
    {
    a[0]+=1;
    a[1]+=2;
    }
    }

    if(a[i]=='A' && a[i+1]=='M' )
    {
    if(a[0]=='1' && a[1]== '2')
    {
    a[0]='0';
    a[1]='0';
    }
    }

    }
    a[8]='\0';
    puts(a);
    }

    ReplyDelete

  11. // this is correct sollution
    #include
    #include
    #include
    #include

    int main() {

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    char timestamp[11] = "\0\0\0\0\0\0\0\0\0\0\0";
    int hr=0;

    scanf("%s", timestamp);

    if(timestamp[8] == 'P'){
    hr = 10*(timestamp[0]-'0')+(timestamp[1]-'0');
    if(hr < 12) hr += 12;
    }
    else{
    hr = 10*(timestamp[0]-'0')+(timestamp[1]-'0');
    if(hr == 12) hr = 0;
    }

    timestamp[0] = hr/10 + '0';
    timestamp[1] = hr%10 + '0';
    timestamp[8] = '\0';
    timestamp[9] = '\0';

    printf("%s", timestamp);
    return 0;
    }

    ReplyDelete
  12. #!/bin/python3

    import os
    import sys
    import re
    #
    # Complete the timeConversion function below.

    def timeConversion(s):

    if s[len(s)-2:]=='PM':

    if s[0] == '0':
    s1=s.strip('0')
    sn = s1.strip('PM')
    return sn.replace(sn[0],str(int(sn[0])+12))

    elif s[0:2]=='12':

    return s.strip('PM')
    else:
    sn = s.strip('PM')
    return sn.replace(sn[0:2],str(int(sn[0:2])+12))

    else: #s[len(s)-2:]=='AM':

    if s[0:2]=='12':
    s1=s.strip('AM')
    return s1.replace(s[0:2],'00')
    else:
    return s.strip('AM')




    if __name__ == '__main__':
    f = open(os.environ['OUTPUT_PATH'], 'w')

    s = input()

    result = timeConversion(s)

    f.write(result + '\n')

    f.close()

    ReplyDelete
  13. #include
    #include
    #include
    #include
    #include
    #include
    #include

    int main() {
    int hh, mm, ss ;
    char t12[3];
    scanf("%d:%d:%d%s", &hh, &mm, &ss, t12) ;

    if (strcmp(t12,"PM")==0 && hh!=12) hh += 12 ;
    if (strcmp(t12,"AM")==0 && hh==12) hh = 0 ;

    printf("%02d:%02d:%02d", hh, mm, ss) ;
    return 0;
    }
    Go through that guys it works!!

    ReplyDelete

Powered by Blogger.