Skip to content

Instantly share code, notes, and snippets.

View railsstudent's full-sized avatar
🏠
Researching third-party library

Connie Leung railsstudent

🏠
Researching third-party library
View GitHub Profile
class VigenereCipher
constructor: (@key, @abc) ->
@dict = {}
i = 0
for x in @abc
shift = @abc[i..] + @abc[0...i]
@dict[x] = shift
i++;
encode: (str) ->
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 {
// 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;
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++) {
// 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
function handleAcromyn(accumulator, path, isLast) {
var excluded = ["the","of","in","from","by","with","and", "or", "for", "to", "at", "a"];
var acromynList = path.split('-');
var shortener = '';
for (var k in acromynList) {
var term = acromynList[k];
if (excluded.indexOf(term) < 0) {
shortener += term[0];
}
}
// 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;
}
// 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;
}
// complete the function so that it returns the fastest route
function navigate(numberOfIntersections, roads, start, finish) {
var arrDrivingTimes = [];
var arrRoutes = []
// initialize driving time and route in matrixes
for (var i = 0; i < numberOfIntersections; i++) {
arrDrivingTimes.push([]);
arrRoutes.push([]);
for (var j = 0; j < numberOfIntersections; j++) {
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--) {