Integer To English Words
To solve this coding challenge of converting a non-negative integer to its English words representation, we can divide the solution into several manageable parts. Here's a detailed breakdown of the approach:
Explanation
The first step is to handle the edge case where the number is zero. For this, we can simply return "Zero". Next, we need to devise a way to handle numbers by breaking them down and processing them in chunks of thousands (e.g., thousand, million, billion). We need mappings for the following:- Numbers less than twenty.
- Tens multiples.
- Thousands, millions, and billions. For the numbers less than twenty and the tens, we create lists, as these are direct mappings. The "thousands" list helps append the correct thousand, million, or billion label while we iterate through the number. The core of the solution involves a helper function that recursively converts numbers less than a thousand to words. For the main logic, as we process each chunk of the number (from the least significant to the most significant part), we invoke this helper function and append the appropriate thousand label. Here's the pseudocode explaining how to solve it step-by-step:
- Define a function that takes in the number.
- If the number is zero, return "Zero".
- Create lists for:
- Numbers less than 20.
- Tens multiples.
- Thousands (including empty string for units).
- Define a helper function that takes a number less than 1000 and converts it to words:
- If the number is zero, return an empty string.
- If the number is less than 20, use the less than 20 list.
- If the number is less than 100, process the tens and the remainder recursively.
- Otherwise, process the hundreds place and the remainder recursively.
- Initialize an empty result string and a variable for indexing the thousands list.
- While the number is greater than zero:
- Process the last three digits of the number using the helper function.
- Append the appropriate thousand label if the chunk is not zero.
- Move to the next chunk by dividing the number by 1000.
- Increment the thousands index.
- Return the result string after trimming.
Initial Setup
Helper Function
Main Logic
Detailed Steps in Pseudocode:
function numberToWords(num):
# List for numbers less than 20
LESS_THAN_20 = ["", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"]
# List for tens multiples
TENS = ["", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"]
# List for thousands, millions, billions
THOUSANDS = ["", "Thousand", "Million", "Billion"]
# Function to handle numbers less than 1000
function helper(n):
if n == 0:
return ""
elseif n < 20:
return LESS_THAN_20[n] + " "
elseif n < 100:
return TENS[n // 10] + " " + helper(n % 10)
else:
return LESS_THAN_20[n // 100] + " Hundred " + helper(n % 100)
# Edge case for zero
if num == 0:
return "Zero"
# Initialize result string and index for thousands list
result = ""
i = 0
# Process each chunk of the number
while num > 0:
if num % 1000 != 0:
result = helper(num % 1000) + THOUSANDS[i] + " " + result
num = num // 1000
i += 1
# Return the result string after removing any extra spaces
return result.strip()
In summary, the solution involves breaking down the problem into manageable components, processing each chunk of the number iteratively, and using recursion to convert smaller numbers to words. By using lists for different ranges of numbers, we simplify the mapping process and ensure efficient conversion from integer parts to their respective word forms.