Skip to content

Instantly share code, notes, and snippets.

@depeele
Created May 22, 2016 00:06
Show Gist options
  • Save depeele/22e94e904a8ea9e4ef8fd0e78f0209ad to your computer and use it in GitHub Desktop.
Save depeele/22e94e904a8ea9e4ef8fd0e78f0209ad to your computer and use it in GitHub Desktop.
HackerRank Javascript stdin using an ES6 generator
'use strict';
function* Lines( stream ) {
const EOL = require('os').EOL;
const Fs = require('fs');
const buf = new Buffer(1);
while( true ) {
const chars = [];
while (Fs.readSync(stream.fd, buf, 0, buf.length)) {
let str = buf.toString();
if (str === EOL) { break; }
chars.push( str );
}
if (chars.length < 1) { break; }
yield chars.join('');
}
}
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 (or replace this loop entirely)
console.log("%d", N);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment