Last active
January 1, 2016 10:28
-
-
Save Yonet/8131253 to your computer and use it in GitHub Desktop.
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
var shufflin = function (n, c) { | |
var deck; | |
var count = 1; | |
//array to hold the frequency of the cards appearing in the same index | |
var frequency = new Array(n); | |
//creates a deck with n values | |
var createDeck = function (n) { | |
var arr = []; | |
for (var i = 0; i< n; i++) { | |
arr.push(i); | |
} | |
return arr; | |
}; | |
deck = createDeck(n); | |
var checkFrequency = function (arr) { | |
for (var i = 0; i < arr.length; i++) { | |
frequency[i] = frequency[i] === undefined && arr[i] === i ? count : frequency[i] || undefined; | |
} | |
}; | |
//greatest common divisor | |
var gcd = function (x, y) { | |
while (y !== 0) { | |
var z = x % y; | |
x = y; | |
y = z; | |
} | |
return x; | |
}; | |
//least common multiple | |
var lcm = function (x, y){ | |
return (x * y / gcd(x, y)); | |
}; | |
//Recursively iterate through pairs of arguments in the frequency array | |
var lcmR = _.memoize(function (args){ | |
if(args.length == 2){ | |
return lcm(args[0], args[1]); | |
} else { | |
var arg0 = args[0]; | |
args.shift(); | |
return lcm(arg0, lcmR(args)); | |
} | |
}); | |
var shuffle = function (deck, c) { | |
var s1 = deck.slice(n-c);//upper slice | |
var s2 = deck.slice(0, n-c);//lower slice | |
var len = c < n-c ? c : n-c; | |
var nDeck = []; | |
//perfect shuffle | |
for (var i = 0; i < len*2; i+=2) { | |
nDeck[i] = s1.shift(); | |
nDeck[i+1] = s2.shift(); | |
}; | |
var rest = s2.length ? s2 : s1; | |
//adds the rest of the cards on top | |
nDeck = nDeck.concat(rest); | |
checkFrequency(nDeck); | |
if (_.contains(frequency, undefined)) { | |
count++; | |
shuffle(nDeck, c); | |
}else{ | |
var nums = _.uniq(frequency); | |
count = lcmR(nums); | |
console.log(count); | |
return count; | |
} | |
}; | |
shuffle(deck,c); | |
}; |
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
//compares the arrays | |
var arraysEqual = function (arr1, arr2) { | |
if(arr1.length !== arr2.length) | |
return false; | |
for(var i = arr1.length; i--;) { | |
if(arr1[i] !== arr2[i]) | |
return false; | |
} | |
return true; | |
}; | |
var deck = function (n) { | |
var arr = []; | |
for (var i = 0; i< n; i++) { | |
arr.push(i); | |
} | |
return arr; | |
} | |
var count = 1; | |
var shufflin = function (deck, c) { | |
var n = deck.length; | |
var s1 = deck.slice(n-c); | |
var s2 = deck.slice(0, n-c); | |
var len = c < n-c ? c : n-c; | |
var nDeck = []; | |
for (var i = 0; i < len*2; i+=2) { | |
nDeck[i] = s1.shift(); | |
nDeck[i+1] = s2.shift(); | |
} | |
var rest = s2.length ? s2 : s1; | |
nDeck = nDeck.concat(rest); | |
if(arraysEqual(nDeck, nDeck.slice().sort(function(a,b) {return a-b}))){ | |
console.log(nDeck); | |
console.log(count); | |
return; | |
} else { | |
count++; | |
console.log(nDeck); | |
shufflin(nDeck, c); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment