We have another challenge from MMMAAANNN on Codewars. Today, you are asked to:
Turn a given number (an integer > 0, < 1000) into the equivalent English words.
For example:wordify(1) == "one"
wordify(12) == "twelve"
wordify(17) == "seventeen"
wordify(56) == "fifty six"
wordify(90) == "ninety"
wordify(326) == "three hundred twenty six"
Good luck, and happy coding!
Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge for a future post? Email yo+challenge@dev.to with your suggestions!
Oldest comments (11)
I would cheat and use google translate api xD
JavaScript
Live demo on CodePen.
I tried to not dictionary everything, but the exceptions are real. Lots of amusing output as I got closer, that I'll paste for your enjoyment. I did actually get it working though :P
In Perl, we have CPAN, where you can find modules for almost any task imaginable. Including Lingua::EN::Numbers.
Here's a JS one that goes into the gazillions 🤔
English is fun what with all the exceptions...
Gist: gist.github.com/kerrishotts/ea2bd9...
Try French. I’ll pass you the details of “quatre-vingt-dix-neuf” for 99, but you also have “vingt et un” (21), “vingt deux” (22), “deux cents” (200, note the s) but “deux cent un” (201).
Common Lisp is a good choice for this:
Rust Solution!
Pretty hard coded. Probably could be made much more compact and general. But this satisfies the requirements!
I’m learning Erlang, and I got to use file I/O and a list comprehension for the unit tests (in the Gist version).
Short version:
In this Gist, there’s a test file with all values from 1 to 999, and a EUnit test that loads this file and generates a test for each value.