Last active
September 29, 2018 19:43
-
-
Save bchase/253debb829c8df46ad1155e1c546b23d to your computer and use it in GitHub Desktop.
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
#!/usr/local/bin/node | |
////////////////// | |
// Combinations // | |
////////////////// | |
// build all consecutive combination of elements, given an array | |
function getConsecutiveCombinations(orig_arr) { | |
var combinations = [] | |
for (var pointer = 0; pointer <= orig_arr.length; pointer++) { | |
for (var size = pointer; size <= orig_arr.length; size++) { | |
var new_arr = orig_arr.slice(pointer, size) | |
if ( new_arr.length > 0 ) { | |
combinations.push(new_arr) | |
} | |
} | |
} | |
return combinations | |
} | |
// add the above to `Array.prototype`, | |
// so that we can call it as a method, like... | |
// | |
// [1,2].getConsecutiveCombinations() | |
Array.prototype.getConsecutiveCombinations = function() { | |
return getConsecutiveCombinations(this) | |
} | |
///////////// | |
// Helpers // | |
///////////// | |
// sum all numbers in an array (imperative `+=` approach) | |
function sum(arr) { | |
var total = 0 | |
arr.forEach(function(num) { total += num }) | |
return total | |
} | |
// // sum all numbers in an array (functional (`reduce`) approach) | |
// | |
// function sum(arr) { | |
// return arr.reduce(function(total, num) { | |
// return total + num | |
// }) | |
// } | |
// function to sort numbers sanely | |
// (instead of using implicit `toString`) | |
function sortNumbers(x,y) { return x - y } | |
// helper method to get the last element of an array | |
Array.prototype.last = function() { return this[this.length - 1] } | |
////////////// | |
// Run it! // | |
////////////// | |
// calculate and print our max | |
var numbers = [-5, 10, 2, -3, 5, 8, -20] | |
var max = | |
numbers | |
.getConsecutiveCombinations() | |
.map(sum) | |
.sort(sortNumbers) | |
.last() | |
console.log(max) | |
// // `max` above could also be defined as... | |
// var max = numbers.getConsecutiveCombinations().map(toSum).sort(sortNumbers).last() |
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
arrays - Javascript - How Do I Check if 3 Numbers Are Consecutive and Return Starting Points? - Stack Overflow | |
javascript consecutive numbers in array - Google Search | |
Array.prototype.find() - JavaScript | MDN | |
javascript find array - Google Search | |
javascript - How to sort an array of integers correctly - Stack Overflow | |
javascript sort array integers - Google Search | |
javascript reverse array - Google Search | |
array reverse string - Google Search | |
Math.max() - JavaScript | MDN | |
javascript get maximun array - Google Search | |
Array.prototype.concat() - JavaScript | MDN | |
js concat array - Google Search | |
How can I increase the maximum call stack size in Node.js - Stack Overflow | |
node increase call stack size - Google Search | |
javascript - Generating all combinations of an array - Code Review Stack Exchange | |
javascript combinations - Google Search | |
perumutations and - Google Search | |
JavaScript Array reduce() Method | |
javascript sum array - Google Search | |
Array.prototype.map() - JavaScript | MDN | |
js map - Google Search | |
Permutations in JavaScript? - Stack Overflow | |
javascript every permutation of array elements - Google Search |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment