Created
August 10, 2021 02:29
-
-
Save mflorida/22082e0f68afb43f3852b7497bcd5c06 to your computer and use it in GitHub Desktop.
Remove '100' until string is either empty or not equal to '100'
This file contains 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
function writeOutput(inputData) { | |
// Start writing code here to consume input, and return result. | |
// separate input for consumptino | |
const inputParts = inputData.split('\n'); | |
// shift first element to serve as line count | |
const lineCount = Number(inputParts.shift()); | |
// update this after we have a result to minimize time complexity (no nested loops) | |
let inputIndex = 0; | |
function while100(input){ | |
let guard = 0; | |
function doReplace(str){ | |
if (guard += 1 > 100000) return 'STOP'; | |
return str.replace('100', '') | |
} | |
let result = null; | |
let a = doReplace(input); | |
// result will stay null until | |
// one or the other condition is met | |
while (result === null) { | |
let b = doReplace(a); | |
// replacement result will be 100 or an empty string | |
if (b === '100' || b === '') { | |
result = true; | |
} | |
if (a === b) { | |
result = false; | |
} | |
// console.log(''); | |
// console.log('- - - - - - - - - -') | |
// console.log('a', a); | |
// console.log('b', b); | |
// console.log('- - - - - - - - - -') | |
// do the switcheroo | |
a = b; | |
} | |
return result; | |
} | |
for (let item of inputParts) { | |
if (++inputIndex > lineCount) { | |
break; | |
} | |
if (while100(item)) { | |
console.log('yes') | |
} | |
else { | |
console.log('no') | |
} | |
} | |
return '' | |
} | |
const inputData = '' + | |
'8\n' + | |
'101000\n' + // yes | |
'10111000101000\n' + // no | |
'1010001\n' + // no | |
'10010011001001100\n' + // no | |
'101010000\n' + // yes | |
'10010010010010010\n' + // no | |
'100100100100100101\n' + // no | |
'101000101000101000101000100'; // yes | |
writeOutput(inputData); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment