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 digits = {2: ['a', 'b', 'c'], 3: ['d', 'e', 'f'], 4: ['g','h','i'], 5: ['j','k','l'], 6: ['m','n','o'], 7:['p', 'q', 'r', 's'], 8: ['t','u','v'], 9: ['x','y','z']}; | |
function crossProduct(sets) { | |
var r = sets.reduce((p,c) => { | |
return p.map(x => { | |
return c.map(y => { | |
return x.concat(y); | |
}) | |
}).reduce((prev,cur) => { return prev.concat(cur) },[]) | |
}, [[]]).map(s => s.join('')); |
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 EnglishDictionary = function (words) { | |
var self = {}; | |
var letterHash = initLeterHash(); | |
// this is efficient enough to get 1mil primes fast. | |
function getPrimes(num) { | |
var primes = [2, 3]; | |
for (var n = 5; primes.length < num; n += 2) { | |
var sqrt = Math.sqrt(n); | |
var isPrime = true; |
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
import java.util.HashMap; | |
import java.util.Map.Entry; | |
public class Trie { | |
private HashMap<Character, TNode> nodes = new HashMap<Character, TNode>(); | |
public Trie() {} | |
public void insert(String word) { |
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
package main.java; | |
import java.time.LocalTime; | |
import java.time.temporal.ChronoUnit; | |
import java.util.HashMap; | |
import java.util.UUID; | |
public class LruCache<T extends Object> { | |
private HashMap<UUID, LinkedNode<T>> hashMap; | |
private LinkedPointerList q; |
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
function ArrayAddition(arr, expectedSum) { | |
// get all subsets, and filter out subsets whose sum doesn't add up to expectedSum. | |
var sizeRange = range(arr.length); | |
for (var i = 0; i < sizeRange.length; i++){ | |
var subs = subsets(arr, sizeRange[i]).filter(function(s){ return s.length > 0;}); | |
for(var j = 0; j < subs.length; j++){ | |
if (sum(subs[j]) === expectedSum) { | |
return subs[j]; | |
} |
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
function getElement(obj, element) { | |
var res = {}; | |
var nodes = element.split('.'); | |
var currentSelf = res; | |
var currentObj = obj; | |
if (nodes.length === 1) { | |
res[element] = obj[element]; | |
return res; | |
} else { | |
nodes.slice(0, nodes.length -1).forEach(function(key) { |
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
Array.prototype.flatten = function () { | |
return this.reduce(function (all, current) { | |
if (current.constructor === Array) return all.concat(current.flatten()); | |
else return all.concat(current); | |
},[]); | |
} | |
function getAllKeys(obj) { | |
return Object.keys(obj).reduce(function (allKeys, key) { | |
var val = obj[key]; |
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
function getPalindromes(from, to) { | |
var palindromes = []; | |
for (var n = from; n <= to; n++) { | |
if (isPalindrome(n)) palindromes.push(n); | |
} | |
return palindromes; | |
} | |
function isPalindrome(n) { | |
var nCArray = n.toString().split(''); | |
for (var i = 0; i < nCArray.length; i++) { |
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
// find largest prime factor of n | |
function primeFactorization(n) { | |
// begin by fetching the first 100 primes. if this proves insufficient we'll get more by multiplying the count by 10 each time. | |
var primeCount = 100; | |
var primes = getPrimes(primeCount); | |
var factors = []; | |
var temp = n; | |
for (var i = 0; primes.indexOf(temp) < 0; i++) { | |
if (i > primes.length - 1) { | |
primeCount *= 10; |
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
function ArrayAddition(arr) { | |
// first get the max and remove it from the array | |
arr.sort(function(a,b) {return b-a;}); | |
var max = arr[0]; | |
var arr = arr.slice(1); | |
// get all subsets, and filter out subsets whose sum doesn't add up to max. | |
var res = subsets(arr, 2).filter(function(sub) { | |
return sum(sub) == max; |
NewerOlder