Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
66
rated 0 times [  71] [ 5]  / answers: 1 / hits: 9212  / 4 Years ago, sun, october 11, 2020, 12:00:00

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.


More From » stdout

 Answers
7

  • 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',''));
});
}

[#2511] Wednesday, October 7, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
willieelisham

Total Points: 201
Total Questions: 108
Total Answers: 106

Location: Zambia
Member since Sat, Oct 31, 2020
4 Years ago
willieelisham questions
;