Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add tests for Levenshtein Distance dynamic programming solution
  • Loading branch information
JCarlosR committed Oct 1, 2022
commit cda424cf7dab23a936a0a0e5a4dbb773e073437b
12 changes: 8 additions & 4 deletions Dynamic-Programming/LevenshteinDistance.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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)
Expand All @@ -39,4 +43,4 @@ function calculate (x, y) {
return dp[x.length][y.length]
}

export { calculate }
export { calculateLevenshteinDp }
19 changes: 19 additions & 0 deletions Dynamic-Programming/tests/LevenshteinDistance.test.js
Original file line number Diff line number Diff line change
@@ -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)
})