Munkith Abid
3 min readJan 6, 2022

--

numsToDigits problem solution

Recently at one of the coding assessments I’ve come across, one of the coding challenges was to convert string numerals in their literal form to a string representing the original string but through digits instead of numerals literals. I deliberately changed the original function name given in the test and won’t mention the company name to keep it from googlers!

On top of the given string being literal, it also got its letters all scrambled, and the resulting output is required to be in ascending order. So the input to the function would be something like:

’vzoireofeurf’

and the output is expected to be

‘045’

The given string is guaranteed to map to its respective digital representation without any extra letter; that is also given.

Now needless to mention that I couldn't get this challenge solved during the assessment session as it was given only 20 minutes, and I’ve not majored in Data Structures and Algorithms. Nevertheless, I gave it a shot after the test just because I couldn't get it out of my head, and this is out I came up with.

Firs observation we can make when looking at the input is that some of the numerals have unique characters that can’t be found in other numerals. For example:

The numeral “zero” has the letter ‘z’ unique in it, and no other numeral has the letter ‘z’ in it. That sounds promising because it means that only counting how many ‘z’’s in our input string will automatically indicate how many zeros that string with having to look for the rest of the letters that make up the numeral ‘zero.’ The same thing applies the numeral ‘two,’ it has ‘was its unique character, and ‘four’ has ‘u’ and so on.

The second observation we could make is that even though the rest of our numerals don’t have a unique character, some of them will end up having unique characters once we remove the first set mentioned above from the input string, for example:

‘five’ has the letter ‘f’ almost to itself. It only shares it with ‘four.’ So if we count how many ‘f’’s we have in the string, that’ll give the total of all ‘five’s and ‘four’s in the string. But we already know that ‘u’ is unique to ‘four,’ so counting how many ‘u’s in the string and subtracting it from the total ‘f’s, the result will be how many ‘five’s in the string. The exact mechanism will apply to the rest of the possible numerals for (1–9).

Once we create the data structure to map each digit to its unique letter and also mention the extra letters that the numeral depends on, we should be all set, and the rest of the program will be just as easy as repeating the digits found as many times as their count.

Below is the solution snippet:

function numsToDigits(str) {const strArr = str.split('');const numeralsSpecials = [['0', 'z'], ['1', 'o', 'z', 'w', 'u'], ['2', 'w'], ['3', 'h', 'g'], ['4', 'u'],['5', 'f', 'u'], ['6', 'x'], ['7', 's', 'x'], ['8', 'g'], ['9', 'i', 'x', 'g']];let res = '';const findCount = (char) => strArr.filter(ch => char === ch).length;function numsToDigits(digit, uniq, ...shared) {let totalShared = 0;let initialUniqCount = findCount(uniq);for (let i = 0; i < shared.length; i++) {totalShared += findCount(shared[i])}let actualUniqCount = initialUniqCount - totalShared;return digit.repeat(actualUniqCount);}for (let i = 0; i < numeralsSpecials.length; i++) {res += numsToDigits(...numeralsSpecials[i]);}

You can try out different test cases, and it will always yield the expected result based on the explanation above.

--

--