-
-
Notifications
You must be signed in to change notification settings - Fork 30.6k
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
Fix knuthMorrisPratt for empty word request #101
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,9 @@ import knuthMorrisPratt from '../knuthMorrisPratt'; | |
|
||
describe('knuthMorrisPratt', () => { | ||
it('should find word position in given text', () => { | ||
expect(knuthMorrisPratt('', '')).toBe(0); | ||
expect(knuthMorrisPratt('a', '')).toBe(0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test is actually not correct. It says that when I search for an empty word '' in a string 'a' I should find one and this empty word will be accessible at zero index. But There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @trekhleb Actually I was understanding it a bit differently. In my understanding I was expecting // definition in terms of code
const startIdx = knuthMorrisPratt(text, word);
text.substr(startIdx, word.length) === word // should be true for startIdx !== -1 Actually both |
||
expect(knuthMorrisPratt('a', 'a')).toBe(0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test is already valid without main function code changes. |
||
expect(knuthMorrisPratt('abcbcglx', 'abca')).toBe(-1); | ||
expect(knuthMorrisPratt('abcbcglx', 'bcgl')).toBe(3); | ||
expect(knuthMorrisPratt('abcxabcdabxabcdabcdabcy', 'abcdabcy')).toBe(15); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,10 @@ function buildPatternTable(word) { | |
* @return {number} | ||
*/ | ||
export default function knuthMorrisPratt(text, word) { | ||
if (word.length === 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please take a look at the comments to the tests above. No need to do this checking. |
||
return 0; | ||
} | ||
|
||
let textIndex = 0; | ||
let wordIndex = 0; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this test is wrong. We should expect
-1
here instead of0
because having zero as a result would mean that I may fetch empty word out ofstring[0]
. But this is not true since thestring
itself is empty.