From 65e2d5d784c99957f8579f6b513b573c4e91da59 Mon Sep 17 00:00:00 2001 From: Shereef Marzouk Date: Wed, 31 Mar 2021 21:10:36 -0300 Subject: [PATCH 1/8] Added lodash --- package-lock.json | 3 +-- package.json | 3 ++- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 858cfa3..a2e17a0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3724,8 +3724,7 @@ "lodash": { "version": "4.17.21", "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", - "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==", - "dev": true + "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==" }, "lru-cache": { "version": "6.0.0", diff --git a/package.json b/package.json index b0a344f..233a1b7 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,8 @@ }, "homepage": "https://github.com/MoveInc/coding_exercise#readme", "dependencies": { - "express": "^4.17.1" + "express": "^4.17.1", + "lodash": "^4.17.21" }, "devDependencies": { "eslint": "^7.22.0", From 3e2a3edb58cb2558afa4eb97a168df9738c09d51 Mon Sep 17 00:00:00 2001 From: Shereef Marzouk Date: Wed, 31 Mar 2021 21:13:53 -0300 Subject: [PATCH 2/8] prettier formatting the document --- src/anagram-service.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/anagram-service.js b/src/anagram-service.js index 962a3a4..c579b8a 100644 --- a/src/anagram-service.js +++ b/src/anagram-service.js @@ -1,11 +1,9 @@ -const fs = require('fs'); - +const fs = require("fs"); /** - * Looks up anagrams of a given word based on the + * Looks up anagrams of a given word based on the * word dictionary provided in the constructor. */ class AnagramService { - /** * Creates an AnagramService instance * @param {string} dictionaryFilePath Path to the dictionary file @@ -26,7 +24,7 @@ class AnagramService { return reject(err); } - const lines = data.toString().split('\n'); + const lines = data.toString().split("\n"); lines.forEach((line) => { this.wordsMap.set(line.toLowerCase(), [line]); @@ -38,12 +36,12 @@ class AnagramService { /** * Returns all anagrams for the given term - * @param {string} term The term to find anagrams for + * @param {string} term The term to find anagrams for * @returns A string[] of anagram matches */ async getAnagrams(term) { if (!this.wordsMap || this.wordsMap.size === 0) { - throw Error('Error: Dictionary not initialized'); + throw Error("Error: Dictionary not initialized"); } // TODO: The anagram lookup 🤦‍♂️ From e1d6cc4fe6467a471f1741458f67c4d18e4d7e68 Mon Sep 17 00:00:00 2001 From: Shereef Marzouk Date: Wed, 31 Mar 2021 21:16:49 -0300 Subject: [PATCH 3/8] Changed wordmap into an array or lower case --- src/anagram-service.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/anagram-service.js b/src/anagram-service.js index c579b8a..f3623aa 100644 --- a/src/anagram-service.js +++ b/src/anagram-service.js @@ -10,7 +10,7 @@ class AnagramService { */ constructor(dictionaryFilePath) { this.dictionaryFilePath = dictionaryFilePath; - this.wordsMap = new Map(); + this.wordsMap = []; } /** @@ -27,7 +27,7 @@ class AnagramService { const lines = data.toString().split("\n"); lines.forEach((line) => { - this.wordsMap.set(line.toLowerCase(), [line]); + this.wordsMap.push(line.toLowerCase().trim()); }); return resolve(this); }); @@ -40,12 +40,12 @@ class AnagramService { * @returns A string[] of anagram matches */ async getAnagrams(term) { - if (!this.wordsMap || this.wordsMap.size === 0) { + if (!this.wordsMap || this.wordsMap.length === 0) { throw Error("Error: Dictionary not initialized"); } // TODO: The anagram lookup 🤦‍♂️ - return this.wordsMap.get(term); + return this.wordsMap; } } From 6a92807cc222a5f35b200fae65e2af40e8fbcc86 Mon Sep 17 00:00:00 2001 From: Shereef Marzouk Date: Wed, 31 Mar 2021 21:17:40 -0300 Subject: [PATCH 4/8] Added the code to find the anagram --- src/anagram-service.js | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/anagram-service.js b/src/anagram-service.js index f3623aa..14f5359 100644 --- a/src/anagram-service.js +++ b/src/anagram-service.js @@ -1,4 +1,5 @@ const fs = require("fs"); +const lodash = require("lodash"); /** * Looks up anagrams of a given word based on the * word dictionary provided in the constructor. @@ -43,9 +44,31 @@ class AnagramService { if (!this.wordsMap || this.wordsMap.length === 0) { throw Error("Error: Dictionary not initialized"); } + const termCharacterMap = {}; + for (const index in Array.from(term).sort()) { + const character = term[index]; + if (termCharacterMap[character]) { + termCharacterMap[character]++; + } else { + termCharacterMap[character] = 1; + } + } + const filteredWordsMapKeys = this.wordsMap.filter((word) => { + const hasSameLength = word.length === term.length; + const wordCharacterMap = {}; + for (const index in Array.from(word).sort()) { + const character = word[index]; + if (wordCharacterMap[character]) { + wordCharacterMap[character]++; + } else { + wordCharacterMap[character] = 1; + } + } + const KeysAreEqual = lodash.isEqual(wordCharacterMap, termCharacterMap); - // TODO: The anagram lookup 🤦‍♂️ - return this.wordsMap; + return hasSameLength && KeysAreEqual; + }); + return filteredWordsMapKeys; } } From 6b05e8b23f2c484fddf7f24d2d2d58df1e35dc23 Mon Sep 17 00:00:00 2001 From: Shereef Marzouk Date: Wed, 31 Mar 2021 21:18:08 -0300 Subject: [PATCH 5/8] Renamed wordsMap to wordsArray because it doesn't make sense anymore --- src/anagram-service.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/anagram-service.js b/src/anagram-service.js index 14f5359..718bab3 100644 --- a/src/anagram-service.js +++ b/src/anagram-service.js @@ -11,7 +11,7 @@ class AnagramService { */ constructor(dictionaryFilePath) { this.dictionaryFilePath = dictionaryFilePath; - this.wordsMap = []; + this.wordsArray = []; } /** @@ -28,7 +28,7 @@ class AnagramService { const lines = data.toString().split("\n"); lines.forEach((line) => { - this.wordsMap.push(line.toLowerCase().trim()); + this.wordsArray.push(line.toLowerCase().trim()); }); return resolve(this); }); @@ -41,7 +41,7 @@ class AnagramService { * @returns A string[] of anagram matches */ async getAnagrams(term) { - if (!this.wordsMap || this.wordsMap.length === 0) { + if (!this.wordsArray || this.wordsArray.length === 0) { throw Error("Error: Dictionary not initialized"); } const termCharacterMap = {}; @@ -53,7 +53,7 @@ class AnagramService { termCharacterMap[character] = 1; } } - const filteredWordsMapKeys = this.wordsMap.filter((word) => { + const filteredWordsMapKeys = this.wordsArray.filter((word) => { const hasSameLength = word.length === term.length; const wordCharacterMap = {}; for (const index in Array.from(word).sort()) { From acc3adc229f7c5d21486af444be74e5ed7bfe04b Mon Sep 17 00:00:00 2001 From: Shereef Marzouk Date: Wed, 31 Mar 2021 21:21:07 -0300 Subject: [PATCH 6/8] created a function for the duplicate code --- src/anagram-service.js | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/src/anagram-service.js b/src/anagram-service.js index 718bab3..cc203b4 100644 --- a/src/anagram-service.js +++ b/src/anagram-service.js @@ -44,32 +44,31 @@ class AnagramService { if (!this.wordsArray || this.wordsArray.length === 0) { throw Error("Error: Dictionary not initialized"); } - const termCharacterMap = {}; - for (const index in Array.from(term).sort()) { - const character = term[index]; - if (termCharacterMap[character]) { - termCharacterMap[character]++; - } else { - termCharacterMap[character] = 1; - } - } + const termCharacterMap = this.getCharacterMap(term); + const filteredWordsMapKeys = this.wordsArray.filter((word) => { const hasSameLength = word.length === term.length; - const wordCharacterMap = {}; - for (const index in Array.from(word).sort()) { - const character = word[index]; - if (wordCharacterMap[character]) { - wordCharacterMap[character]++; - } else { - wordCharacterMap[character] = 1; - } - } - const KeysAreEqual = lodash.isEqual(wordCharacterMap, termCharacterMap); + + const wordCharacterMap = this.getCharacterMap(word); + const MapsAreEqual = lodash.isEqual(wordCharacterMap, termCharacterMap); - return hasSameLength && KeysAreEqual; + return hasSameLength && MapsAreEqual; }); return filteredWordsMapKeys; } + + getCharacterMap(word) { + const map = {}; + for (const index in Array.from(word).sort()) { + const character = word[index]; + if (map[character]) { + map[character]++; + } else { + map[character] = 1; + } + } + return map; + } } module.exports = AnagramService; From 4372660a466cf19c2ce1d4aa96379f288dc0430a Mon Sep 17 00:00:00 2001 From: Shereef Marzouk Date: Wed, 31 Mar 2021 21:22:09 -0300 Subject: [PATCH 7/8] if it doesn't have the same length cut it short --- src/anagram-service.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/anagram-service.js b/src/anagram-service.js index cc203b4..3032865 100644 --- a/src/anagram-service.js +++ b/src/anagram-service.js @@ -48,7 +48,7 @@ class AnagramService { const filteredWordsMapKeys = this.wordsArray.filter((word) => { const hasSameLength = word.length === term.length; - + if (!hasSameLength) return false; const wordCharacterMap = this.getCharacterMap(word); const MapsAreEqual = lodash.isEqual(wordCharacterMap, termCharacterMap); From f9cd2fb65c711e10eadc74608fd299c6098301a5 Mon Sep 17 00:00:00 2001 From: Shereef Marzouk Date: Wed, 31 Mar 2021 21:23:42 -0300 Subject: [PATCH 8/8] I just needed a line before the return to make it look nice --- src/anagram-service.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/anagram-service.js b/src/anagram-service.js index 3032865..4d231e1 100644 --- a/src/anagram-service.js +++ b/src/anagram-service.js @@ -54,6 +54,7 @@ class AnagramService { return hasSameLength && MapsAreEqual; }); + return filteredWordsMapKeys; }