Skip to content

Instantly share code, notes, and snippets.

@matthandlersux
Last active October 5, 2019 15:37
Show Gist options
  • Save matthandlersux/d5d5933ece968a929ec2c6c2ebdc7e59 to your computer and use it in GitHub Desktop.
Save matthandlersux/d5d5933ece968a929ec2c6c2ebdc7e59 to your computer and use it in GitHub Desktop.
const { uniq, map, range, zip, mapValues, groupBy, flatMap } = require('lodash');
const expandWordToDeletions = (word) => {
const wordArray = word.split("");
const deletionList = map(
range(word.length),
i => {
const copied = [...wordArray];
copied.splice(i, 1);
return copied.join("");
}
)
return [word, ...deletionList];
};
const createMap = (words) => (
mapValues(
groupBy(
flatMap(words, word =>
zip(expandWordToDeletions(word), (new Array(word.length + 1)).fill(word))
),
([key, value]) => key
),
listOfKeyValues => map(listOfKeyValues, ([key, value]) => value)
)
);
const search = (word, dictionary) => {
if (word in dictionary) return dictionary[word];
else {
return uniq(
flatMap(
expandWordToDeletions(word),
deleteWord => dictionary[deleteWord],
).filter(Boolean)
)
}
};
const dictionary = createMap(["matt handler", "mutt handler"]);
console.log(search("patt handler", dictionary));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment