2

I am currently doing some tests at Kattis and I'm stuck with this one. The code I have written until now gives me the last else statement when console.logging in Visual Studio code. If I type a number below 100 it gives me the first if statement however Kattis only gives me errors. Where does the problem lie here?

I am using JavaScript (Nodejs).

Below is the code I am working on:

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

rl.on('line', (line) => {
    var n = line.split(' ');
    for (var i = 0; i < n.length; i++) {
        var r = parseInt(n[i][0]);
        var e = parseInt(n[i][1]);
        var c = parseInt(n[i][2]);
        if (r > e - c) {
            console.log("do not advertise");
        }
        else if (r < e - c) {
            console.log("advertise");
        } else {
            console.log("does not matter");
        }
    }
}); 
0

2 Answers 2

1

You could take a flag for getting the first line and if you got the line number, just split the line for getting the values.

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

var first = true;

rl.on('line', (line) => {
    if (first) {
        n = +line;
        first = false;
        return;
    }
    if (!n || !n--) return; // exit early for not needed lines

    var [r, e, c] = line.split(' ').map(Number); // take numbers

    if (r > e - c) {
        console.log("do not advertise");
    } else if (r < e - c) {
        console.log("advertise");
    } else {
        console.log("does not matter");
    }
}); 
Sign up to request clarification or add additional context in comments.

9 Comments

I'm not up-to-date with my JS, but should the first line have a semicolon line terminator?
Thanks, I thought that might be the case. I wondered if it could be fixed for the apparent inconsistency though - my OCD set off a big red alarm :-D
@NinaScholz Hi Nina! So in this problem I had only one first line which was n.. but if I have two first lines. How would I go about that? open.kattis.com/problems/muddyhike this one is a bit trickier and I am trying a nested for loop but not sure I am doing this correct. const readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }) rl.on('line', (line) => { var r, c; for (var i = 0; i < r; i++) { for (var j = 0; j <= c; j++) { console.log([i][j]) } } })
@Vicky: I created a gist that would read test cases with internal limits on Node gist.github.com/tonythomas01/9aa6bf46fe0eaaef1d7fb846f8b73ec2
|
0

Here's a slightly simpler option than the accepted answer using .once("line", ...) to skip the first line:

const readline = require("readline");

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

rl.once("line", line => // collect the first line; in this case, discard it
  rl.on("line", line => { // register another listener to handle the rest
    const [r, e, c] = line.split(/ +/).map(Number);

    if (e - c > r) {
      console.log("advertise");
    }
    else if (e - c < r) {
      console.log("do not advertise");
    }
    else {
      console.log("does not matter");
    }
  })
);

Kattis will automatically send EOF so there's no need to track n.

Promises also work:

const readline = require("readline");

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

rl.once("line", async () => {
  for await (const line of rl) {
    const [r, e, c] = line.split(/ +/).map(Number);

    if (e - c > r) {
      console.log("advertise");
    }
    else if (e - c < r) {
      console.log("do not advertise");
    }
    else {
      console.log("does not matter");
    }
  }
});

See Getting input in Kattis challenges - readline js for help with the general case when you need to colect data from the first line, and/or want to print a single result after all lines have been consumed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.