Skip to content

Instantly share code, notes, and snippets.

@depeele
Created May 22, 2016 00:18
Show Gist options
  • Save depeele/66656a73bf53773ae852f6613ae05876 to your computer and use it in GitHub Desktop.
Save depeele/66656a73bf53773ae852f6613ae05876 to your computer and use it in GitHub Desktop.
HackerRank Javascript stdin using an ES6 generator and larger buffer
'use strict';
function* Lines( stream ) {
const EOL = require('os').EOL;
const Fs = require('fs');
const buf = new Buffer(1024);
while (Fs.readSync(stream.fd, buf, 0, buf.length)) {
let lines = buf.toString().split( EOL );
if (lines.length < 1) { break; }
for (let idex in lines) {
yield lines[idex];
}
}
}
const lines = Lines( process.stdin );
for (let T = 0, end = parseInt( lines.next().value ); T < end; T++) {
let N = parseInt( lines.next().value );
// Do your work here.
console.log("%d", N);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment