Character Count Algorithm Solution Two

Sejan Miah
killingMeSwiftly
Published in
4 min readSep 29, 2017

Recently I was asked to solve an algorithm:

In Swift, write a that counts the total number of characters of every number between two numbers (inclusive) spelled out, with whitespace, hyphens and “and” omitted. For example, “525” to “526” spelled out is “fivehundredtwentyfivefivehundredtwentysix”, so your function should return “41” for the total number of characters. Your function should handle a maximum input of “1000”.

Your function should adhere to the following signature:

func countCharacters(from start: Int, to end: Int) -> Int

For example, one through three, would be “onetwothree”, which has “11” characters; “fortyone” has “8” characters; and “onehundredone” has “13” characters. Therefore:

“countCharacters(from: 1, to: 3)” should return “11”

“countCharacters(from: 41, to: 41)” should return “8”

“countCharacters(from: 101, to: 101)” should return “13”

Your definition of “countCharacters” should return the correct answer for “countCharacters(from: 0, to: 1000)” (please test your code with this input). Please avoid using NSNumberFormatter, or any other method of automatically converting the numbers to strings.

My previous solution for you guys included extended the struct Int and calling on the class NumberFormatter and returning the word form of each number. Turns out that was thought of as a short-cut and I should have solved the algorithm without any helper methods.

This is my attempt at doing so:

First we need to account for the fact that we are going to count characters for any number in the ones place. If there is no number aka 0 we will have nothing to account for, hence “”. If we have the number 1, then the key will be 1 and the value will be three. Do this for all single digit numbers and prime numbers.

Then we have our tens place numbers.

We repeat a similar process, except we don’t have too much repetition. Once again account for the proper key/value pairings, ignoring the first element zero with an empty string, hence a 0 count in its value. Don’t you just love the enumerated method?

Now let’s get into the actual function.

First thing we’re going to do is account for what happens if we encounter the number 1000, why we’re going to give it a default count, just like with if n was 100. Please note n is our default placeholder, you can call it whatever you like, I just remembered in algebra using n a lot.

We also need a variable to keep track of our sum.

There’s a lot going on here. Let me walk you through it. So what do we care about in the end? The total number of characters AKA the sum. So if number is less than 20, let’s subscript in our dictionary and get a count. If the number is greater than 99, we can continue subscripting with n and accommodating for the ever changing sum.

Once outside of that particular scope where you are defining how to calculate the sum, you just add the sums together for the tens and ones place dictionaries.

Finally, call this function within the countCharacters function.

The math required a lot of playing around for me to fully understand. If the code is too segmented, please hunt me down and I’ll send it to you. Again, not sure why we had to do this the hard way, but I guess it really does help you understand and appreciate the functions you have around you that you take for granted.

--

--