Skip to content

Instantly share code, notes, and snippets.

View YanivHaramati's full-sized avatar

Yaniv Haramati YanivHaramati

View GitHub Profile
@YanivHaramati
YanivHaramati / combos.js
Last active October 18, 2016 05:13
Map a phone number to potential words. This part produces all possible combinations.
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(''));
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;
@YanivHaramati
YanivHaramati / subset_sum.js
Created April 17, 2015 23:03
find a subset that adds up to a number over a verrrrry long list
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];
}
@YanivHaramati
YanivHaramati / deepPick
Last active August 29, 2015 14:18
deepPick whitlisted nodes from an object
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) {
@YanivHaramati
YanivHaramati / getAllKeys
Created April 9, 2015 21:26
get all keys from an object, nested or otherwise.
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];
@YanivHaramati
YanivHaramati / largest_palindrome_product.js
Created March 30, 2015 21:31
Largest palindrome product
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++) {
@YanivHaramati
YanivHaramati / largest_prime_factor.js
Last active August 29, 2015 14:18
Largest prime factor for n
// 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;
@YanivHaramati
YanivHaramati / ArrayAddition
Last active August 29, 2015 14:17
Naive solution to subset sum problem in javascript. Given an array of numbers find any combination that adds up to the max.
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;
@YanivHaramati
YanivHaramati / capitalizeSentence
Created January 12, 2015 06:22
capitalize a sentence assuming one space delimiter
// 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);
};
@YanivHaramati
YanivHaramati / simpleAdding
Created January 12, 2015 06:06
adding numbers as a reduce function
// 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);
}