HackerEarth: How to read from STDIN and write to STDOUT?

Viewed 7366

Is anybody here who solves problems on HackerEarth? I am confused with the way they supply the input data.

I have been using Leetcode to date to solve problems and I am pretty happy with them but unfortunately, some people prefer HackerEarth to host coding challenges and I have issues trying to read the input test case properly.

Take this for example https://www.hackerearth.com/practice/algorithms/searching/ternary-search/practice-problems/algorithm/small-factorials/submissions/

I did my research and found their "solution guide" which has the wrong info: https://www.hackerearth.com/docs/wiki/developers/solution-guide/

How would I read the individual lines and output the results in JS (Node v10) judge?

Thank you.

3 Answers
  • Just logged into and looked it up here.

  • Seems to be similar to HackerRank which I'm not fond of. (LeetCode's UI is fun and much easier to use.)

  • On LeetCode, we don't have to print things out, here it seems we have to print the output (for instance in JavaScript we would use console.log not to mention printing inside methods is generally a bad practice coding).

This solution (copied from one of those activities) seems to be passing based on which you can figure things out:


/*
// Sample code to perform I/O:

process.stdin.resume();
process.stdin.setEncoding("utf-8");
var stdin_input = "";

process.stdin.on("data", function (input) {
    stdin_input += input;                               // Reading input from STDIN
});

process.stdin.on("end", function () {
   main(stdin_input);
});

function main(input) {
    process.stdout.write("Hi, " + input + ".\n");       // Writing output to STDOUT
}

// Warning: Printing unwanted or ill-formatted data to output will cause the test cases to fail
*/

// Write your code here


process.stdin.resume();
process.stdin.setEncoding("utf-8");
var stdin_input = "";
process.stdin.on("data", function (input) {
    stdin_input += input;
});
process.stdin.on("end", function () {
   main(stdin_input);
});
function main(input) {
    input = input.split('\n');
    input.shift();
    input.forEach(n => {
        n = parseInt(n);
        let fact = BigInt(1);
        while(n){
            fact = BigInt(fact) * BigInt(n);
            n--;
        }
        console.log(String(fact).replace('n',''));
    });
}

in accessing inputs HackerEarth bit different compare to leetcode and Hacker Rank here you will have to extract inputs from standard input (STDIN) by line number you can find more details here

for example, if the format of the input given as below
Input format:
First line contains integer N.
Second line contains string S.

then you will split the STDIN by new line("\n") and access each input line-wise

below is a sample of how would you access inputs in JavaScript

var input1 = 0;
var input2 = "";
var stdin_input = ""
process.stdin.on("data", function (input) {
    // Reading input from STDIN
    stdin_input += input;  
    input1 = stdin_input.split("\n")[0] 
    input2 = stdin_input.split("\n")[1]

    console.log("input1 = ", input1)
    console.log("input2 = ", input2)
                       
                             
});

1- First step --> Find the first occurrence of a number using Regex \d+ (No need to add the global flag as the problem stated that the first line always contains a number, thus we are looking for the first number). The + flag is to always match more than one occurrence of a digit like 2,12,21,234, and so on.

2-Second Step --> Cast the matched string to a number to multiply it by two (double it), then cast it back to a string and make the replacement using the regex replace methods. More about the method can be found here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace

process.stdin.resume();
process.stdin.setEncoding("utf-8");
var stdin_input = "";

process.stdin.on("data", function (input) {
    stdin_input += input;                             
});

process.stdin.on("end", function () {
   main(stdin_input);
});

function main(input) {
    const regex = /(\d+)/g;
    let expr = input.replace(regex,(match) => {
        return String(Number(match) * 2);
    });
    process.stdout.write(expr);    
}

Related