-
Notifications
You must be signed in to change notification settings - Fork 24
Solution and tests for issue 19, find pairs that sum to x #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| // Find pairs that sum to x | ||
| // Return array of pairs from an input array of numbers that adds up to second input x. Each number can be used only once. | ||
| // input: [1,2,3,4,5], 6 | ||
| // output: [[1,5], [2,4]] | ||
|
|
||
| // Solution by Zach Nagatani @zachnagatani | ||
|
|
||
| /** | ||
| * Returns an multidimensional array containing pairs of numbers from an passed in array which add up to the passed in sum | ||
| * Each integer can only be used to form one pair | ||
| * @param {number[]} arr - An array of numbers to search through | ||
| * @param {number} sum - The sum to use as the basis of the search | ||
| * @returns {number[][]} pairs - A multidimensional array of pairs that add up to the sum | ||
| */ | ||
| const solution = (arr, sum) => { | ||
| let unusedNums = arr.slice(), | ||
| pairs = [], | ||
| head = 0, | ||
| tail = unusedNums.length - 1; | ||
|
|
||
| while (head < tail) { | ||
| let currentSum = unusedNums[head] + unusedNums[tail]; | ||
|
|
||
| if (currentSum === sum) { | ||
| pairs.push([unusedNums[head], unusedNums[tail]]); | ||
| unusedNums.splice(head, 1); | ||
| unusedNums.splice(tail, 1); | ||
|
|
||
| head = 0; | ||
| tail = unusedNums.length - 1; | ||
| } else if (currentSum < sum) { | ||
| head++; | ||
| } else { | ||
| tail--; | ||
| } | ||
| } | ||
|
|
||
| return pairs.length ? pairs : null; | ||
| }; | ||
|
|
||
| module.exports = { | ||
| solution | ||
| }; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| const expect = require('chai').expect; | ||
| let solution = require('../solutions/19').solution; | ||
| //solution = require('../yourSolution').solution; | ||
|
|
||
| describe('Array pairs that sum to x', () => { | ||
| it('should return an array of pairs of integers whose sum equal to x', () => { | ||
| const result = solution([1,2,3,4,5,6], 6); | ||
| expect(result).to.deep.equal([[1,5], [2,4]]); | ||
|
|
||
| const result2 = solution([2,1,3,4,5,6], 6); | ||
| expect(result).to.deep.equal([[1,5], [2,4]]); | ||
| }); | ||
|
|
||
| it('should only use an integer once in its output', () => { | ||
| const result = solution([1,3,3,3], 4); | ||
| expect(result).to.deep.equal([[1,3]]); | ||
| }); | ||
|
|
||
| it('should return null of no pairs are found', () => { | ||
| const result = solution([1,2,3,4,5,6], 40); | ||
| expect(result).to.equal(null); | ||
| }); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return [] makes more sense to represent an empty set.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. Also, I've realized my solution only works on a sorted array. Is that a reasonable constraint, or should I rework it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah I was going to point that something was off. We can't assume its a sorted array. Real life scenario: We get a list of test scores and we want to pair people up so that the teams are even. We can't assume the list of test scores are sorted.