Skip to content

Instantly share code, notes, and snippets.

@jasonwaters
jasonwaters / queue.js
Created January 31, 2017 20:54
queue
// solution for this hackerrank challenge
// https://www.hackerrank.com/challenges/ctci-queue-using-two-stacks
function Queue() {
this.data = [];
this.operations = {
1: args => this.enqueue(...args),
2: args => this.dequeue(...args),
3: args => this.print(...args)
@jasonwaters
jasonwaters / memoize.js
Created January 27, 2017 18:06
memoize function
function memoized(fn) {
let cache = {};
return function(key) {
if(typeof cache[key] !== 'undefined') {
return cache[key];
}else {
let result = fn.apply(null, arguments);
cache[key] = result;
return result;
@jasonwaters
jasonwaters / debounce-throttle.js
Created January 27, 2017 17:13
debounce and throtle
function throttle(fn, delay) {
let lastTime;
return function() {
let now = new Date().getTime();
if(!lastTime || now - lastTime > delay) {
fn.apply(null, arguments);
lastTime = now;
}
@jasonwaters
jasonwaters / magazine-ransom.js
Created January 26, 2017 15:50
magazine ransom
/*
Given the words in the magazine and the words in the ransom note, print Yes if he can replicate his ransom note exactly using whole words from the magazine; otherwise, print No.
https://www.hackerrank.com/challenges/ctci-ransom-note?h_r=internal-search
*/
function magazineRansom(magazine, note) {
magazine = magazine
.split(' ')
.reduce((map, word) => {
if (!map[word]) {
@jasonwaters
jasonwaters / anagram-removals.js
Created January 25, 2017 23:43
anagram removals
/*
Given two strings, and , that may or may not be of the same length, determine the minimum number of character deletions required to make and anagrams. Any characters can be deleted from either of the strings.
https://www.hackerrank.com/challenges/ctci-making-anagrams
*/
function indexChars(map, char) {
if(!map.hasOwnProperty(char)) {
map[char] = 0;
}
map[char]++;
@jasonwaters
jasonwaters / lakes.js
Last active January 31, 2017 22:44
bodies of water
/*
A script that processes a matrix that represents bodies of water. Any values that equal 0 represent water.
The script outputs a list of bodies of water found, with their respective sizes. For Example:
let water = [
[0, 2, 1, 0],
[0, 1, 0, 1],
[1, 1, 0, 1],
[0, 1, 0, 1]
];
@jasonwaters
jasonwaters / reversepolish.js
Created January 23, 2017 20:13
reverse polish notation
// a script that processes arithmetic in reverse polish notation
//https://en.wikipedia.org/wiki/Reverse_Polish_notation
const operate = {
'+': (a,b) => a+b,
'-': (a,b) => a-b,
'*': (a,b) => a*b,
'/': (a,b) => a/b,
};
function flattenObj(obj, keyChain=[], flattened={}) {
for(let key in obj) {
let value = obj[key];
let keys = keyChain.concat(key);
if(value instanceof Object) {
flattenObj(value, keys, flattened);
}else {
flattened[keys.join('.')] = value;
}
Array.prototype.swap = function(idx1, idx2) {
if(idx1 >= this.length || idx2 >= this.length) {
throw new Error("index out of bounds");
}
let first = Math.max(idx1, idx2);
let last = Math.min(idx1, idx2);
let value = this.splice(first, 1, this[last])[0];
//Given an array of positive integers and a target total of X, find if there exists a contiguous subarray with sum = X
var list=[1,3,5,18];
function isValid(arr, target) {
var left=0;
var right=0;
while(true) {
var sum = 0;