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 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 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
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 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 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 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 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 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
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 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 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 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
// 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 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 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; |
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
// take the str parameter being passed and capitalize the first letter of each word. | |
// Words will be separated by only one space. | |
function capitalize(str) { | |
var upperLowerDiff = "a".charCodeAt(0) - "A".charCodeAt(0); | |
var toUpper = function(c) { | |
return String.fromCharCode(c.charCodeAt(0) - upperLowerDiff); | |
}; | |
var isLower = function(c) { | |
return /[a-z]/g.test(c); | |
}; |
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
// add all numbers from 1 to n | |
function simpleAdding(n) { | |
return Array.apply(null, Array(n)).map(function(_, i) { | |
return i + 1; | |
}) | |
.reduce(function(p, c) { | |
return p + c; | |
}, 0); | |
} |
NewerOlder