我有一个编码挑战即将到来,将使用HackerRank,这是我不熟悉的。在开始之前,我开始尝试熟悉它,想象一下当我在编辑器中看到这些样板代码时我有多惊讶!
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++];
}
关于HR的其他挑战都稍微修改了这个版本,我不禁想知道到底发生了什么。似乎有某种文本文件,编辑器正在读取,因此能够比较我的输出?
我非常感谢任何对此的见解,因为我很确定,当我进行编码挑战时,我将不得不编写我自己的Node"样板文件"。
谢谢!
代码基本上接收作为挑战输入所需的信息。这种特殊的方式使得输入的方式与挑战中描述的方式相同。
// 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++];
}
通常这段代码后面还有一些(在注释行下面),其中您已经获得了分配的变量,以使数据具有可操作的格式。
还有另一个版本的代码,它不能以咬块的形式传递:
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);
});
正如你所看到的,代码基本上是相同的,但是一些"使输入可用"是你必须在这个中做的事情。
别搞混了。我在这里解决的每一个挑战,我要做的第一件事就是console.log(输入)或任何其他预先创建的变量。它帮助我知道什么东西在哪里。