Number letter counts
If the numbers 1 to 5 are written out in words: one, two, three,
four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.
If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?
If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?
NOTE: Do not count spaces or hyphens. For
example, 342 (three hundred and forty-two) contains 23 letters and 115
(one hundred and fifteen) contains 20 letters. The use of "and" when
writing out numbers is in compliance with British usage.
If you have come till here I am assuming that you have already solved Problem 14(Largest Collatz sequence). In problem 14 we have created a dictionary and have cached the values of the previous sequences and have reduced the run time by 90%. If you have not done the solution in that method I suggest you to have a look at the problem 14 solution and find the concept, because we have used the same concept here.
To be simple first we have stored the value of 1-20, 30, 40, 50, 60, 70, 80, 90 manually and then we have used a for loop from 20-99, created the values of these numbers and stores them in the dictionary so that we can use them again afterwards without repetition.
For example if I were to wite 115 in words then I would just break the number to 1, 15 and then find the value of 1 in dictionary as 'one'(3) and 15 in dictionary as 'fifteen'(7) and then simply add 'and hundred'(10) in the middle and get the value. As simple as that. If I had not used the memorization technique/dynamic programming then I will have to generate the value of the numbers again and again which will consume a lot of resources.
Okay anyways you will get a clear cut idea, once you will see the program.
And here is the program(It might look very large but the content in it is really small 😋):
The code is well commented again and I am assuming that there is no need for explaining each and every line. I have already explained the concept behind it(Not in a elaborate way but in a brief manner 😄).
If you want to download above program then you can download it from Github Gist
Guys I have not explained any part code directly because I have commented the code in a very neat and tidy way(Atleast for my imagination😝). But if you have any doubt or didn't understand anything then please do comment in the comment box below and I will be glad to answer your question.
Please feel free to comment in the comment box if I have made any typo or if you have a better/different code when compared to mine. Comment even if your code has more execution time compared to mine. Thinking different is important.
You can also contact me if you want to.
Thank you. Have a nice day😃.
To be simple first we have stored the value of 1-20, 30, 40, 50, 60, 70, 80, 90 manually and then we have used a for loop from 20-99, created the values of these numbers and stores them in the dictionary so that we can use them again afterwards without repetition.
For example if I were to wite 115 in words then I would just break the number to 1, 15 and then find the value of 1 in dictionary as 'one'(3) and 15 in dictionary as 'fifteen'(7) and then simply add 'and hundred'(10) in the middle and get the value. As simple as that. If I had not used the memorization technique/dynamic programming then I will have to generate the value of the numbers again and again which will consume a lot of resources.
Okay anyways you will get a clear cut idea, once you will see the program.
Program
I am assuming that if you were given a number you can write it in words. If not it is very easy and you can learn it from here: Number Pronunciation and Spelling About.com.And here is the program(It might look very large but the content in it is really small 😋):
The code is well commented again and I am assuming that there is no need for explaining each and every line. I have already explained the concept behind it(Not in a elaborate way but in a brief manner 😄).
If you want to download above program then you can download it from Github Gist
Output
Summary
I think this problem first tested whether you knew how to write numbers in words. And then tested if we will remember the concept behind problem 14. Leaving these apart the problem was straight forward and there were no twists, except someone would have missed adding the value of 'one thousand'. That's a silly mistake and the person would soon have rectified after re checking the code.Guys I have not explained any part code directly because I have commented the code in a very neat and tidy way(Atleast for my imagination😝). But if you have any doubt or didn't understand anything then please do comment in the comment box below and I will be glad to answer your question.
Please feel free to comment in the comment box if I have made any typo or if you have a better/different code when compared to mine. Comment even if your code has more execution time compared to mine. Thinking different is important.
You can also contact me if you want to.
Thank you. Have a nice day😃.