Last active
June 16, 2017 01:32
-
-
Save martinmckenna/4cdd7e0a83b0ad008e1206329a3514d3 to your computer and use it in GitHub Desktop.
Test for VIP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Write a program that allows for an integer array to be passed in and will then output all of the pairs that sum up to 10. | |
//Please provide a solution that allows for 1) output all pairs (includes duplicates and the reversed ordered pairs), | |
//2) output unique pairs only once (removes the duplicates but includes the reversed ordered pairs), | |
//and 3) output the same combo pair only once (removes the reversed ordered pairs). | |
//find pairs of ints that add to 10 | |
const pairsSumToTen = function getPairs(arrayOfInts) { //takes an array of ints as an argument | |
let result = []; //used for every pair of numbers that add to ten | |
for (i = 0; i < arrayOfInts.length; i++) { //map over the array | |
let arrayWithoutIndex = arrayOfInts.filter(function (element, index) { | |
return index != i; | |
}); //make a new array but filter out the index | |
for (k = 0; k < arrayWithoutIndex.length; k++) { //then map over that array and compare the index to every other int | |
if (arrayOfInts[i] + arrayWithoutIndex[k] === 10) { //if the two values add to 10, make an array with those two ints | |
result.push([arrayOfInts[i], arrayWithoutIndex[k]]); | |
} | |
} | |
} | |
return result; | |
} | |
// remove all duplicates from the mutlidimensional array. Disregard reverse | |
// order matches | |
const getUniquePairs = function uniquePairs(arrayOfPairs) { //takes an multidimensional array of ints | |
arrayOfPairs.sort(); //first, let's sort the array so it's simpler to compare each element | |
let result = []; | |
for (let i = 0; i < arrayOfPairs.length - 1; i++) { //map over every element except the last | |
if (arrayOfPairs[i].join('') !== arrayOfPairs[i + 1].join('')) { //turn the arrays into strings and compare them that way. Using JSON.stringify is poor practice | |
result.push(arrayOfPairs[i]); | |
} | |
} | |
let lastElement = arrayOfPairs.pop(); //the last element in the array is always going to be unique, so always push it to the result array | |
result.push(lastElement); | |
return result; | |
} | |
// remove all duplicates from multidimensional array - even the reverse order | |
// pairs | |
const getReverseOrderPairs = function reverseOrder(array) { | |
let sortedElements = array.map(function (value, index, array) { | |
return value.sort(); //sort each element in the array so it's easier to filter later | |
}) | |
sortedElements.sort(); //then sort the outer-most array | |
let result = getUniquePairs(sortedElements); //since they're already sorted, all we have to do is get rid of the unique pairs | |
return result; | |
} | |
//TEST CASES: | |
console.log(pairsSumToTen([ | |
1, | |
1, | |
2, | |
4, | |
4, | |
5, | |
5, | |
5, | |
6, | |
7, | |
9 | |
])); //result: [[1, 9], [1, 9], [4, 6], [4, 6], [5, 5], [5, 5], [5, 5], [5, 5], [5, 5], [5, 5], [6, 4], [6, 4], [9, 1], [9, 1]] | |
console.log(getUniquePairs(pairsSumToTen([ | |
1, | |
1, | |
2, | |
4, | |
4, | |
5, | |
5, | |
5, | |
6, | |
7, | |
9 | |
]))); //result: [[1, 9], [4, 6], [5, 5], [6, 4], [9, 1]] | |
console.log(getReverseOrderPairs(pairsSumToTen([ | |
1, | |
1, | |
2, | |
4, | |
4, | |
5, | |
5, | |
5, | |
6, | |
7, | |
9 | |
]))); //result: [[1, 9], [4, 6], [5, 5]] | |
console.log(pairsSumToTen([ | |
0, | |
1, | |
2, | |
3, | |
5, | |
7, | |
10, | |
3, | |
5 | |
])); //[[0, 10], [3, 7], [5, 5], [7, 3], [7, 3], [10, 0], [3, 7], [5, 5]] | |
console.log(getUniquePairs(pairsSumToTen([ | |
0, | |
1, | |
2, | |
3, | |
5, | |
7, | |
10, | |
3, | |
5 | |
]))); //[[0, 10], [10, 0], [3, 7], [5, 5], [7, 3]] | |
console.log(getReverseOrderPairs(pairsSumToTen([ | |
0, | |
1, | |
2, | |
3, | |
5, | |
7, | |
10, | |
3, | |
5 | |
]))); //[[0, 10], [3, 7], [5, 5]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment