diff --git a/snippets_archive/levenshteinDistance.md b/snippets_archive/levenshteinDistance.md new file mode 100644 index 000000000..5100d4d30 --- /dev/null +++ b/snippets_archive/levenshteinDistance.md @@ -0,0 +1,32 @@ +### levenshteinDistance + +Calculates the [Levenshtein distance](https://en.wikipedia.org/wiki/Levenshtein_distance) between two strings. + +Calculates the number of changes (substitutions, deletions or additions) required to convert `string1` to `string2`. +Can also be used to compare two strings as shown in the second example. + +``` js +const levenshteinDistance = (string1, string2) => { + if(string1.length === 0) return string2.length; + if(string2.length === 0) return string1.length; + let matrix = Array(string2.length + 1).fill(0).map((x,i) => [i]); + matrix[0] = Array(string1.length + 1).fill(0).map((x,i) => i); + for(let i = 1; i <= string2.length; i++){ + for(let j = 1; j<=string1.length; j++){ + if(string2[i-1] === string1[j-1]){ + matrix[i][j] = matrix[i-1][j-1]; + } + else{ + matrix[i][j] = Math.min(matrix[i-1][j-1]+1, matrix[i][j-1]+1, matrix[i-1][j]+1); + } + } + } + return matrix[string2.length][string1.length]; +}; +``` + +```js +levenshteinDistance('30-seconds-of-code','30-seconds-of-python-code'); // 7 +const compareStrings = (string1,string2) => (100 - levenshteinDistance(string1,string2) / Math.max(string1.length,string2.length)); +compareStrings('30-seconds-of-code', '30-seconds-of-python-code'); // 99.72 (%) +``` diff --git a/test/levenshteinDistance/levenshteinDistance.js b/test/levenshteinDistance/levenshteinDistance.js new file mode 100644 index 000000000..6b26dba3f --- /dev/null +++ b/test/levenshteinDistance/levenshteinDistance.js @@ -0,0 +1,18 @@ +const levenshteinDistance = (string1,string2) => { +if(string1.length === 0) return string2.length; +if(string2.length === 0) return string1.length; +let matrix = Array(string2.length+ 1).fill(0).map((x,i) => [i]); +matrix[0] = Array(string1.length + 1).fill(0).map((x,i) => i); +for(let i = 1;i <= string2.length;i++){ +for(let j = 1;j<=string1.length; j++){ +if(string2[i-1] === string1[j-1]){ +matrix[i][j] = matrix[i-1][j-1]; +} +else{ +matrix[i][j] = Math.min(matrix[i-1][j-1]+1,matrix[i][j-1]+1,matrix[i-1][j]+1); +} +} +} +return matrix[string2.length][string1.length]; +}; +module.exports = levenshteinDistance; \ No newline at end of file diff --git a/test/levenshteinDistance/levenshteinDistance.test.js b/test/levenshteinDistance/levenshteinDistance.test.js new file mode 100644 index 000000000..3c3c74404 --- /dev/null +++ b/test/levenshteinDistance/levenshteinDistance.test.js @@ -0,0 +1,13 @@ +const test = require('tape'); +const levenshteinDistance = require('./levenshteinDistance.js'); + +test('Testing levenshteinDistance', (t) => { + //For more information on all the methods supported by tape + //Please go to https://github.com/substack/tape + t.true(typeof levenshteinDistance === 'function', 'levenshteinDistance is a Function'); + //t.deepEqual(levenshteinDistance(args..), 'Expected'); + //t.equal(levenshteinDistance(args..), 'Expected'); + //t.false(levenshteinDistance(args..), 'Expected'); + //t.throws(levenshteinDistance(args..), 'Expected'); + t.end(); +}); \ No newline at end of file diff --git a/test/testlog b/test/testlog index e0ee56095..3f0e6708a 100644 --- a/test/testlog +++ b/test/testlog @@ -1,4 +1,4 @@ -Test log for: Mon Feb 26 2018 15:06:03 GMT+0530 (India Standard Time) +Test log for: Mon Feb 26 2018 15:00:57 GMT+0530 (India Standard Time) > 30-seconds-of-code@0.0.2 test R:\github\30-seconds-of-code > tape test/**/*.test.js | tap-spec @@ -538,12 +538,6 @@ Test log for: Mon Feb 26 2018 15:06:03 GMT+0530 (India Standard Time) √ Returns own methods √ Returns own and inherited methods - Testing fuzzySearch - - √ fuzzySearch is a Function - √ Provided examples work - √ Provided examples work - Testing gcd √ gcd is a Function @@ -904,10 +898,6 @@ Test log for: Mon Feb 26 2018 15:06:03 GMT+0530 (India Standard Time) √ isSet is a Function - Testing isSimilar - - √ isSimilar is a Function - Testing isSorted √ isSorted is a Function @@ -999,6 +989,10 @@ Test log for: Mon Feb 26 2018 15:06:03 GMT+0530 (India Standard Time) √ Returns the least common multiple of two or more numbers. √ Returns the least common multiple of two or more numbers. + Testing levenshteinDistance + + √ levenshteinDistance is a Function + Testing longestItem √ longestItem is a Function @@ -1527,6 +1521,11 @@ Test log for: Mon Feb 26 2018 15:06:03 GMT+0530 (India Standard Time) √ spreadOver is a Function √ Takes a variadic function and returns a closure that accepts an array of arguments to map to the inputs of the function. + Testing stableSort + + √ stableSort is a Function + √ Array is properly sorted + Testing standardDeviation √ standardDeviation is a Function @@ -1910,13 +1909,13 @@ Test log for: Mon Feb 26 2018 15:06:03 GMT+0530 (India Standard Time) √ Runs the function provided √ Runs promises in series √ Sends a GET request - √ Sends a POST request √ Works as expecting, passing arguments properly + √ Sends a POST request √ Works with multiple promises - total: 974 - passing: 974 - duration: 2.6s + total: 973 + passing: 973 + duration: 3s