diff --git a/snippets_archive/levenshteinDistance.md b/snippets_archive/levenshteinDistance.md deleted file mode 100644 index 646adc298..000000000 --- a/snippets_archive/levenshteinDistance.md +++ /dev/null @@ -1,42 +0,0 @@ ---- -title: levenshteinDistance -tags: algorithm,advanced ---- - -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 (%) -``` \ No newline at end of file diff --git a/test/levenshteinDistance.test.js b/test/levenshteinDistance.test.js deleted file mode 100644 index 27f5fbddf..000000000 --- a/test/levenshteinDistance.test.js +++ /dev/null @@ -1,14 +0,0 @@ -const {levenshteinDistance} = require('./_30s.js'); - -test('levenshteinDistance is a Function', () => { - expect(levenshteinDistance).toBeInstanceOf(Function); -}); -test('levenshteinDistance returns the correct results', () => { - expect(levenshteinDistance('30-seconds-of-code', '30-seconds-of-python-code')).toBe(7); -}); -test('levenshteinDistance returns the correct result for 0-length string as first argument', () => { - expect(levenshteinDistance('', 'foo')).toBe(3); -}); -test('levenshteinDistance returns the correct result for 0-length string as second argument', () => { - expect(levenshteinDistance('bar', '')).toBe(3); -});