Skip to content

Instantly share code, notes, and snippets.

@tonythomas01
Last active May 25, 2020 09:46
Show Gist options
  • Save tonythomas01/9aa6bf46fe0eaaef1d7fb846f8b73ec2 to your computer and use it in GitHub Desktop.
Save tonythomas01/9aa6bf46fe0eaaef1d7fb846f8b73ec2 to your computer and use it in GitHub Desktop.
[Node.js] Kattis read test cases with limits
// 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