Skip to content
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

Merged
merged 2 commits into from
Jul 24, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Owner

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 of 0 because having zero as a result would mean that I may fetch empty word out of string[0]. But this is not true since the string itself is empty.

expect(knuthMorrisPratt('a', '')).toBe(0);
Copy link
Owner

Choose a reason for hiding this comment

The 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 string[0] will return us a which expected but is not correct according to this test. So I assume that we should expect -1 here instead of 0.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 knuthMorrisPratt to return the same as String.prototype.indexOf - more a less in a sense.
In other word I considered the output as being: index within text of the first occurrence of word [official reference].

// definition in terms of code
const startIdx = knuthMorrisPratt(text, word);
text.substr(startIdx, word.length) === word // should be true for startIdx !== -1

Actually both "".indexOf("") === 0 and "a".indexOf("") === 0.

expect(knuthMorrisPratt('a', 'a')).toBe(0);
Copy link
Owner

Choose a reason for hiding this comment

The 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);
4 changes: 4 additions & 0 deletions src/algorithms/string/knuth-morris-pratt/knuthMorrisPratt.js
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) {
Copy link
Owner

Choose a reason for hiding this comment

The 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;