I have a coding challenge coming up that will be using HackerRank, which I am unfamiliar with. I started trying to get familiar with it before I started and imagine my surprise when I saw this boilerplate-ish code in the editor!
process.stdin.resume();
process.stdin.setEncoding('ascii');
var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;
process.stdin.on('data', function (data) {
input_stdin += data;
});
process.stdin.on('end', function () {
input_stdin_array = input_stdin.split("\n");
main();
});
function readLine() {
return input_stdin_array[input_currentline++];
}
The rest of the challenges on HR have slightly modified versions of this and I can't help but wonder what is really going on. It seems like there is some sort of text file that the editor is reading from and is therefore able to compare to my output?
I'd really appreciate any insight into this as I am pretty sure that I will have to write my own Node "boilerplate" when I do my coding challenge.
Thanks!
The code basically receives the information you need as input for the challenges. This particular one makes it so that the input comes through the same way the describe it in the challenge.
// Begin reading from stdin so the process does not exit. (basically reading from the command line)
process.stdin.resume();
//set the enconding for received data to ascii so it will be readable
process.stdin.setEncoding('ascii');
//declare variables to process the data
var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;
//if data is coming through, put it in the input_stdin string. keep receiving data until no more comes through
process.stdin.on('data', function (data) {
input_stdin += data;
});
//after the transmission when the end signal is received break the string up and push each new line (\n == new line) as an element into the array.
process.stdin.on('end', function () {
input_stdin_array = input_stdin.split("\n");
main();
});
//gives you one element per line to work with.
function readLine() {
return input_stdin_array[input_currentline++];
}
Usually this code is followed by some more (below the comment line) where you already get variables assigned to have the data in a workable format.
There is another version of the code that doesn't deliver as bite sized chunks:
function processData(input) {
//Enter your code here
}
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});
As you can see, the code is basically the same but some of the boiler plate 'making the input usable' is something you have to do in this one.
Just don't be confused. Every challenge I solve there, the first thing I do is console.log(input) or any other pre-created variable. it helps me knowing what is actually where.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With