Skip to content

Instantly share code, notes, and snippets.

@xkeshav
Last active September 28, 2021 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xkeshav/4c1e677eef3f2055caa0381b5dabe7fa to your computer and use it in GitHub Desktop.
Save xkeshav/4c1e677eef3f2055caa0381b5dabe7fa to your computer and use it in GitHub Desktop.
Interview question: find numeral letter from a random string
const input = 'torzeonthowoereezero';
// idea here is, first check that every letter of numeral exist in the given string,
// if it exist then remove the numeral letter from given string and search for next numeral and so on
const numeral = {
'zero' : 0,
'one': 1,
'two': 2,
'three': 3,
'four' : 4,
'five': 5,
'six': 6,
'seven': 7,
'eight': 8,
'nine':9
};
const indexFound = (word, str) => str.indexOf(word) !== -1;
// check whether every letter exist on given string
const isExist = (word, str) => [...word].every((w) => indexFound(w, str));
//replace letter in string
const replaceLetter = (word, from) => word.replace(from, '');
const replacedWord = (word, str) => [...word].reduce(replaceLetter, str);
// check the occurrence of numeral letter in given string
const findOccurrence = ([sentence, collector], word) => {
const exist = isExist(word, sentence);
if(exist) {
collector.push((numeral[word])); // push respective number in separate array
const replacedString = replacedWord(word, sentence); // replace match word from input string
return [replacedString, collector];
} else {
return [sentence, collector];
}
}
const findJumbleWord = (input, next = []) => {
// tricky part is we are sending array as initial value so that
// if there is multiple instance of the same literal then it will be done on next time calling of same method
const [remains] = Object.keys(numeral).reduce(findOccurrence, [input, next]);
if(remains.length > 2 ) { // as we know no numeral is less than 3 letter
findJumbleWord(remains, next);
}
return next;
};
const result = findJumbleWord(input);
// now if we require to sort ascending and display as a number then
const resultSorted = result.sort().join('');
console.log({result, resultSorted});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment