Last active
May 25, 2020 09:46
-
-
Save tonythomas01/9aa6bf46fe0eaaef1d7fb846f8b73ec2 to your computer and use it in GitHub Desktop.
[Node.js] Kattis read test cases with limits
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
// Assume you have to read something like: | |
// 3 - number of test cases that follow | |
// 3 - limit in this test case. | |
// 1 2 3 - test case data | |
// 4 | |
// 1 2 3 4 | |
const readline = require('readline') | |
const rl = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout | |
}); | |
let firstLine = true; | |
let numberOfTestCasesLine = true; | |
let totalTestCasesCount = undefined; | |
let testCasesProcessedCount = 0; | |
let shouldProcessTestCaseLimit = true; | |
let testCaseLimit; | |
rl.on('line', (line) => { | |
if (firstLine) { | |
totalTestCasesCount = +line; | |
firstLine = false; | |
return; | |
} | |
// Ignore this first line now ? | |
if (shouldProcessTestCaseLimit) { | |
shouldProcessTestCaseLimit = false; | |
testCaseLimit = +line; | |
return; | |
} | |
console.log(`Processing test case line ${line}, with testCaseLimit: ${testCaseLimit}`); | |
const numberArray = line.split(' ').map(Number); | |
console.log("numberArray", numberArray); | |
// Do your magic here. | |
shouldProcessTestCaseLimit = true; | |
testCasesProcessedCount++; | |
if (totalTestCasesCount <= testCasesProcessedCount) { | |
rl.close(); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment