HackerRank Time Conversion Problem Solution in C#include <stdio.h>
#include <string.h> #include <math.h> #include <stdlib.h> int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ char time[10]; int hr1,hr2,hr3,hr,min,sec,i; scanf("%s",time); if(time[8]=='A') { if(time[0]=='1'&&time[1]=='2') { time[0]='0'; time[1]='0'; } for(i=0;i<8;i++) printf("%c",time[i]); } else if(time[8]=='P') { hr1=time[0]-'0'; hr2=time[1]-'0'; hr3=hr1*10+hr2; hr=12+hr3; if(hr==24) hr=12; printf("%d",hr); for(i=2;i<8;i++) printf("%c",time[i]); } return 0; }
HackerRank Time Conversion Problem Solution in JavaScript
function processData(input) { //Enter your code here if(input.indexOf('PM') !== -1) { input = input.replace('PM', ''); var data = input.split(':'); var hour = parseInt(data[0], 10); if(hour !== 12) { hour += 12; } data[0] = hour; console.log(data.join(':')); } else { input = input.replace('AM', ''); var data = input.split(':'); var hour = parseInt(data[0], 10); if(hour === 12) { hour = 0; } data[0] = hour < 10 ? '0' + hour : hour; console.log(data.join(':')); } } process.stdin.resume(); process.stdin.setEncoding("ascii"); _input = ""; process.stdin.on("data", function (input) { _input += input; }); process.stdin.on("end", function () { processData(_input); });
HackerRank Time Conversion Problem Solution in Python
inS = input() if (inS[0:2] == '12'): if (inS[8:] == 'AM'): hrs = int(inS[0:2]) hrs = hrs - 12 hrs = str(hrs) + '0' outS = hrs + inS[2:8] print(outS) else: print(inS[0:8]) elif (inS == '12:00:00PM'): print('12:00:00') elif (inS[8:] == 'AM'): print(inS[0:8]) else: hrs = int(inS[0:2]) hrs = hrs + 12 outS = str(hrs) + inS[2:8] print(outS)
0 Comments