From 30718536f0bb7a33169187ac9cdf3768183333a7 Mon Sep 17 00:00:00 2001 From: Juan Ramos Date: Sat, 1 Oct 2022 10:31:09 -0500 Subject: [PATCH 1/3] Update comment to clarify test command usage --- CONTRIBUTING.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index d54c7472cd..28659f6bd1 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -91,7 +91,7 @@ npm test If you want save some time and just run a specific test: ```shell -# this will run any test file where the filename matches "koch" +# This will run any test file where the filename contains "koch" (no need to specify folder path) npm test -- koch ``` From cda424cf7dab23a936a0a0e5a4dbb773e073437b Mon Sep 17 00:00:00 2001 From: Juan Ramos Date: Sat, 1 Oct 2022 10:51:50 -0500 Subject: [PATCH 2/3] Add tests for Levenshtein Distance dynamic programming solution --- Dynamic-Programming/LevenshteinDistance.js | 12 ++++++++---- .../tests/LevenshteinDistance.test.js | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 Dynamic-Programming/tests/LevenshteinDistance.test.js diff --git a/Dynamic-Programming/LevenshteinDistance.js b/Dynamic-Programming/LevenshteinDistance.js index ee2fbcbe40..71ff6fe0a2 100644 --- a/Dynamic-Programming/LevenshteinDistance.js +++ b/Dynamic-Programming/LevenshteinDistance.js @@ -1,6 +1,10 @@ /** - * A Dynamic Programming based solution for calculation of the Levenshtein Distance - * https://en.wikipedia.org/wiki/Levenshtein_distance + * @function calculateLevenshteinDp + * @description A Dynamic Programming based solution for calculation of the Levenshtein Distance. + * @param {String} x - Word to be converted. + * @param {String} y - Desired result after operations. + * @return {Integer} The Levenshtein distance between x and y. + * @see [Levenshtein_distance](https://en.wikipedia.org/wiki/Levenshtein_distance) */ function minimum (a, b, c) { @@ -18,7 +22,7 @@ function costOfSubstitution (x, y) { } // Levenshtein distance between x and y -function calculate (x, y) { +function calculateLevenshteinDp (x, y) { const dp = new Array(x.length + 1) for (let i = 0; i < x.length + 1; i++) { dp[i] = new Array(y.length + 1) @@ -39,4 +43,4 @@ function calculate (x, y) { return dp[x.length][y.length] } -export { calculate } +export { calculateLevenshteinDp } diff --git a/Dynamic-Programming/tests/LevenshteinDistance.test.js b/Dynamic-Programming/tests/LevenshteinDistance.test.js new file mode 100644 index 0000000000..2c250f391f --- /dev/null +++ b/Dynamic-Programming/tests/LevenshteinDistance.test.js @@ -0,0 +1,19 @@ +import { calculateLevenshteinDp } from '../LevenshteinDistance' + +test('Test Case 1', () => { + const from = 'kitten' + const to = 'sitting' + expect(calculateLevenshteinDp(from, to)).toBe(3) +}) + +test('Test Case 2', () => { + const from = 'book' + const to = 'back' + expect(calculateLevenshteinDp(from, to)).toBe(2) +}) + +test('Test Case 3', () => { + const from = 'sunday' + const to = 'saturday' + expect(calculateLevenshteinDp(from, to)).toBe(3) +}) From 420471a44f4d69bc9e7781dc8a4f9e61b32b8b0b Mon Sep 17 00:00:00 2001 From: Juan Ramos Date: Sat, 1 Oct 2022 15:02:18 -0500 Subject: [PATCH 3/3] Add more descriptive titles --- Dynamic-Programming/tests/LevenshteinDistance.test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Dynamic-Programming/tests/LevenshteinDistance.test.js b/Dynamic-Programming/tests/LevenshteinDistance.test.js index 2c250f391f..6d13e31c20 100644 --- a/Dynamic-Programming/tests/LevenshteinDistance.test.js +++ b/Dynamic-Programming/tests/LevenshteinDistance.test.js @@ -1,18 +1,18 @@ import { calculateLevenshteinDp } from '../LevenshteinDistance' -test('Test Case 1', () => { +test('Should return the distance counting additions and removals', () => { const from = 'kitten' const to = 'sitting' expect(calculateLevenshteinDp(from, to)).toBe(3) }) -test('Test Case 2', () => { +test('Should return the distance based on replacements in the middle of the strings', () => { const from = 'book' const to = 'back' expect(calculateLevenshteinDp(from, to)).toBe(2) }) -test('Test Case 3', () => { +test('Should return the distance for strings with different length', () => { const from = 'sunday' const to = 'saturday' expect(calculateLevenshteinDp(from, to)).toBe(3)