The code defines a function to calculate the count of apples and oranges. The function is written in such a way that it can be read from a string. The input string is split into lines using the "
" character as a delimiter. Each line is read and the following information is stored:
The first number is the number of the apple
The second number is the number of the orange
The third number is the value of the apple
The fourth number is the value of the orange
The fifth number is the total number of apples and oranges
The main() function uses the readLine() function to read in the input string. The input string is split into lines and the following variables are defined:
s - the value of the apples
t - the value of the oranges
ab - the number of apples in the input string
a - the number of oranges in the input string
b - the total number of apples and oranges
mn - the number of oranges in the input string
m - the number of apples in the input string
n - the number of oranges in the input string
Each line in the input string is split on the "
'use strict';
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', inputStdin => {
inputString += inputStdin;
});
process.stdin.on('end', _ => {
inputString = inputString.replace(/\s*$/, '')
.split('\n')
.map(str => str.replace(/\s*$/, ''));
main();
});
function readLine() {
return inputString[currentLine++];
}
// Complete the countApplesAndOranges function below.
function countApplesAndOranges(s, t, a, b, apples, oranges) {
// Calculate apples
let appleCount = 0, orangeCount = 0, p1 = 0, p2 = 0;
while (p1 < apples.length || p2 < oranges.length) {
// apple count
if (apples[p1] >= 0) {
let appleVal = a + apples[p1];
if (appleVal >= s && appleVal <= t) {
appleCount += 1
p1 += 1;
} else {
p1 += 1
}
} else {
p1 += 1;
}
// oranges count
if (oranges[p2] < 0) {
let orangeVal = b + oranges[p2];
if (orangeVal >= s && orangeVal <= t) {
orangeCount += 1;
p2 += 1;
} else {
p2 += 1;
}
} else {
p2 += 1;
}
}
console.log(appleCount);
console.log(orangeCount);
}
function main() {
const st = readLine().split(' ');
const s = parseInt(st[0], 10);
const t = parseInt(st[1], 10);
const ab = readLine().split(' ');
const a = parseInt(ab[0], 10);
const b = parseInt(ab[1], 10);
const mn = readLine().split(' ');
const m = parseInt(mn[0], 10);
const n = parseInt(mn[1], 10);
const apples = readLine().split(' ').map(applesTemp => parseInt(applesTemp, 10));
const oranges = readLine().split(' ').map(orangesTemp => parseInt(orangesTemp, 10));
countApplesAndOranges(s, t, a, b, apples, oranges);
}