Created
October 12, 2023 23:07
-
-
Save theredpea/57e6ad4b52365b51a06422219112230a to your computer and use it in GitHub Desktop.
Technical Challenge Interview
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
/******************/ | |
/**** Solution ****/ | |
/******************/ | |
function emboldenSubstrings(input, substrings) { | |
// Implement me! | |
// if (!substrings.length) { | |
// return input; | |
// } | |
// // ['abc', '123'] -> '(abc|123)' | |
// // would break if substrings included [')', '|', ...any other regex special characters...]; | |
// // filter out garbage/redundant values like [''] | |
// substrings = substrings.filter(_ => _); | |
// // 'abc123' <- ['abc', 'c12']; | |
// // -----vvvvvvv | |
// //'<b>abc</b>12' | |
// //'ab<b>c12</b>' | |
// // -----vvvvvvv | |
// //'<b>ab<b>c</b>12</b>' | |
// // input: | |
// // 'abc123' <- ['abc', 'c12']; | |
// // expected output: | |
// // '<b>abc12<b>3' | |
// // 'abc123' <- ['abc', 'c12', ... , 'abcc12', 'abc12']; | |
// const substringRegex = new RegExp('(' + substrings.join('|') + ')', 'g'); | |
// const replacedInput = input.replace(substringRegex, (matchedPart) => { return '<b>' + matchedPart + '</b>' }); | |
// // vvvvvvvvvv vvvv | |
// // ['abc', 'c1', 'c12'] | |
// // vvvvvvvvvvv | |
// ['a', 'b', 'c', '1', '2', '3'] | |
// // loop approach: | |
// // | |
let inputToBeReplacedMask = Array.apply(null, Array(input.length)).map(_ => 0); | |
substrings.forEach(substring => { | |
// replacedInput = input.replace() | |
const foundIndex = input.indexOf(substring); | |
if (foundIndex >= 0) { | |
//0...3 | |
// [0, 0, 0, 0, 0, 0] | |
for (var i = foundIndex; i < foundIndex + substring.length; i++) { | |
inputToBeReplacedMask[i] = 1; | |
} | |
} | |
// console.log(inputToBeReplacedMask); | |
// 'abc12' | |
// const longestConcattedSubstring = | |
// 'abc' -> '|||' | |
// 0, 3 | |
// 'c12' -> '|||' | |
// 2, 5 | |
// [1,1,1,1,1,0] | |
// '||||||' -> '<b>*orig*</b>' | |
// 0, 5 <-- | |
}); | |
// 2 replacements: | |
// [ | |
// 1, 1, 1, 0, 0, | |
// 0, 1, 1, 1 | |
// ] | |
let fromIndex = 0; | |
let toIndex = 0; | |
let replacedInput = ''; | |
inputToBeReplacedMask.forEach((currentMaskVal, currentIndex) => { | |
const nextMaskVal = inputToBeReplacedMask[currentIndex + 1]; | |
const previousMaskVal = inputToBeReplacedMask[currentIndex - 1]; | |
if (nextMaskVal!== currentMaskVal | |
|| currentIndex === inputToBeReplacedMask.length-1){ | |
toIndex = currentIndex+1; | |
if (currentMaskVal===1){ | |
replacedInput += '<b>'+input.slice(fromIndex, toIndex)+'</b>'; | |
} else { | |
replacedInput += input.slice(fromIndex, toIndex); | |
} | |
fromIndex = toIndex; | |
toIndex = undefined; | |
} | |
}); | |
// 1 replacement: | |
// [ 1, 1, 1, 1, 1, 0 ] | |
return replacedInput; | |
} | |
/******************/ | |
/****** Tests *****/ | |
/******************/ | |
// testing function, please ignore | |
const it = (desc, result, assertion) => { | |
if (Array.isArray(assertion) && assertion.some(value => value === result)) { | |
return console.log('True!', desc, '\n') | |
} | |
if (result == assertion) return console.log('True!', desc, '\n') | |
console.log('False:', desc) | |
if (Array.isArray(assertion)) { | |
console.log('\texpected result to be included in: ', assertion) | |
} else { | |
console.log('\texpected: ', assertion) | |
} | |
console.log('\tgot: ', result, '\n') | |
} | |
it('handles no substrings', emboldenSubstrings('abcxyz', []), 'abcxyz') | |
it('handles one substring', emboldenSubstrings('abcxyz', ['abc']), '<b>abc</b>xyz') | |
it('handles two substrings', emboldenSubstrings('abcxyz123', ['abc', '123']), '<b>abc</b>xyz<b>123</b>') | |
it('handles two substrings', emboldenSubstrings('abc123', ['abc', 'c12']), '<b>abc12</b>3') | |
// console.log('abc'.slice(1,2)) | |
// console.log('abc'.slice(1,1)) | |
// | |
// Add more tests here! | |
// |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment