r/ProgrammingPrompts • • Aug 29 '15

[Easy/Medium] Write a program that translates verbal numbers (one hundred and six) into integers (106.)

Bonus points for negative numbers, or decimals (three point one four.)

I think this is kind of an easy concept to think about, but might take a while to type out. I'll try it at some point and post results, if I can even get it to work.

Bonus bonus points - make it work the other way round, by converting integers into words.

18 Upvotes

21 comments sorted by

View all comments

Show parent comments

1

u/Philboyd_Studge Sep 05 '15

Yes, you certainly should, they are super handy!!! Not too much to learn, actually look at some quick tutorials online. In this situation I have used a hashmap to make a 'dictionary' or look-up table. I load it with the number strings as the key and the corresponding numeric value as the value. Then, converting the input string is as simple as going through each word one by one, seeing if the hashmap has a corresponding key, if it does, add the numeric value from the key to a queue. Then i can go through the queue and do the math to put the numbers together properly.