Last active
February 2, 2020 21:13
-
-
Save alessbell/caef8a9e227462d76ad1463a5660a842 to your computer and use it in GitHub Desktop.
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
const paragraph = | |
'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.'; | |
// First we'll take the set of unique characters | |
const uniqueCharacters = str => { | |
const chars = []; | |
str.split('').forEach(char => { | |
if (!chars.includes(char)) { | |
chars.push(char); | |
} | |
}); | |
return chars; | |
}; | |
// ...and count the number of times each character can be found in our original text. | |
// We can then sort them by frequency. | |
const countAndSortCharacters = characters => { | |
const countedChars = {}; | |
const sortedVals = {}; | |
characters.forEach(char => { | |
let escapedChar = char; | |
if (char === '.') { | |
escapedChar = '\\.'; // escape period | |
} | |
countedChars[char] = paragraph.match(new RegExp(escapedChar, 'g')).length; | |
}); | |
Object.keys(countedChars) | |
.sort((a, b) => countedChars[a] - countedChars[b]) | |
.forEach(val => { | |
sortedVals[val] = countedChars[val]; | |
}); | |
return sortedVals; | |
}; | |
// Last, we can use a simple regular expression to filter an arbitrary | |
// list of characters out of a string. | |
const removeCharacters = (charachters, str) => { | |
const reg = new RegExp(charachters.join('|'), 'g'); | |
return str.replace(reg, ''); | |
}; | |
const uniques = uniqueCharacters(paragraph); | |
const countedCharacters = countAndSortCharacters(uniques); | |
console.log(countedCharacters); | |
// { | |
// '0': 1, | |
// '5': 1, | |
// I: 1, | |
// y: 1, | |
// j: 1, | |
// k: 1, | |
// ',': 1, | |
// '’': 1, | |
// ':': 1, | |
// q: 1, | |
// v: 1, | |
// '.': 1, | |
// d: 2, | |
// b: 3, | |
// f: 4, | |
// w: 4, | |
// m: 4, | |
// c: 5, | |
// n: 7, | |
// p: 7, | |
// g: 7, | |
// u: 9, | |
// i: 9, | |
// l: 10, | |
// s: 12, | |
// h: 12, | |
// o: 15, | |
// r: 15, | |
// a: 17, | |
// e: 20, | |
// t: 29, | |
// ' ': 42 | |
// } | |
// By observing this output, we can immediately see that there are several possible answers, | |
// to this question, but each correct answer is a list of 30 characters: | |
// we can remove all characters *except* the space character (appearing 42 times) and one other character | |
// appearing 8 or more times (in the ranked list, that's characters u to t) in order to build the | |
// largest set of characters that can be removed without the remaining text dropping below 50 characters. | |
// Thus, there are ten valid answers, all lists 30 characters in length. | |
// For example: | |
console.log( | |
removeCharacters( | |
[ | |
'0', | |
'5', | |
'I', | |
'y', | |
'j', | |
'k', | |
',', | |
'’', | |
':', | |
'q', | |
'v', | |
'\\.', // escaped period | |
'd', | |
'b', | |
'f', | |
'w', | |
'm', | |
'c', | |
'n', | |
'p', | |
'g', | |
'u', | |
'i', | |
'l', | |
's', | |
'h', | |
'o', | |
'r', | |
'a', | |
'e', | |
], | |
paragraph | |
).length | |
); | |
// 71 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment