mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-04 15:39:42 +08:00
tests: Levenshtein Distance (dynamic programming solution) (#1114)
This commit is contained in:
@ -91,7 +91,7 @@ npm test
|
|||||||
If you want save some time and just run a specific test:
|
If you want save some time and just run a specific test:
|
||||||
|
|
||||||
```shell
|
```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
|
npm test -- koch
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
/**
|
/**
|
||||||
* A Dynamic Programming based solution for calculation of the Levenshtein Distance
|
* @function calculateLevenshteinDp
|
||||||
* https://en.wikipedia.org/wiki/Levenshtein_distance
|
* @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) {
|
function minimum (a, b, c) {
|
||||||
@ -18,7 +22,7 @@ function costOfSubstitution (x, y) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Levenshtein distance between x and y
|
// Levenshtein distance between x and y
|
||||||
function calculate (x, y) {
|
function calculateLevenshteinDp (x, y) {
|
||||||
const dp = new Array(x.length + 1)
|
const dp = new Array(x.length + 1)
|
||||||
for (let i = 0; i < x.length + 1; i++) {
|
for (let i = 0; i < x.length + 1; i++) {
|
||||||
dp[i] = new Array(y.length + 1)
|
dp[i] = new Array(y.length + 1)
|
||||||
@ -39,4 +43,4 @@ function calculate (x, y) {
|
|||||||
return dp[x.length][y.length]
|
return dp[x.length][y.length]
|
||||||
}
|
}
|
||||||
|
|
||||||
export { calculate }
|
export { calculateLevenshteinDp }
|
||||||
|
19
Dynamic-Programming/tests/LevenshteinDistance.test.js
Normal file
19
Dynamic-Programming/tests/LevenshteinDistance.test.js
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
import { calculateLevenshteinDp } from '../LevenshteinDistance'
|
||||||
|
|
||||||
|
test('Should return the distance counting additions and removals', () => {
|
||||||
|
const from = 'kitten'
|
||||||
|
const to = 'sitting'
|
||||||
|
expect(calculateLevenshteinDp(from, to)).toBe(3)
|
||||||
|
})
|
||||||
|
|
||||||
|
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('Should return the distance for strings with different length', () => {
|
||||||
|
const from = 'sunday'
|
||||||
|
const to = 'saturday'
|
||||||
|
expect(calculateLevenshteinDp(from, to)).toBe(3)
|
||||||
|
})
|
Reference in New Issue
Block a user