Added more implementations (JS) (#67)

* Added recursive binary search (JS)

* Added recursive selection sorting

* Added another loop implementation sum func by reduce (JS)

* Recursion reduced by one iteration

* Recursive binary search in ES6 added to appropriate folder

* JS files ordered by standards (ES4/ES6)

* Added hashtable implementation in JS

* Fixed typo with LENGTH prop

* Added Euclidian algorithm for two numbers and set of them

* Added universal selection sort

* Commented output

* Added ES6 version of Euclidean algorithm

* Converted from ES6 to ES5

* #69 Added search for LCS

* #69 added levenstein algorithm

* Removed excessive property

* Removed excessive file

* Removed excessive function calls

* Removed excessive file

* Removed excessive file

* Renamed

* Fixed indentation
This commit is contained in:
Artem Solovev
2018-08-24 21:21:54 +03:00
committed by Aditya Bhargava
parent a5fe9178dd
commit 0d5d0164ce
12 changed files with 502 additions and 29 deletions

View File

@@ -1,7 +1,78 @@
if (word_a[i] === word_b[j]) {
// The letters match
cell[i][j] = cell[i-1][j-1] + 1;
} else {
// The letters don't match
cell[i][j] = Math.max(cell[i-1][j], cell[i][j-1]);
/**
* Search for LCS
*
* @param {string} string1 first string
* @param {string} string2 second string
*
* @return {object} with keys: lcs, offset, sequence
*/
function lcs(string1, string2) {
if (!string1 || !string2) {
return {
lcs: 0,
offset: 0,
sequence: ""
};
}
let lcs = 0;
let lastSubIndex = 0;
let table = [];
let len1 = string1.length;
let len2 = string2.length;
let row;
let col;
/**
* Matrix
* - has an increased dimension to avoid extra checks for previous elements
*
* - the number of rows is equal to the length of the first string + 1
* - the number of columns is equal to the length of the second string + 1
*/
for (row = 0; row <= len1; row++) {
table[row] = [];
for (col = 0; col <= len2; col++) {
table[row][col] = 0;
}
}
// Fill the matrix
let i;
let j;
for (i = 0; i < len1; i++) {
for (j = 0; j < len2; j++) {
if (string1[i] === string2[j]) {
// The letters match
if (table[i][j] === 0) {
table[i + 1][j + 1] = 1;
} else {
table[i + 1][j + 1] = table[i][j] + 1;
}
// increment lcs if it's needed
if (table[i + 1][j + 1] > lcs) {
lcs = table[i + 1][j + 1];
lastSubIndex = i;
}
} else {
// The letters don't match
table[i + 1][j + 1] = 0;
}
}
}
return {
lcs: lcs,
offset: lastSubIndex - lcs + 1,
sequence: string1.slice(lastSubIndex - lcs + 1, lastSubIndex + 1)
};
}
console.log(lcs("hish", "fish")); // { lcs: 3, offset: 1, sequence: 'ish' }
console.log(lcs("vista", "hish")); // { lcs: 2, offset: 1, sequence: 'is' }
console.log(lcs("google", "abcdefgooglehijklm")); // { lcs: 6, offset: 0, sequence: 'google' }
console.log(lcs("0", 0)); // { lcs: 0, offset: 0, sequence: '' }

View File

@@ -0,0 +1,47 @@
/**
* Compute the edit distance between the two given strings
*
* @param {*} source
* @param {*} target
*
* @return {number}
*/
function getEditDistance(source, target) {
if (source.length == 0) return target.length;
if (target.length == 0) return source.length;
let i;
let j;
let matrix = [];
// Fill the column
for (i = 0; i <= target.length; i++) {
matrix[i] = [i];
}
// Fill the column
for (j = 0; j <= source.length; j++) {
matrix[0][j] = j;
}
// Fill in the rest of the matrix
for (i = 1; i <= target.length; i++) {
for (j = 1; j <= source.length; j++) {
if (target.charAt(i - 1) == source.charAt(j - 1)) {
matrix[i][j] = matrix[i - 1][j - 1];
} else {
matrix[i][j] = Math.min(
matrix[i - 1][j - 1] + 1, // substitution
Math.min(
matrix[i][j - 1] + 1, // insertion
matrix[i - 1][j] + 1
)
); // deletion
}
}
}
return matrix[target.length][source.length];
}
console.log(getEditDistance("google", "notgoogl")); // 4