Skip to content

Instantly share code, notes, and snippets.

@veltman
Created September 2, 2015 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save veltman/c7c7fc15887d8a03f9a4 to your computer and use it in GitHub Desktop.
Save veltman/c7c7fc15887d8a03f9a4 to your computer and use it in GitHub Desktop.
function headTails(arr){
var breaks = [],
avg = mean(arr),
head = arr.filter(function(d){
return d > avg;
});
while (head.length && head.length < Math.floor(arr.length/2)) {
breaks.push(avg);
arr = head;
avg = mean(arr);
head = arr.filter(function(d){
return d > avg;
});
}
return breaks;
}
function mean(a){
var total = a.reduce(function(p, v){ return p + v}, 0);
return total/a.length;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment