-
Notifications
You must be signed in to change notification settings - Fork 934
chore(lab): add new exercises and fix comment #65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+103
−7
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
lab/exercises/10-mixed/trie-wildcard-search.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* Your WordDictionary object will be instantiated and called as such: | ||
* var obj = new WordDictionary() | ||
* obj.addWord(word) | ||
* var param_2 = obj.search(word) | ||
*/ | ||
class WordDictionary { | ||
children = {}; | ||
isWord = false; | ||
/** | ||
* Initialize your data structure here. | ||
*/ | ||
constructor() { | ||
} | ||
|
||
/** | ||
* Adds a word into the data structure. | ||
* @param {string} word | ||
* @return {void} | ||
*/ | ||
addWord (word) { | ||
let curr = this; | ||
|
||
for (let char of word) { | ||
if (!curr.children[char]) curr.children[char] = new WordDictionary(); | ||
curr = curr.children[char]; | ||
} | ||
|
||
curr.isWord = true; | ||
} | ||
|
||
/** | ||
* Returns if the word is in the data structure. | ||
* A word could contain the dot character '.' to represent any one letter. | ||
* @param {string} word | ||
* @return {boolean} | ||
*/ | ||
search (word, curr = this, index = 0) { | ||
if (index > word.length) return true; // e.g. final '.' | ||
for (let [i, char] of [...word.slice(index)].entries()) { | ||
if (char === '.') { | ||
for (let child of Object.keys(curr.children)) { | ||
if (this.search(word, curr.children[child], i + 1)) return true; | ||
} | ||
} | ||
else if (!curr || !curr.children[char]) return false; | ||
curr = curr.children[char]; | ||
} | ||
|
||
return curr.isWord; | ||
} | ||
} | ||
|
||
module.exports = WordDictionary; |
47 changes: 47 additions & 0 deletions
lab/exercises/10-mixed/trie-wildcard-search.spec.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
const WordDictionary = require('./trie-wildcard-search'); | ||
|
||
describe('WordDictionary', () => { | ||
let wd; | ||
|
||
beforeEach(() => { | ||
wd = new WordDictionary(); | ||
}); | ||
|
||
describe('should find exact matches', () => { | ||
beforeEach(() => { | ||
wd.addWord('bad'); | ||
wd.addWord('mad'); | ||
}); | ||
|
||
it('should find match', () => { | ||
expect(wd.search('bad')).toEqual(true); | ||
expect(wd.search('mad')).toEqual(true); | ||
}); | ||
|
||
it('should be false for partial match', () => { | ||
expect(wd.search('ba')).toEqual(false); | ||
}); | ||
|
||
it('should be false for NO match', () => { | ||
expect(wd.search('dad')).toEqual(false); | ||
}); | ||
}); | ||
|
||
describe('should find wildcard matches', () => { | ||
beforeEach(() => { | ||
wd.addWord('bad'); | ||
}); | ||
|
||
it('should work with 1 wildcard', () => { | ||
expect(wd.search('.ad')).toEqual(true); | ||
}); | ||
|
||
it('should work with 1 wildcard not match', () => { | ||
expect(wd.search('.ax')).toEqual(false); | ||
}); | ||
|
||
it('should work with 2 wildcard', () => { | ||
expect(wd.search('..d')).toEqual(true); | ||
}); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.