-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03-Compare_The_Triplets.js
82 lines (59 loc) · 2.15 KB
/
03-Compare_The_Triplets.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
* Created on Sat Feb 19 2022
* @author Carlos Páez
*/
'use strict'
const fs = require('fs')
process.stdin.resume()
process.stdin.setEncoding('utf-8')
let inputString = ''
let currentLine = 0
/* Reading the input from the user and storing it in the variable `inputString` */
process.stdin.on('data', (inputStdin) => {
inputString += inputStdin
})
/* Reading the input from the user and storing it in the variable `inputString` */
process.stdin.on('end', () => {
inputString = inputString.split('\n')
main()
})
/**
* It reads a line from the input string and returns it
*/
const readLine = () => inputString[currentLine++]
/*
* Complete the `compareTriplets` function below
* The function is expected to return an INTEGER_ARRAY
* The function accepts following parameters:
* 1. INTEGER_ARRAY a
* 2. INTEGER_ARRAY b
*/
/**
* Given two arrays of integers, return an array of two integers where the first element is the number
* of elements in the first array that are greater than the corresponding element in the second array
* and the second element is the number of elements in the second array that are greater than the
* corresponding element in the first array
* @param a - an array of integers representing Alice's challenge rating
* @param b - an array of integers representing Alice's challenge rating
* @returns An array of two integers.
*/
const compareTriplets = (a, b) => {
let points = [0, 0]
let maxLen = (a.length > b.length) ? a.length : b.length
for (let i = 0; i <= maxLen; i++) {
if (a[i] > b[i] && (1 <= a[i] <= 100 && 1 <= b[i] <= 100)) points[0] += 1
else if (a[i] < b[i] && (1 <= a[i] <= 100 && 1 <= b[i] <= 100)) points[1] += 1
}
return points
}
/**
* Compare two arrays and return the sum of the differences between the two arrays
*/
const main = () => {
const ws = fs.createWriteStream('./output.txt')
const a = readLine().replace(/\s+$/g, '').split(' ').map(aTemp => parseInt(aTemp, 10))
const b = readLine().replace(/\s+$/g, '').split(' ').map(bTemp => parseInt(bTemp, 10))
const result = compareTriplets(a, b)
ws.write(result.join(' ') + '\n')
ws.end()
}