Last active
February 4, 2018 20:09
-
-
Save uncompiled/67c6cf9797ab08fb3ca6915761b55b28 to your computer and use it in GitHub Desktop.
All Tests Pass Week 3: Manual Minification
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This file includes the reasoning behind my manual minification choices. | |
// I wouldn't write code like this for an actual project because if I | |
// looked at this code in 2 months, I'd probably say WTF. | |
function largestCommonSubstring(a, b) { | |
// Use one declaration to minimize the number of let statements | |
let l = 0, // Length of the longest common substring | |
s = '', // Substring to return | |
// Generate mxn array filled with zeroes | |
// I would probably call this "lcs" in a real project, | |
// but "d" is shorthand for "dynamic programming table" | |
d = Array(a.length).fill([]).map(() => Array(b.length).fill(0)) | |
/* Removed the equality check because it is an optimization that does not | |
* affect the return value. It's something that you might want in production | |
* code because it avoids unnecessary work, but we're playing code golf. | |
*/ | |
// Using forEach loop, we can get rid of length checking and boundary | |
// conditions. The matrix represents a grid with each string as an axis | |
// so we can use those boundaries to iterate over the strings. | |
// The second parameter of the forEach callback is the index. | |
d.forEach((r, j) => | |
r.forEach((c, i) => | |
// Because we initialized the matrix to all zeroes, | |
// we could get rid of the null check. | |
// We really only care if the two characters are the same. | |
// I'm sorry for the nested ternary expressions, but it saves characters! | |
a[j] === b[i] | |
// If we're in the first row/col, the longest substring must be length 1 | |
// Otherwise, the new longest substring is one longer | |
? ((d[i][j] = i && j ? d[i - 1][j - 1] + 1 : 1), | |
// Using Math.max lets us skip the conditional check | |
(l = Math.max(l, d[i][j]))) | |
// If the characters aren't the same, reset to zero. | |
: d[i][j] = 0 | |
) | |
) | |
// At this point, we have the longest common substring in the table, | |
// so we use forEach to iterate over the elements of the table and | |
// use the indicies of the table to get the substring out of the input string | |
d.forEach((r, _) => | |
r.forEach((c, i) => { | |
if (c === l) s = a.slice(i - (l - 1), i + 1) | |
}) | |
) | |
// !1 is shorter than false, but don't use this in real code | |
return s === '' ? !1 : s | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Week 3 - Largest Common Substring (Manual Minification) | |
* | |
* Given two strings, return either a string that matches the largest common substring or false. | |
* | |
* Examples: | |
* largestCommonSubstring("foo", "bar"); // false | |
* largestCommonSubstring("foobarbaz", "foo"); // "foo" | |
* largestCommonSubstring("foobarbaz", "bazbaz"); // "baz" | |
* largestCommonSubstring("the quick brown fox", "i like brown as a color"); // "brown" | |
**/ | |
function largestCommonSubstring(a, b) { | |
let l = 0, | |
s = '', | |
d = Array(a.length).fill([]).map(() => Array(b.length).fill(0)) | |
d.forEach((r, j) => | |
r.forEach((c, i) => | |
a[j] === b[i] | |
? ((d[i][j] = i && j ? d[i - 1][j - 1] + 1 : 1), | |
(l = Math.max(l, d[i][j]))) | |
: d[i][j] = 0 | |
) | |
) | |
d.forEach((r, _) => | |
r.forEach((c, i) => { | |
if (c === l) s = a.slice(i - (l - 1), i + 1) | |
}) | |
) | |
return s === '' ? !1 : s | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment