CodingSpace

[HackerRank/Algorithms] Warmup - Time Conversion 본문

HackerRank/Algorithm

[HackerRank/Algorithms] Warmup - Time Conversion

개발자_조이킴 2022. 8. 23. 23:12

Problem. Warmup - Time Conversion


Link.

https://www.hackerrank.com/challenges/mini-max-sum/problem?isFullScreen=true 


Description.

Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers.

Then print the respective minimum and maximum values as a single line of two space-separated long integers.

 


Key Point. 


My Answer. 

'use strict';

const fs = require('fs');

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', function(inputStdin) {
    inputString += inputStdin;
});

process.stdin.on('end', function() {
    inputString = inputString.split('\n');

    main();
});

function readLine() {
    return inputString[currentLine++];
}

/*
 * Complete the 'timeConversion' function below.
 *
 * The function is expected to return a STRING.
 * The function accepts STRING s as parameter.
 */

function timeConversion(s) {
    // Write your code here
    let format = s.slice(8);
    let hour = s.slice(0, 2);
    let minAndSecond = s.slice(2, 8);
    
    if(format === 'PM' && Number(hour) < 12)
        hour = (Number(hour) + 12).toString()
    else if(format === 'AM' && Number(hour) === 12)
        hour = "00"
    
    return hour + minAndSecond;   

}

function main() {
    const ws = fs.createWriteStream(process.env.OUTPUT_PATH);

    const s = readLine();

    const result = timeConversion(s);

    ws.write(result + '\n');

    ws.end();
}

References. 

 

Comments