https://www.freecodecamp.com/challenges/pairwise
The challenge description says that "If multiple pairs are possible that have the same numeric elements but different indices, return the smallest sum of indices. Once an element has been used, it cannot be reused to pair with another."
So for pairwise([0, 0, 0, 0, 1, 1], 1) this should produce a result of 4 (0 + 4) and not 10
function pairwise(arr, arg) {
var pairs;
var result;
pairs = arr.reduce( function(previous, current, index, array){
var values;
var notFound;
for( var i = 0; i < arr.length; i++ ){
if( i === index ){
continue;
}
if( arr[i]+arr[index] === arg ){
values = [ Math.min( arr[i], arr[index] ), Math.max( arr[i], arr[index] ) ];
notFound = true;
for( var j = 0; j < previous.length; j++ ){
if( previous[j][0][0] === values[0] &&
previous[j][0][1] === values[1] ){ // if same pair is found
notFound = false;
if( previous[j][1] > i+index ){ // if new sum is smaller, update it
previous[j][1] = i+index;
}
break;
}
}
if( notFound ){
previous.push( [values, i+index] );
}
break;
}
}
return previous;
}, []);
result = pairs.reduce( function(previous, current, index, array){
return previous + current[1];
}, 0);
return result;
}
pairwise([0, 0, 0, 0, 1, 1], 1);
@johnhuichen it should still return 10. What the challenge is saying is that when considering multiple
| Index | 0 | 1 | 2 | 3 | 4 | 5 |
| --- | --- | --- | --- | --- | --- | --- |
| Value | 0 | 0 | 0 | 0 | 1 | 1 |
You'll need to get a total of one for this case. To do so, you'll need to find combinations to consider, in this case, we can consider the pair (0, 1) (Indices 0 and 4) and (0, 1) (Indices 1 and 5). Take the lowest indices as a pair first (indices 0 and 4 = total of 4). You cannot reuse them anymore and so you'll need to consider the next pair with indices 1 and 5, which add to 6 so that your total is 6 + 4 = 10.
Reading the instructions again, I can see how this may be misinterpreted. Maybe we could rewrite the instructions as:
If multiple pairs are possible that have the same numeric elements but different indices, combine the elements so that they have the lowest possible indices. Then sum each pair of elements' indices, add the smallest sum to your total, and remove the pair with the smallest sum from future consideration. Once an element has been used, it cannot be reused to pair with another.
cc/ @FreeCodeCamp/issue-moderators feedback on my changes are welcome. Trying to balance clarity, with length/wordiness and ease of the challenge.
@erictleung I got the same impression as @johnhuichen. I did not understand why the test case did not align with the outline.
I think rather then making the outline anymore confusing by adding more words just change the test case. But if you must persist with the current test case add your example instead into the outline.
To do so, you'll need to find combinations to consider, in this case, we can consider the pair (0, 1) (Indices 0 and 4) and (0, 1) (Indices 1 and 5). Take the lowest indices as a pair first (indices 0 and 4 = total of 4). You cannot reuse them anymore and so you'll need to consider the next pair with indices 1 and 5, which add to 6 so that your total is 6 + 4 = 10.
Of course reducing the length. But again I want to reiterate, I feel everything is fine just the test case and that should be trivial to change.
Hi Eric and TopOne,
I understood what the question meant to say, when I received the debug log after failing the test the first time. The issue I wanted to bring up was related to wording of the problem:
The original question dictates that if:
1) multiple pair exists
2) these pairs have the same values
3) these pairs have different indices
only one pair with the smallest indices should be returned.
Using this sequential logic, ([0, 0, 0, 0, 1, 1], 1) should count index 0 and 4, where as index 1 and 5 will be another identical pair to index 0 and 4 (condition 1), have the same values (condition 2), have different indices (condition 3), therefore they should not be counted.
I understood what the designer wanted us to do. The solution was actually simpler if I didn't have to keep track of paired values. I brought the question to your attention so you may improve on it if you see fit. Otherwise I have already finished Frontend challenges and there is nothing for me to look back to.
@erictleung yep, the description is really confusing.
I'm with @johnhuichen on this one.
"If multiple pairs are possible that have the same numeric elements but different indices,"
Once a combination of '0' and '1' has been found, it should not be allowed again, generating unique combinations.
I agree that the wording of the instructions conflicts with the intent of that test case.
It may be clearer to state"It is possible to encounter multiple pairs that have the same numeric elements but different indices. " -with no additional instructions/words.
Also +1 on this.
The test case pairwise([0, 0, 0, 0, 1, 1], 1)===10
unambiguously conflicts with the description:
If multiple pairs are possible that have the same numeric elements but different indices, return the smallest sum of indices.
Here indices (0, 4) and indices (1, 5) have the same numeric elements [0, 1]. Therefore only the smaller sum of those two indices should be returned, for a sum of 4.
I believe the easiest solution is to remove the above quoted sentence entirely, as it describes a restriction that is not followed in the test case. OR: Change the test case to evaluate to 4.
I'm with @johnhuichen on this as well. The challenge text doesn't match the test cases.
@gristow explains why perfectly. If someone has a use case that requires the "If multiple pairs..." statement please include it in the challenge as a test case.
Thanks all for the feedback. The example pairwise([0, 0, 0, 0, 1, 1], 1)
demonstrates the confusing text. It is not well written to reflect what the test asks for. We can change the confusing text to:
It is possible to encounter multiple pairs that have the same numeric elements but different indices. Each element pair should use the lowest possible available indices. Once a pair of indices has been used, they cannot be used again.
@erictleung it looks like you didn't get around to making this copy change to the Pairwise challenge copy. Do you still think this is a change worth making?
@QuincyLarson I think the instructions are still worth changing. Strictly reading the instructions can be confusing, especially when you start looking at the challenges tests.
There hasn't been much activity on this issue, either by people coming to this issue or asking people online to clarify. Either way, I still think the instructions should be changed. I still like the second paragraph to be changed to this:
It is possible to encounter multiple pairs that have the same numeric elements but different indices. Each element pair should use the lowest possible available indices. Once a pair of indices has been used, they cannot be used again.
I think it makes it clear. I'll flag this as Help Wanted
and if my updated text still isn't clear enough, we can discuss it here.
I still would vote to just delete the sentence:
If multiple pairs are possible that have the same numeric elements but different indices, return the smallest sum of indices.
This resolves the issue and has the added advantage of making the question more readable.
@gristow I like the simplicity of the instructions after removing that sentence but it doesn't explain what to do if/when there are multiple pairs of elements like in the contested test of pairwise([0, 0, 0, 0, 1, 1], 1)
. Yes, you can look at the challenge tests but I think these instructions should also be present in the instructions themselves.
Ok -- I think I'm finally understanding what was meant by the original wording. How does this read to you, @erictleung?
You may use multiple pairs that have the same numeric elements but different indices. Each pair should use the lowest possible available indices. Once an element has been used it cannot be reused to pair with another element.
To me the word "encounter" is problematic as it suggests they may exist, but should not be used, whereas in fact they are expected to be used.
@gristow sounds good to me!
Happy to put in a PR for this unless you want to @erictleung.
@gristow I was having trouble understanding this challenge but the new wording you suggested cleared everything up for me. Thank you!
Most helpful comment
Hi Eric and TopOne,
I understood what the question meant to say, when I received the debug log after failing the test the first time. The issue I wanted to bring up was related to wording of the problem:
The original question dictates that if:
1) multiple pair exists
2) these pairs have the same values
3) these pairs have different indices
only one pair with the smallest indices should be returned.
Using this sequential logic, ([0, 0, 0, 0, 1, 1], 1) should count index 0 and 4, where as index 1 and 5 will be another identical pair to index 0 and 4 (condition 1), have the same values (condition 2), have different indices (condition 3), therefore they should not be counted.
I understood what the designer wanted us to do. The solution was actually simpler if I didn't have to keep track of paired values. I brought the question to your attention so you may improve on it if you see fit. Otherwise I have already finished Frontend challenges and there is nothing for me to look back to.