简体   繁体   中英

Sibice challenge open Kattis JavaScript not working

I am learning JavaScript, and my friend recommended open Kattis to solve tasks. (I see now that it might not be the best for JS).

Anyway, I am doing this challenge Sibice where you are checking if matches will fit a box. 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. I use readline to take input, since you need to take the input from terminal in open 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

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. Your program should be totally silent except for printing the exact expected output, nothing more or less. Stifle your UX instincts and focus on the algorithm.

I usually recommend the following approach for solving Kattis problems in JS:

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.

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.

See also:


Regarding code formatting, the standard is Prettier , which you can install locally or use in their online playground . I already applied it to your post.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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