PROBLEM STATEMENT: Alice and Bob each created one problem for HackerRank. A reviewer rates the two challenges, awarding points on a scale from 1 to 100 for three categories: problem clarity, originality, and difficulty.
The task is to find their comparison points by comparing a[0] with b[0], a[1] with b[1], and a[2] with b[2].
If a[i] > b[i], then Alice is awarded 1 point.
If a[i] < b[i], then Bob is awarded 1 point.
If a[i] = b[i], then neither person receives a point.
Comparison points is the total points a person earned.
Given a and b, determine their respective comparison points.
SOLUTION:
step1: we declare our function, let’s call it compareTriplets
step2: declare counters to track the number of times elements inside each array is greater than, less than, or equal to each other
step3: declare arrays to collect the values of the counters declared in step2
step4: declare an iterator variable that gets the keys of the first Array(a)
step5: declare a for loop statement, this loop uses the iterator to compare values in the two arrays, set the counters to either increase or decrease, and finally push the counters to their arrays (finalArray1, finalArray2)
step6: we collect the last values from finalArray1 and finalArray2 respectively and pass them into an output array
step7: we run the function against all Hackerrank test cases
The code:
function compareTriplets(a,b) {
let counterA = 0;
let counterB = 0;
let finalArray1 = [];
let finalArray2 = [];
const iterator = a.keys();
for (const key of iterator) {
if (a[key] > b[key]) {
counterA++;
counterB += 0;
finalArray1.push(counterA);
finalArray2.push(counterB);
}
if (a[key] < b[key]) {
counterB++;
counterA += 0;
finalArray2.push(counterB);
finalArray1.push(counterA);
}
if (a[key] == b[key]) {
counterA += 0;
counterB += 0;
finalArray2.push(counterB);
finalArray1.push(counterA);
}
}
const res1 = finalArray1[finalArray1.length – 1];
const res2 = finalArray2[finalArray2.length – 1];
const output = [res1, res2];
console.log(output);
return output;
}
compareTriplets([1,2,3], [3,2,1])
compareTriplets([5,6,7], [3,6,10])
compareTriplets([17,18,30], [99,16,8])
compareTriplets([20,20,30], [20,20,50])
compareTriplets([6,8,12], [7,9,15])
compareTriplets([10,15,20], [5,6,7])
Comedian Lasisi Elenu Welcome his First Child (2022)
Comedian Lasisi Elenu Welcome his First Child Congratulations, as the famous Nigerian comedian, and actor Nosa Afolabi, popularly known as...
There are some problems with that code.
I mean, sure, the code works. It does what is intended. But the problem is that it does so in an unnecessarily complex way. The code is over-engineered, making it harder to understand and reason about.
First, you don’t need finalArray1 and finalArray2 at all. What you’re doing with them is… completely unneeded. Let’s briefly follow an execution and see what happens. You loop through a and b, get the first pair a[0] and b[0], and compare. You keep the count in countA and countB, and then you also push the current value of countA and countB into the arrays. You do the same for the second pair, a[1] and b[1], and again push the current values of countA and countB into the arrays. Again for the third pair a[2] and b[2].
Finally, after the loop you get the last value stored in each array. But here comes the catch: First you already have that value in countA and countB, and second why store the first values in the array if you’re going to discard them?
So, just remove finalArray1 and finalArray2. The score is already tracked in countA and countB.
A lesser, but also avoidable, problem is you sometimes explicitly do nothing. I mean things like doing countB+=0; This does nothing. And while it is sometimes good to be explicit, in this case it only adds noise. There’s the whole case of when a[i] === b[i]. In that case you really don’t do anything at all. No scores need to be added to either counter. You can just remove that whole if block and the code would still be correct.
Finally, going for an iterator in such a basic loop is, again, noisy. I mean, yes, you learned how to use iterators, you want to use them every chance you get. I understand that. But… you also need to learn not to overuse things. You need to simplify. Aiming for the simpler solution is always a good idea.
So… to recap:
– Just use one place to store your score. In this case, countA and countB. Remove the finalArrayX because they serve no purpose.
– When a condition means you shouldn’t do something, then just don’t do it. If you’re not doing anything at all in one case, remove it altogether.
– Reach for simpler, cleaner tools. Use an index (i, from 0 to a.length) when it is enough to use an index.
– As an added bonus, use else when conditions are mutually exclusive. if (a[i] > b[i]) … ELSE if (a[i] < b[i]) … This helps to explicitly communicate that the conditions are indeed mutually exclusive and that you only apply one of them on each loop iteration.