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
class VigenereCipher | |
constructor: (@key, @abc) -> | |
@dict = {} | |
i = 0 | |
for x in @abc | |
shift = @abc[i..] + @abc[0...i] | |
@dict[x] = shift | |
i++; | |
encode: (str) -> |
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
function VigenèreCipher(key, abc) { | |
this.encode = function (str) { | |
var encodeStr = '' | |
for (var i in str) { | |
var c = str[i]; | |
var k = key[i % key.length]; | |
if (abc.indexOf(c) >= 0) { | |
encodeStr += abc[(abc.indexOf(c) + abc.indexOf(k)) % abc.length]; | |
} else { |
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
// http:// stackoverflow.com/questions/23202489/how-does-this-code-find-the-number-of-trailing-zeros-from-any-base-number-factor | |
// https://comeoncodeon.wordpress.com/2010/02/17/number-of-zeores-and-digits-in-n-factorial-in-base-b/ | |
function zeroes (base, number) { | |
var noz = Number.MAX_VALUE; | |
// Now we can break the Base B as a product of primes : | |
// B = a^p1 * b^p2 * c^p3 * … | |
//Then the number of trailing zeroes in N factorial in Base B is given by the formulae | |
// min{1/p1(n/a + n/(a*a) + ….), 1/p2(n/b + n/(b*b) + ..), ….}. | |
var j = base; |
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
var Sudoku = function(data) | |
{ | |
// Private methods | |
// ------------------------- | |
var N = data.length; | |
var rootN = Math.floor(Math.sqrt(N)); | |
var isValidSubSudoku = function(leftX, leftY, size) { | |
var sudokuTracker = {}; | |
for (var j = leftX; j < leftX+size; j++) { |
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
// https://codemyroad.wordpress.com/2014/05/01/solving-sudoku-by-backtracking/ | |
/* | |
On the other hand, the backtracking algorithm fills up a blank cell with a | |
valid number (i.e. no two same numbers in any row, column or big box), | |
moves on to the next cell, and then does the same thing. If all the possible | |
numbers from 1 to 9 are invalid for any cell that the algorithm is currently “at”, | |
the algorithm moves back to the previous cell and changes that cell’s value to another valid number. Afterwards, it moves back to the next cell and the whole process repeats. | |
*/ | |
// https://en.wikipedia.org/wiki/Sudoku_solving_algorithms | |
// use backtracking algorithm |
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
// http://www.geeksforgeeks.org/find-last-digit-of-ab-for-large-numbers/ | |
//http://qa.geeksforgeeks.org/1061/given-big-number-form-string-how-do-take-mod-of-that-big-number | |
var asciiZero = "0".charCodeAt(0); | |
var findModulo = function(base, exponent) { | |
var mod = 0; | |
for (var i in exponent) { | |
//mod = (mod*10 + b[i] - '0')%a; | |
mod = (mod * 10 + exponent.charCodeAt(i) - asciiZero) % base; | |
} |
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
// http://stackoverflow.com/questions/5131497/find-the-index-of-a-given-permutation-in-the-sorted-list-of-the-permutations-of | |
// http://stackoverflow.com/questions/18644470/anagram-index-calculation/18646156#18646156 | |
var f = []; | |
function factorial (n) { | |
if (n == 0 || n == 1) | |
return 1; | |
if (f[n] > 0) | |
return f[n]; | |
return f[n] = factorial(n-1) * n; | |
} |
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
function undoRedo(object) { | |
//var storage = object; | |
var actions = []; // keep track of action performed | |
// action schema -> { name, params, has_undone } | |
// for non-undo acton, example of params { key, value } | |
// for undo action, example of params { action, key, value } | |
var findLastAction = function(status) { | |
var lastAction = null; | |
for (var i = actions.length - 1; i >= 0; i--) { |