Integer to English Words
Real-world version: Spell the Voucher AmountThe statement here is our own: the same inputs, outputs and technique.
A payments desk prints vouchers, and each voucher carries its amount spelled out in words beside the figures. Given a non-negative integer amount, return the words the printer should put on the voucher.
The printer's vocabulary is fixed. Single figures and teens: 1 One, 2 Two, 3 Three, 4 Four, 5 Five, 6 Six, 7 Seven, 8 Eight, 9 Nine, 10 Ten, 11 Eleven, 12 Twelve, 13 Thirteen, 14 Fourteen, 15 Fifteen, 16 Sixteen, 17 Seventeen, 18 Eighteen, 19 Nineteen. Whole tens: 20 Twenty, 30 Thirty, 40 Forty, 50 Fifty, 60 Sixty, 70 Seventy, 80 Eighty, 90 Ninety. It also holds the words Hundred, Thousand, Million, Billion and Zero.
The printer works to these rules.
- An
amountof0prints asZero. For every other amount the wordZerois never printed. - Cut the figures into groups of three digits, counting from the right, so the leftmost group may be short. Read from the left, the groups are tagged
Billion,Million,Thousand, and the rightmost group carries no tag. - A group whose digits are all
0prints nothing whatsoever, tag included. - Any other group prints, in this order: the word for its hundreds digit followed by
Hundredwhen that digit is not0; then the number formed by its last two digits, as one word when that number is1through19, as the whole-tens word plus the units word when the units digit is not0, as the whole-tens word by itself when the units digit is0, and as nothing at all when both digits are0. The group's tag comes last. - Exactly one space separates neighbouring words. No hyphens, no commas, no
and, and no space at either end of the answer.
Examples
Example 1
- Input
- amount = 907
- Output
- "Nine Hundred Seven"
There is one group, 907. Its hundreds digit is 9, which prints Nine Hundred, and its last two digits form 7, which prints Seven.
Example 2
- Input
- amount = 6042519
- Output
- "Six Million Forty Two Thousand Five Hundred Nineteen"
The groups are 6, 042 and 519, tagged Million, Thousand and nothing. They print Six Million, then Forty Two Thousand, then Five Hundred Nineteen.
Example 3
- Input
- amount = 5000018
- Output
- "Five Million Eighteen"
The groups are 5, 000 and 018. The middle group is all zeros, so it prints nothing and its Thousand tag goes with it, leaving Five Million Eighteen.
Constraints
- 0 <= amount <= 2^31 - 1
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def spell_amount(amount: int) -> str:public String spellAmount(int amount)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 20 test cases: 20 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's