Skip to content

Instantly share code, notes, and snippets.

/*
Solution to:
https://blog.svpino.com/2015/05/17/programming-challenge-merging-overlapping-intervals
*/
function combineOverlappingIntervals(intervals){
var overlapping = [], newOnes = [];
// Compare each interval with every other interval
/*
Solution to
https://blog.svpino.com/2015/05/24/programming-challenge-the-position-of-the-element
*/
function posOfEl(list,el){
// if the element exist return the index
var index = list.indexOf(el);
if(index>=0){return index;}
// otherwise insert it in a copy
@ironboy
ironboy / towers.js
Created August 30, 2015 22:42
Towers of Hanoi - non-recursive
/*
Towers of Hanoi
(non-recursive solution)
https://blog.svpino.com/2015/06/07/programming-challenge-towers-of-hanoi
*/
function solveHanoi(discs){
var towers = {a:[],b:[],c:[]};
@ironboy
ironboy / Array shuffle.js
Created September 4, 2015 20:37
Shuffle an array - return a shuffled copy (keep original intact)
Array.prototype.shuffle = function(){
var x = this.slice(), y = [];
while(x.length){y.push(x.splice(Math.floor(Math.random()*x.length),1)[0]);}
return y;
}
@ironboy
ironboy / bit.js
Created September 4, 2015 22:50
Bit test in js
function bit_test(num,bit){
return ((num>>bit) % 2 != 0)
}
function bit_set(num,bit){
return num | 1<<bit;
}
function bit_clear(num,bit){
return num & ~(1<<bit);
@ironboy
ironboy / wordlist.txt
Created September 5, 2015 21:40
English wordlist
aardvark
aardwolf
aaron
aback
abacus
abaft
abalone
abandon
abandoned
abandonment
@ironboy
ironboy / date format.js
Last active July 27, 2016 12:41
date format
// Example usage niceFormat
// new Date().niceFormat('yyyy-mm-dd hh:ii:ss')
// Example usage countdown
// new Date().countdown(dateInFuture,'ii:ss')
// try with new Date().countdown(new Date(new Date().getTime()+3600000),"ii:ss")
(function(){
var a = {
// options object example
/*
{
decimals:2,
decimalSign:",",
thousandSep: " ",
currency:"SEK"
}
*/
$scope.changeWatch = function(){
// An alternative to Angular $scope.$watch
// that does not trigger on initial load
var
init = true,
args = [].slice.call(arguments),
listener = args[1];
Number.prototype.slice = function(x,y){
return (this+'').slice(x,y)/1;
}