-
Notifications
You must be signed in to change notification settings - Fork 24
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
Solution and tests for Largest Sum in an Array, #20 #49
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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,31 @@ | ||
// Largest Sum in an Array | ||
// Return the largest sum of 2 consecutive numbers given an array | ||
// input: [1, 2, 9, 0, 10] | ||
// output: 11 | ||
|
||
// Zach Nagatani | ||
|
||
/** | ||
* Uses a greedy approach to track the largestSum so far, updating as necessary | ||
* @param {Number[]} arr - an array of numbers | ||
* @returns {Number} largestSum - the largestSum of two consecutive numbers in arr | ||
*/ | ||
const solution = arr => { | ||
// Initialize largestSum to the sum of the first two elements in arr | ||
let largestSum = arr[0] + arr[1]; | ||
|
||
// Loop through arr, adding i + (i + 1) and compare to largestSum, updating as necessary | ||
for (let i = 1; i < arr.length; i++) { | ||
// Set the current sum of the iteration to the current element i and the next element, i + 1 | ||
let currentSum = arr[i] + arr[i + 1]; | ||
|
||
// If currentSum is larger than largestSum, update accordingly, else leave as is | ||
largestSum = currentSum > largestSum ? currentSum : largestSum; | ||
} | ||
|
||
return largestSum; | ||
}; | ||
|
||
module.exports = { | ||
solution | ||
}; |
This file contains 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,18 @@ | ||
const expect = require('chai').expect; | ||
let solution = require('../solutions/20').solution; | ||
//solution = require('../yourSolution').solution; | ||
|
||
describe('largest sum in array', () => { | ||
it('should return 11 for [1,2,9,0,10]', () => { | ||
const result = solution([1,2,9,0,10]); | ||
expect(result).to.equal(11); | ||
}); | ||
it('should return 46 for [13,14,32,3,17,6]', () => { | ||
const result = solution([13,14,32,3,17]); | ||
expect(result).to.equal(46); | ||
}); | ||
it('should work with negative numbers [-1,-2,-9,-3,-10]', () => { | ||
const result = solution([-1,-2,-9,-3,-10]); | ||
expect(result).to.equal(-3); | ||
}) | ||
}); |
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.
@zachnagatani code looks great, but please remove the comments inside of the solution such as commenting before a variable declaration or loop, etc.
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.
@msach22 Thanks! They've been removed and pushed