Skip to content

wordsfornumbers

bradendubois edited this page Jun 9, 2021 · 6 revisions

Words for Numbers

ID: wordsfornumbers

Difficulty: 2.6

CPU Time: 1 second

Memory: 1024 MB

Solution

For this, the actual lookup/conversion is more tedious/implementation-time than comprehension. An easy way (all things being relative) to do the lookup of a number if to take the ones' and tens' digits, and just do some enumeration from 0-10 for each of them, and figure out the two numbers (as words) they would be: "twenty, thirty, ..." for the tens, "one, two, ..." for the one's. After this matching, if the ones' digit == 0, then just return the tens' word; if the tens' digit is >= 2, return the tens concatenated with the ones by a hyphen; if the tens' digit is a zero, the ones' word can be returned. This leaves some hardcoding eleven to nineteen. Special cases (by this implementation) to check at the start are zero and ten.

Clone this wiki locally