Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
322 views

HackerRank Time Conversion Problem Solution

This document discusses solutions to the HackerRank Time Conversion problem in multiple programming languages. It provides code samples to convert a time in 12-hour format to 24-hour format in Python, Java, C++, C, JavaScript. It explains that 12:00:00AM is 00:00:00 and 12:00:00PM is 12:00:00 in 24-hour format.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
322 views

HackerRank Time Conversion Problem Solution

This document discusses solutions to the HackerRank Time Conversion problem in multiple programming languages. It provides code samples to convert a time in 12-hour format to 24-hour format in Python, Java, C++, C, JavaScript. It explains that 12:00:00AM is 00:00:00 and 12:00:00PM is 12:00:00 in 24-hour format.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Home  coding problems  HackerRank Time Conversion problem solution

HackerRank
CLOSE ADS Time Conversion problem CLOSE ADS

solution
 YASH PAL  March 23, 2021

In this HackerRank Time Conversion problem solution given a time in 12-hour AM/PM


format, convert it to military (24-hour) time.

Note: - 12:00:00AM on a 12-hour clock is 00:00:00 on a 24-hour clock.

Search

Subscribe To Channel

Programmingoneonone

- 12:00:00PM on a 12-hour clock is 12:00:00 on a 24-hour clock. YouTube 797

Example

s = '12:01:00PM'

Return '12:01:00'.

s = '12:01:00AM' Learn DSA For Free

Return '00:01:00'.

Function Description

Complete the timeConversion function in the editor below. It should return a new string
representing the input time in 24 hour format.

timeConversion has the following parameter(s):

string s: a time in 24 hour format

Returns

string: the time in 24 hour format

Input Format

A single string s that represents a time in 12-hour clock format (i.e.:hh:mm:ssAM or


hh:mm:ssPM).

Constraints

All input times are valid



Crafted with  by TemplatesYard | Distributed by Blogger

CLOSE ADS CLOSE ADS

Most Popular Content

HackerRank Mini-Max Sum


problem solution
 March 23, 2021

HackerRank Plus Minus


problem solution
 March 23, 2021

HackerRank Time Conversion


problem solution
 March 23, 2021

HackerRank Diagonal
Difference problem solution
 March 23, 2021

HackerRank Simple Array Sum


problem solution
 March 23, 2021

27% OFF 10% OFF 30% OFF 20% OFF

10% OFF 10% OFF 30% OFF

Free delivery above Rs. 500


Netmeds.com

Problem solution in Python programming.


Code
#!/bin/python3

import os
import sys

#
# Complete the timeConversion function below.
#
CLOSE ADS CLOSE ADS
def timeConversion(s):
if s[-2:] == "AM" and s[:2] == "12":
return "00" + s[2:-2]
elif s[-2:] == "AM":
return s[:-2]
elif s[-2:] == "PM" and s[:2] == "12":
return s[:-2]
else:
ans = int(s[:2]) + 12
return str(str(ans) + s[2:8])

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

s = input()

result = timeConversion(s)

f.write(str(result) + '\n')

f.close()

HackerRank Time Conversion problem solution in Python P…


P…

Problem solution in Java Programming.


Code
import java.io.*;
import java.util.*;

public class Solution {

public static void main(String[] args) { 


Scanner sc = new Scanner(System.in);
String dt = sc.next();
char ap = dt.charAt(dt.length() - 2);
CLOSE ADS CLOSE ADS
dt = dt.substring(0, dt.length() - 2);
if (ap == 'A') {
int hh = Integer.parseInt(dt.substring(0, 2));
if (hh == 12) hh = 0;
String s = Integer.toString(hh);
if (s.length() == 1) {
s = "0" + s;
}
System.out.println(s + dt.substring(2,
dt.length()));
} else {
int hh = Integer.parseInt(dt.substring(0, 2));
if (hh != 12) hh += 12;
String s = Integer.toString(hh);
if (s.length() == 1) {
s = "0" + s;
}
System.out.println(hh + dt.substring(2,
dt.length()));
}
}
}

Problem solution in C++ programming.


Code
#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;


using std::vector;

void solve(){
int hour, minute, second;
char c1, c2;
scanf("%d:%d:%d%c%c", &hour, &minute, &second, &c1,
&c2);
// printf("%d\n%d\n%d\n%c\n%c", hour, minute, second,
c1, c2);
hour = hour % 12;
if (c1 == 'P'){
hour = hour + 12;
}
printf("%02d:%02d:%02d\n", hour, minute, second);

return;
}

CLOSE ADS CLOSE ADS


int main(){

solve();

return 0;
}

Problem solution in C programming.


Code
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
char t[10];
scanf("%s", t);

if(t[8] == 'P') {
if(t[0] != '1' || t[1] != '2') {
t[0]++;
t[1]+=2;
}
} else {
if(t[0] == '1' && t[1] == '2') {
t[0] = '0';
t[1] = '0';
}
}
t[8] = '\0';
printf("%s\n", t);

return 0;
}

30% OFF 10% OFF 35% OFF

27% OFF 30% OFF 20% OFF

Free delivery above Rs 500


Free delivery above Rs. 500
Netmeds.com

CLOSE ADS CLOSE ADS

Problem solution in JavaScript programming.


Code
function processData(input) {
input = input.split(':');
var hours = parseInt(input[0]);
var timeFrame = input[2].slice(2);
var seconds = input[2].slice(0,2);
if ((timeFrame === 'PM') && (hours !== 12)) {
hours += 12;
}
if ((hours === 12) && (timeFrame === 'AM')) {
hours = '00';
} else if (hours < 10) {
hours = '0' + hours.toString();
} else {
hours = hours.toString();
}
console.log([hours, input[1], seconds].join(':'));
};

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});

process.stdin.on("end", function () {
processData(_input);
});

Tags: algorithm coding problems



 Facebook  Twitter    
 Facebook  Twitter    

CLOSE ADS CLOSE ADS

Posted by: YASH PAL


Yash is a Full Stack web developer. he always will to help others. and this approach
takes him to write this page.

You may like these posts

HackerRank Smart Number HackerRank XOR Strings 2


problem solution problem solution
 July 29, 2021  July 29, 2021

HackerRank Prime Dates


problem solution
 April 15, 2022

Post a Comment

2 Comments

GAJANAN KULKARNI
 January 20, 2022 at 11:19 PM

public static String timeConversion(String s) {


String ampm = s.substring(s.length()-2, s.length()-1);
StringBuffer sb = new StringBuffer(s.substring(0, s.length()-2));
int start = Integer.parseInt(s.substring(0, 2));
if(s.contains("P")){
String sb1 = start == 12 ? "12": ""+(start+12);
sb.replace(0, 2, "" +sb1);
System.out.println(sb);
} else {
String sb1 = start == 12 ? "00": start > 9? ""+start: "0" +start;
sb.replace(0, 2, "" +sb1);
System.out.println(sb);
}
return sb.toString();
}
}

Reply Delete

 Replies 
Reply

You might also like