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

Conversation

dubzzz
Copy link
Contributor

@dubzzz dubzzz commented Jul 20, 2018

While running property based tests - based on fast-check - on the implementation of knuthMorrisPratt I discovered what seems to be an inconsistency:

knuthMorrisPratt("", "") === -1
knuthMorrisPratt("a", "a") === 0

The tests that discovered the issue are available in the commit: dubzzz@87329dd
If you are interested in this test method, I can issue another pull request for those property based tests.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
@codecov-io
Copy link

codecov-io commented Jul 20, 2018

Codecov Report

Merging #101 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master   #101   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files         115    115           
  Lines        2255   2254    -1     
  Branches      393    391    -2     
=====================================
- Hits         2255   2254    -1
Impacted Files Coverage Δ
...thms/string/knuth-morris-pratt/knuthMorrisPratt.js 100% <100%> (ø) ⬆️
...c/algorithms/sorting/counting-sort/CountingSort.js 100% <0%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 76461f2...618d096. Read the comment docs.

@@ -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);
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.

@@ -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);
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.

@@ -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.

@@ -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.

@trekhleb
Copy link
Owner

@dubzzz thank you for you PR. Could you please check my comments in code to this PR though. It seems like tests are correct now. It is ok to receive -1 when you're looking for an empty word in empty string. This would mean that there is no way of fetching empty word from empty string (string[0] <-- exception, since string length is 0).

@trekhleb trekhleb closed this Jul 23, 2018
@dubzzz
Copy link
Contributor Author

dubzzz commented Jul 23, 2018

Actually both String.prototype.indexOf and rabinKarp agree on the expected results. I really think that the implementation of knuthMorrisPratt should be updated accorindly:

console.log(rabinKarp('', '')); //0
console.log(rabinKarp('a', '')); //0
console.log(rabinKarp('a', 'a')); //0

console.log(''.indexOf('')); //0
console.log('a'.indexOf('')); //0
console.log('a'.indexOf('a')); //0

I can re-open the PR if you agree on it ;)

@trekhleb trekhleb reopened this Jul 24, 2018
@trekhleb trekhleb merged commit 0361fe5 into trekhleb:master Jul 24, 2018
shoredata pushed a commit to shoredata/javascript-algorithms that referenced this pull request Mar 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants