Skip to content

Instantly share code, notes, and snippets.

@romanmt
Last active September 2, 2016 22:39
Show Gist options
  • Select an option

  • Save romanmt/a172f0f81202b0d1fd71ccf416a9eea2 to your computer and use it in GitHub Desktop.

Select an option

Save romanmt/a172f0f81202b0d1fd71ccf416a9eea2 to your computer and use it in GitHub Desktop.
Anagram Dictionary
import _, {reject, every, includes} from 'lodash'
const dictionary = [ "a", "sand", "car", "ester", "reset", "steer", "terse", "trees" ]
export function getMatches(characterString) {
let characters = characterString.split("")
return reject(dictionary, (entry) => {
return !every(characters, (character) => {
return includes(entry, character)
});
});
}
export function createEntry(dict = {}, chars, word) {
let firstChar = _(chars).first(),
restChars = _(chars).slice(1).value()
if(restChars.length === 0) {
if(dict[firstChar])
dict[firstChar].values.push(word)
else
dict[firstChar] = {values: [word]}
return dict;
} else {
dict[firstChar] = createEntry(dict[firstChar], restChars, word)
return dict
}
}
export function createDictionary(words) {
return _.transform(words, (memo, word) => {
memo = createEntry(memo, word.split("").sort(), word)
}, {})
}
require('babel-register')
var expect = require('chai').expect
var _ = require('lodash')
var AnagramDetector = require('anagram_detector')
describe("Anagram detector", () => {
describe.only("createDictionary", () => {
it("adds all values to the dictionary", () => {
var words = ["asp", "sap"]
var res = AnagramDetector.createDictionary(words)
expect(res.a.p.s.values).to.contain.all("asp", "sap")
})
})
describe.only("createEntry", () => {
it("adds to values array if node exists", () => {
var word = "asp"
var dict = {a: {p: {s: {values: ["sap"]}}}}
var res = AnagramDetector.createEntry(dict, word.split("").sort(), "asp")
expect(res.a.p.s.values).to.contain.all("asp", "sap")
})
it("add words to existing dictionary", () => {
var word = "cap"
var dict = {a: {c: {r: {values: ["car"]}}}}
var res = AnagramDetector.createEntry(dict, word.split("").sort(), word)
expect(res.a.c.p.values).to.contain("cap")
expect(res.a.c.r.values).to.contain("car")
})
it("adds to existing nodes", () => {
var word = "as"
var dict = { }
var res = AnagramDetector.createEntry(dict, word.split("").sort(), word)
expect(res.a.s.values).to.contain("as")
})
})
describe("getMatches", () => {
it("returns all possible matches for given word", () => {
const res = AnagramDetector.getMatches("steer")
expect(res.length).to.equal(5)
})
it("doesn't return nonmatches", () => {
const res = AnagramDetector.getMatches("steer")
expect(res).to.not.contain("bar")
})
it("returns [] when there are no matches", () => {
const res = AnagramDetector.getMatches("bar")
expect(res).to.eql([])
})
})
})
> usnode@1.0.0 test /Users/matt/Workspace/romanmt/union_station/node
> NODE_PATH=src mocha test/**/*.test.js
Anagram detector
createDictionary
✓ adds all values to the dictionary
createEntry
✓ adds to values array if node exists
✓ add words to existing dictionary
✓ adds to existing nodes
4 passing (40ms)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment