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");
        }
    }
}); 
Machavity
  • 30,841
  • 27
  • 92
  • 100
Vicky
  • 47
  • 10

2 Answers2

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");
    }
}); 
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • I'm not up-to-date with my JS, but should the first line have a semicolon line terminator? – halfer May 06 '19 at 21:41
  • @halfer, no, please have a look here: [What are the rules for JavaScript's automatic semicolon insertion (ASI)?](https://stackoverflow.com/q/2846283/). – Nina Scholz May 07 '19 at 07:04
  • 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` – halfer May 07 '19 at 07:54
  • @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? https://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 May 09 '19 at 17:35
  • you have still a single line for the dimension of the following data and thi can be used in the next block, like above. – Nina Scholz May 09 '19 at 17:44
  • @NinaScholz oh okay like the code from the past problem? the difference here is that there are three inputs and three outputs in this problem. With the little I have written so far. would you say I am getting anywhere close? Sorry for the bother. I just started a few months ago. Know very little about algorithms in programming. Trying to learn the process. – Vicky May 09 '19 at 17:52
  • for *Muddy Hike*, you need to take the first line, split it and get the values for the wanted lines and width of the following data. this part beside of the splitting of the first line is basically the same as the soulution above. the reading of the following lines, the above solution works as well for it, despite of the different purpose. – Nina Scholz May 09 '19 at 18:07
  • maybe you ask a new question and present the code there. the comment section is not reall suited for problems of another problem. – Nina Scholz May 09 '19 at 18:19
  • 1
    @Vicky: I created a gist that would read test cases with internal limits on Node https://gist.github.com/tonythomas01/9aa6bf46fe0eaaef1d7fb846f8b73ec2 – tonythomas01 May 25 '20 at 09:42
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.

ggorlen
  • 44,755
  • 7
  • 76
  • 106