Skip to content

Instantly share code, notes, and snippets.

@jtribble
jtribble / string-distance.js
Last active January 27, 2016 20:40
Implementation of Levenshtein distance algorithm to compute string similarity. See: https://en.wikipedia.org/wiki/Levenshtein_distance
//
// Compute string similarity using Levenshtein distance algorithm
//
// See: https://en.wikipedia.org/wiki/Levenshtein_distance
//
/**
* @param {string} a - The first string
* @param {string} b - The second string
* @return {number} - An integer representation of the difference
//
// Build a Xbonacci function that takes a signature of X elements - and
// remember each next element is the sum of the last X elements - and
// returns the first n elements of the so seeded sequence.
//
// xbonacci([1,1,1,1],10) -> [1,1,1,1,4,7,13,25,49,94]
// xbonacci([0,0,0,0,1],10) -> [0,0,0,0,1,1,2,4,8,16]
// xbonacci([1,0,0,0,0,0,1],10) -> [1,0,0,0,0,0,1,1,2,4]
//
//
// Example filters
// - will populate a dropdown <select>
// - has an optional useKeywordFilter flag
//
const losFilters = {
key: 'lineOfService',
label: 'Line of Service',
//
// See: http://careercup.com/question?id=16759664
//
// You have k lists of sorted integers. Find the smallest range
// that includes at least one number from each of the k lists.
//
// Input: [[4, 10, 15, 24, 26], [0, 9, 12, 20], [5, 18, 22, 30]]
// Output: [20, 24]
//
//
// See: http://careercup.com/question?id=11070934
//
// Given an array which might contain duplicates, find
// largest subset of it which forms subsequence
//
// Input: [1, 6, 10, 4, 7, 9, 5]
// Output: [4, 5, 6, 7]
//
//
// See: http://careercup.com/question?id=5201559730257920
//
// Given an array which has positive and negative ints, sort
// the list so that all positives come after negatives, and the
// relative order is preserved.
//
// Input: [-1, 2, 2, -2, -3, 1, 10, 9, -9]
// Output: [-1, -2, -3, -9, 2, 2, 1, 10, 9]
//
// Given an array where all numbers except one are repeated,
// find the number that only occurs once.
const uniqNums = function(list) {
let hash = {};
let uniq = {};
// O(n) - iterate through list
for (let i = 0; i < list.length; i++) {
if (hash.hasOwnProperty(list[i])) {
delete uniq[list[i]];
//
// See: http://careercup.com/question?id=5755014750404608
//
// Input: [[1,3], [5,7], [2,4], [6,8]]
// Output: [[1,4], [5,8]] (no specific order)
//
var mergeRanges = (function () {
/**
//
// See: http://careercup.com/question?id=4816567298686976
//
'use strict';
// var set = [2, 3], n = 8;
// expect: results.exp = [1, 2, 3, 4, 6, 8, 9, 12]
// expect: results.non = [5, 7, 10, 11]
//
// See: http://www.glassdoor.com/Interview/You-are-given-a-collection-of-M-arrays-with-N-integers-Every-array-is-sorted-Develop-an-algorithm-to-combine-each-array-i-QTN_1195702.htm
//
// You are given a collection of M arrays with N integers. Every array is sorted.
// Develop an algorithm to combine each array into one sorted array.
//
var mergeSortedLists = (function () {
/**