Last active
June 9, 2022 19:01
-
-
Save brianmugweru/a9789fed31416245e62f3e57b35adc75 to your computer and use it in GitHub Desktop.
Hackerranks(Diagonal Difference)
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'; | |
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 'diagonalDifference' function below. | |
* | |
* The function is expected to return an INTEGER. | |
* The function accepts 2D_INTEGER_ARRAY arr as parameter. | |
*/ | |
function diagonalDifference(arr) { | |
let ltrsum = 0 | |
let rtlsum = 0 | |
for(let i = 0; i < arr.length; i++) { | |
ltrsum += arr[i][i] | |
rtlsum += arr[i][arr.length - 1 - i] | |
} | |
return Math.abs(ltrsum - rtlsum) | |
} | |
function main() { | |
const ws = fs.createWriteStream(process.env.OUTPUT_PATH); | |
const n = parseInt(readLine().trim(), 10); | |
let arr = Array(n); | |
for (let i = 0; i < n; i++) { | |
arr[i] = readLine().replace(/\s+$/g, '').split(' ').map(arrTemp => parseInt(arrTemp, 10)); | |
} | |
const result = diagonalDifference(arr); | |
ws.write(result + '\n'); | |
ws.end(); | |
} |
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) { | |
// Write your code here | |
const positiveRatio = arr.filter(item => item > 0).length / arr.length | |
const negativeRatio = arr.filter(item => item < 0).length / arr.length | |
const zeroRatio = arr.filter(item => item === 0).length / arr.length | |
console.log(positiveRatio.toFixed(6)) | |
console.log(negativeRatio.toFixed(6)) | |
console.log(zeroRatio.toFixed(6)) | |
} | |
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