简体   繁体   English

Sibice 挑战打开 Kattis JavaScript 不工作

[英]Sibice challenge open Kattis JavaScript not working

I am learning JavaScript, and my friend recommended open Kattis to solve tasks.我正在学习JavaScript,朋友推荐打开Kattis解决任务。 (I see now that it might not be the best for JS). (我现在知道它可能不是 JS 的最佳选择)。

Anyway, I am doing this challenge Sibice where you are checking if matches will fit a box.不管怎样,我正在做这个Sibice挑战,你要检查火柴是否适合一个盒子。 Read the challenge linked for the context.阅读上下文链接的挑战。

I am solving the problem, but I am not passing any of the tests Kattis has for the problem.我正在解决问题,但我没有通过 Kattis 针对该问题进行的任何测试。 I use readline to take input, since you need to take the input from terminal in open Kattis.我使用readline获取输入,因为您需要在打开的 Kattis 中从终端获取输入。

Attached is my code.附件是我的代码。 Sorry if it's messy (will take tips on that too haha), do you have any ideas?对不起,如果它很乱(也会接受提示哈哈),你有什么想法吗? Also my first question, so I apologize if I haven't submitted it perfectly!这也是我的第一个问题,如果我没有完美提交,我深表歉意!

This is my code, and when I run it, it works fine.这是我的代码,当我运行它时,它工作正常。 But Kattis is not accepting it.但卡蒂斯不接受。

Sample input and output示例输入和 output

const readline = require("readline");

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

rl.question(
  "Enter the number of matches and the width and height of the box, separated by spaces: ",
  input => {
    const [numMatches, width, height] = input
      .split(" ")
      .map(Number);

    const length = Math.sqrt(width * width + height * height);

    const matchLengths = [];
    const matchArray = () => {
      return new Promise(resolve => {
        rl.question("match length?", matchLength => {
          matchLengths.push(matchLength);
          resolve();
        });
      });
    };

    const askQuestion = async numMatches => {
      for (i = 0; i < numMatches; i++) {
        await matchArray();
      }
      rl.close();
    };
    askQuestion(numMatches).then(() => {
      matchLengths.forEach(element => {
        console.log(element <= length ? "DA" : "NE");
      });
    });
  },
);

Avoid rl.question , which prints a prompt to standard out, the stream that Kattis is looking at to determine whether your program is correct.避免rl.question ,它打印标准输出的提示,即 Kattis 正在查看的 stream 以确定您的程序是否正确。 Your program should be totally silent except for printing the exact expected output, nothing more or less.您的程序应该完全静默,除了打印预期的确切 output,仅此而已。 Stifle your UX instincts and focus on the algorithm.扼杀你的用户体验直觉,专注于算法。

I usually recommend the following approach for solving Kattis problems in JS:我通常推荐以下方法来解决 JS 中的 Kattis 问题:

const readline = require("readline");

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

rl.once("line", line => {
  // process preamble

  rl.on("line", line => {
    // process line
  });

  rl.on("close", () => {
    // print result of computation that requires all lines
  });
});

However, for this problem, each test case can be computed and printed after each line, so you can skip the close listener and array:然而,对于这个问题,每个测试用例都可以在每一行之后计算和打印,所以你可以跳过关闭监听器和数组:

const readline = require("readline");

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

rl.once("line", line => {
  const [n, w, h] = line.split(" ").map(Number);
  const length = Math.sqrt(w * w + h * h);
  rl.on("line", line => console.log(line > length ? "NE" : "DA"));
});

If you really want to use promises, this was also accepted:如果你真的想使用承诺,这也被接受:

const readline = require("readline");

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

const read = () => new Promise(r => rl.once("line", line => r(line)));

(async () => {
  const [n, w, h] = (await read()).split(" ").map(Number);
  const length = Math.sqrt(w * w + h * h);

  for (let i = 0; i < n; i++) {
    console.log((await read()) > length ? "NE" : "DA");
  }
})();

That said, I vaguely recall running into issues with promisification on other Kattis problems, and promises don't seem to offer much of an elegance boost here anyway, so I'd stick to plain callbacks.也就是说,我依稀记得在其他 Kattis 问题上遇到过 promisification 的问题,而且 promises 似乎并没有提供太多的优雅提升,所以我会坚持使用简单的回调。

As an aside, your for (i = 0; i < numMatches; i++) { should use let i to avoid introducing a globally-scoped variable that can easily lead to bugs.顺便说一句,您的for (i = 0; i < numMatches; i++) {应该使用let i以避免引入容易导致错误的全局范围变量。

See also:也可以看看:


Regarding code formatting, the standard is Prettier , which you can install locally or use in their online playground .关于代码格式化,标准是Prettier ,您可以在本地安装或在他们的在线游乐场中使用。 I already applied it to your post.我已经将它应用到您的帖子中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM