Created
January 3, 2018 04:56
-
-
Save lqdev/ba0bdb4eab1ac2bdf246378e9612190c to your computer and use it in GitHub Desktop.
Largest Unique Set of Characters
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
/* | |
write a program that outputs the largest unique set of characters that can be removed | |
from this paragraph without letting its length drop below 50. | |
For example: [‘H’, ‘i’, ‘!’, ‘ ’] | |
*/ | |
//Exception based on paragraph length | |
function ParagraphLengthException(message){ | |
this.name = "ParagraphLengthException" | |
this.message = message; | |
} | |
//Tokenizes string based on specified split character | |
function tokenize(paragraph,split){ | |
return paragraph.split(split); | |
} | |
//Tokenize and convert string to lowercase | |
function prepareInput(paragraph,split){ | |
return tokenize(paragraph,split).join().toLowerCase(); | |
} | |
//Get largest unique set | |
function getLargestUniqueSet(characterArray,minLength){ | |
var result = new Set(); | |
var limit = characterArray.length - minLength; | |
if(characterArray.length<minLength) { | |
throw new ParagraphLengthException("Original paragraph is less than min length of " + minLength); | |
} | |
characterArray.forEach((element)=> { | |
if(!result.has(element) && limit > 0){ | |
result.add(element); | |
limit--; //Simulate removal of element from original character array. Prevents concurrent traversal/deletion access | |
} else if(result.has(element)) | |
{ | |
limit--; //Simulate removal of element from original character array. Prevents concurrent traversal/deletion access | |
} | |
}); | |
return result; | |
} | |
//Output largest unique set | |
function printSet(uniqueSet) | |
{ | |
var uniqueSetString = ""; | |
uniqueSet.forEach((element)=>{ | |
uniqueSetString = uniqueSetString + element + " "; | |
}) | |
console.log(uniqueSetString); | |
} | |
//Run Program | |
function Main(){ | |
//Tokenize and prepare original string input | |
var originalParagraph = "If you want to jumpstart the process of talking to us about this role, here’s a little challenge: write a program that outputs the largest unique set of characters that can be removed from this paragraph without letting its length drop below 50."; | |
var tokenSplit = " "; //Takes spaces into account. Change to '' in order to include spaces. | |
var characterArray = prepareInput(originalParagraph,tokenSplit).split(""); | |
var largestUniqueSet; | |
//Get larges unique set | |
try { | |
largestUniqueSet = getLargestUniqueSet(characterArray,50); | |
printSet(largestUniqueSet); //Output largest unique set | |
} catch(exception) { | |
console.log(exception.name,":",exception.message); | |
} | |
} | |
//Run Program | |
Main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment