HardPro challengePythonJavaScriptTypeScript
Math — Integer to English Words
AlgorithmsMathStringsRecursion
solve(num) — convert a non-negative integer (0 <= num < 2^31) to its English words. Use
the exact format below so the result is unique: words are Capitalised, separated by single
spaces, no "and" and no hyphens.
0is"Zero";- tens above nineteen are
Twenty, Thirty, Forty, Fifty, Sixty, Seventy, Eighty, Ninety; - groups of three digits are followed by
Thousand,MillionorBillion, and a group of
000 is skipped entirely.
Split the number into groups of three digits from the right. Write a helper for a number
below 1000 (hundreds, then the tens / teens / ones), then glue the groups together with their
scale word, from the largest to the smallest.
Examples
solve(123) → "One Hundred Twenty Three"
solve(12345) → "Twelve Thousand Three Hundred Forty Five"
solve(1000000) → "One Million"
solve(0) → "Zero"Sample tests
Test #1Three digits
Input: [123]
Output: "One Hundred Twenty Three"
Test #2Teens inside thousands
Input: [12345]
Output: "Twelve Thousand Three Hundred Forty Five"
Test #3Empty groups are skipped
Input: [1000000]
Output: "One Million"