Skip to content

Instantly share code, notes, and snippets.

View YanivHaramati's full-sized avatar

Yaniv Haramati YanivHaramati

View GitHub Profile
@YanivHaramati
YanivHaramati / gist:7392469
Created November 10, 2013 01:34
A simple trie in java
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) {
var Promise = require('bluebird'),
request = require('request'),
url = require('url'),
tests = require('./tests').tests;
function promiseableRequest(args) {
return new Promise(function(resolve, reject) {
var opts = {
uri: url.parse(args.url),
followRedirect: !!args.redirect
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;
@YanivHaramati
YanivHaramati / min_candy.js
Last active August 29, 2015 14:06
Candy Problem from leetcode
There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy. Children with a higher rating get more candies than their neighbors. What is the minimum candies you must give?
var box = new CandyBox([2,4,7,8,3,4,6,2]);
var count = box.candyCount(); // => [1,2,3,4,1,2,3,1] => 17
var Promise = require('bluebird'),
request = require('request'),
url = require('url'),
tests = require('./tests').tests;
var environments = { "prod": "",
"qa": "qam.",
"dev": "localci."
};
@YanivHaramati
YanivHaramati / factorial
Created January 11, 2015 07:14
dp factorial as map reduce in javascript.
function factorial(num) {
if (num <= 2) return num;
return Array.apply(null, Array(num))
.map(function (_, i) { return i + 1; })
.reduce(function(p, c, i, a) {
a[i] = (i < 2) ? i+1 : (i + 1) * a[i-1];
return a[i];
});
}
@YanivHaramati
YanivHaramati / longest word
Created January 12, 2015 04:11
Longest word in a sentence in terms of reduce
// longest word in sentence, and ignore punctuation.
function longestWord(sen) {
// would be easier if we had support for unicode character classes, at which point can use \p{P} for punctuation.
var punctPattern = /[\!\@\#\$\%\^\&\*\(\)\'\|\"/\\\-\+~`?\<\>\}\{]/g;
return sen.replace(punctPattern, '')
.split(' ')
.reduce(function(p, c, i, a) {
var maxLength = p.max;
var curLength = c.length;
if (curLength >= maxLength) {
@YanivHaramati
YanivHaramati / Letter Changes
Created January 12, 2015 05:40
Letter Changes
// take the str parameter being passed and modify it using the
// following algorithm.
// Replace every letter in the string with the letter following it in the alphabet
// (ie.c becomes d, z becomes a).Then capitalize every vowel in this new string (a, e, i, o, u)
// and finally return this modified string.
function letterChanges(str) {
var lowerStart = "a".charCodeAt(0);
var upperStart = "A".charCodeAt(0);
var letterCount = ("z".charCodeAt(0) - lowerStart) + 1; // 26;
@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);
}