Skip to content

Commit ea7d06a

Browse files
authored
tests: Levenshtein Distance (dynamic programming solution) (TheAlgorithms#1114)
1 parent 9528c71 commit ea7d06a

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ npm test
9191
If you want save some time and just run a specific test:
9292

9393
```shell
94-
# this will run any test file where the filename matches "koch"
94+
# This will run any test file where the filename contains "koch" (no need to specify folder path)
9595
npm test -- koch
9696
```
9797

Dynamic-Programming/LevenshteinDistance.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
/**
2-
* A Dynamic Programming based solution for calculation of the Levenshtein Distance
3-
* https://en.wikipedia.org/wiki/Levenshtein_distance
2+
* @function calculateLevenshteinDp
3+
* @description A Dynamic Programming based solution for calculation of the Levenshtein Distance.
4+
* @param {String} x - Word to be converted.
5+
* @param {String} y - Desired result after operations.
6+
* @return {Integer} The Levenshtein distance between x and y.
7+
* @see [Levenshtein_distance](https://en.wikipedia.org/wiki/Levenshtein_distance)
48
*/
59

610
function minimum (a, b, c) {
@@ -18,7 +22,7 @@ function costOfSubstitution (x, y) {
1822
}
1923

2024
// Levenshtein distance between x and y
21-
function calculate (x, y) {
25+
function calculateLevenshteinDp (x, y) {
2226
const dp = new Array(x.length + 1)
2327
for (let i = 0; i < x.length + 1; i++) {
2428
dp[i] = new Array(y.length + 1)
@@ -39,4 +43,4 @@ function calculate (x, y) {
3943
return dp[x.length][y.length]
4044
}
4145

42-
export { calculate }
46+
export { calculateLevenshteinDp }
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { calculateLevenshteinDp } from '../LevenshteinDistance'
2+
3+
test('Should return the distance counting additions and removals', () => {
4+
const from = 'kitten'
5+
const to = 'sitting'
6+
expect(calculateLevenshteinDp(from, to)).toBe(3)
7+
})
8+
9+
test('Should return the distance based on replacements in the middle of the strings', () => {
10+
const from = 'book'
11+
const to = 'back'
12+
expect(calculateLevenshteinDp(from, to)).toBe(2)
13+
})
14+
15+
test('Should return the distance for strings with different length', () => {
16+
const from = 'sunday'
17+
const to = 'saturday'
18+
expect(calculateLevenshteinDp(from, to)).toBe(3)
19+
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy