Hi I am having issues getting stdin input in a program. I have the following ruby snippet that I want to translate to javascript that runs on node.js.
t = $stdin.readline().to_i
t.times do ||
n,m = $stdin.readline().split().map{|num| num.to_i}
m_arr = $stdin.readline().split().map{|num| num.to_i}
end
I read in t which is the number of test cases. In the t.times block, I read two integers on the same line, n &m, then on the following line I read an array of integers. I do this t times. So for the following input.
2
4 1
1
6 2
2 3
2 would be read into t in the first $stdin.readline. Then 4 would be read into n and 1 would be read into n in the second $stdin.readline. Then [1] would be read into m_arr in the third $stdin.readline. We would repeat the last two $stdin.readline for one more time. So in the second time, 6 would be read into m and 2 would be read into n, and then [2,3] would be read into m_arr. Can someone tell how to do this in javascript for node.js in the best possible way.
After some research and getting to understand that node.js is event driven. I came up with the following solution.
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
input = [];
rl.on('line',function(line){
input.push(line);
}).on('close',function(){
// process the input data in main
main(input);
process.exit(0);
});
function main(){
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.