Skip to content

Instantly share code, notes, and snippets.

View YanivHaramati's full-sized avatar

Yaniv Haramati YanivHaramati

View GitHub Profile
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
@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);
}
@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 / 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;