Last active
April 17, 2022 06:13
-
-
Save MohamedGamil/dc51336da7182d2de89cb16fe9867f2d to your computer and use it in GitHub Desktop.
HackerRank / Plus Minus
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
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 'plusMinus' function below. | |
* | |
* The function accepts INTEGER_ARRAY arr as parameter. | |
*/ | |
function plusMinus(arr) { | |
let n = arr.length, posRatio = 0, negRatio = 0, zeroRatio = 0; | |
const printDecimal = (num, places = 6) => { | |
const pow_ = Math.pow(10, places); | |
const num_ = (Math.round(num * pow_) / pow_).toFixed(places); | |
console.log(num_); | |
}; | |
for(let num of arr) { | |
if (0 === num) { | |
zeroRatio++; | |
} | |
else if (0 < num) { | |
posRatio++; | |
} | |
else if (0 > num) { | |
negRatio++; | |
} | |
} | |
printDecimal(posRatio / n); | |
printDecimal(negRatio / n); | |
printDecimal(zeroRatio / n); | |
} | |
function main() { | |
const n = parseInt(readLine().trim(), 10); | |
const arr = readLine().replace(/\s+$/g, '').split(' ').map(arrTemp => parseInt(arrTemp, 10)); | |
plusMinus(arr); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment