Skip to content

Instantly share code, notes, and snippets.

@MohamedGamil
Created April 17, 2022 06:17
Show Gist options
  • Save MohamedGamil/1e4c313cd7e4954a2ce3b4faefddd879 to your computer and use it in GitHub Desktop.
Save MohamedGamil/1e4c313cd7e4954a2ce3b4faefddd879 to your computer and use it in GitHub Desktop.
HackerRank / Compare the Triplets
'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 'compareTriplets' function below.
*
* The function is expected to return an INTEGER_ARRAY.
* The function accepts following parameters:
* 1. INTEGER_ARRAY a
* 2. INTEGER_ARRAY b
*/
function compareTriplets(a, b) {
const score = [0,0];
for(let idx = 0; idx <= 2; idx++) {
const aliceScore = a[idx];
const bobScore = b[idx];
if (aliceScore > bobScore) {
score[0]++;
}
else if (aliceScore < bobScore) {
score[1]++;
}
}
return score;
}
function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
const a = readLine().replace(/\s+$/g, '').split(' ').map(aTemp => parseInt(aTemp, 10));
const b = readLine().replace(/\s+$/g, '').split(' ').map(bTemp => parseInt(bTemp, 10));
const result = compareTriplets(a, b);
ws.write(result.join(' ') + '\n');
ws.end();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment