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
// 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;
}
// 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;
}
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];
}
}
// 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
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++) {
// 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;
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 {
class VigenereCipher
constructor: (@key, @abc) ->
@dict = {}
i = 0
for x in @abc
shift = @abc[i..] + @abc[0...i]
@dict[x] = shift
i++;
encode: (str) ->
decodeMorse = function(morseCode){
var decodeString = '';
var morseCodeWords = morseCode.split(' ');
for (var i in morseCodeWords) {
var morseCodeArray = morseCodeWords[i].split(' ');
for (var j in morseCodeArray) {
if (MORSE_CODE[morseCodeArray[j]] !== undefined) {
decodeString += MORSE_CODE[morseCodeArray[j]];
}
}
function isValidCoordinates(coordinates){
var coordinatesReg = /^(-?\d+(\.\d+)?),\s*(-?\d+(\.\d+)?)$/g
if (!coordinatesReg.test(coordinates)) {
return false;
}
var arrCoordinates = coordinates.split(',');
try {
var strLat = arrCoordinates[0].trim();