Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
76
rated 0 times [  83] [ 7]  / answers: 1 / hits: 66321  / 7 Years ago, thu, april 20, 2017, 12:00:00

I am a newbie to competitive programming. The only language I know is Javascript but if I select javascript option I couldn't even understand how to get input and how to print output in both the sites for some problems is Hackerrank the code looks like this


function processData(input) {
//Enter your code here
}
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});

process.stdin.on("end", function () {
processData(_input);
});

And in the same Hackerrank for some problems the initial code looks like this


process.stdin.resume();
process.stdin.setEncoding('ascii');

var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;

process.stdin.on('data', function (data) {
input_stdin += data;
});

process.stdin.on('end', function () {
input_stdin_array = input_stdin.split("n");
main();
});

function readLine() {
return input_stdin_array[input_currentline++];
}

/////////////// ignore above this line ////////////////////

function main() {
var n = parseInt(readLine());
}

Whereas in Hackerearth the initial code look like this


   function main(input) {
//Enter your code here
process.stdout.write("Hello World!");
}

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);
});


If someone gives me an example of a program how to get the inputs and print output in those sites or any solved program of those sites using javascript also will do I guess.


More From » node.js

 Answers
10

Let's take a simple example from HackerEarth:
https://www.hackerearth.com/practice/basic-programming/input-output/basics-of-input-output/practice-problems/algorithm/find-factorial/



To provide the solution, you need to do this:



function main(input) {
//Enter your code here
var num = parseInt(input, 10);//This line expects input to be a string so convert to an int as per problem
var res=1;
for(var i=num;i>1;i--) {
res *= i;
}
process.stdout.write(res);//This is how you write output.
}


EDIT:



Here is how you could do it in hackerrank:



function main() {
var n = parseInt(readLine());
var strN = n.toString();//<-- Convert int n to string
for(var i=1;i<=10;i++) {
process.stdout.write(strN+ x +i+ = +n*i);//<-- formatting the
//question requires
process.stdout.write(n);//<-- newline
}
}


The difference seems to be that in HackerRank, you need to convert the output to string yourself.
Hope it helps!



EDIT2:



For multiline input like:



5 1
1 2 3 4 1


You can do this:



function main(input) {
//Enter your code here
var data = input.split('n');
var firstLine = data[0].split(' ');
var len = firstLine[0];
//process.stdout.write('length:'+len);
var toFind = firstLine[1];
//process.stdout.write('toFind:'+toFind);
//process.stdout.write('n');
var arr = data[1].split(' ');
//process.stdout.write(arr);
for(var i=len-1;i>=0;i--) {
if(arr[i] == toFind){
process.stdout.write(i+1);
return;
}
}
process.stdout.write(-1);
}


Notice that input is multi-line, so first you need to split it into lines by doing var data = input.split('n');.
Each split will give you string with spaces in between. So, to get individual characters, you have to split again but this time with space like var firstLine = data[0].split(' ');.
Once you have all the input, you are left with writing your own algorithm.
Notice that I have left comments too so that you know how to debug in the editor itself.



By the way this solution also works and is an accepted solution.



Hope this helps too!


[#58068] Wednesday, April 19, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
josefn

Total Points: 251
Total Questions: 93
Total Answers: 84

Location: Senegal
Member since Fri, Aug 21, 2020
4 Years ago
;