Question
(*Python Program*) Create the recursive function int2words(value). This function returns the word form of the input integer value. For example, the call int2words(12345) returns twelve
(*Python Program*)
Create the recursive function int2words(value). This function returns the word form of the input integer value. For example, the call int2words(12345) returns "twelve thousand three hundred forty-five."
Assume input values are less than one trillion (that is, up to 999,999,999,999).
The key here is to realize that every group of three digits (from right to left) are translated into words the same way. For example int2words(123123123123) is
123,123,123,123 = | one hundred twenty-three | billion |
one hundred twenty-three | million | |
one hundred twenty-three | thousand | |
one hundred twenty-three |
Let's explore the process of turning the number 123,456,789,123 to words as an example, that is the call to int2words(123456789123).
1,234,567,890 = | one | billion |
two hundred thirty-four | million | |
five hundred sixty-seven | thousand | |
eight hundred ninety |
The idea is to translate into words groups of three digits at a time, and concatenate their corresponding magnitude ("thousand","million", "billion", or nothing if the value correspond to the last three digits of the number). For values less than 100, use a dictionary as in the lab on dictionaries.
In order to "split" a number, use the // (integer division) and % (module operator). For example,
1234567890 // 1000000000 = 1 and 1234567890 % 1000000000 = 234567890
234567890 // 1000000 = 234 and 234567890 % 1000000 = 567890
567890 // 1000 = 567 and 567890 % 1000 = 890
890 // 100 = 8 and 890 % 100 = 90
Write a program that asks the user for a number and displays its corresponding text form.
Think about it?
What is your base case? (Is there more than one base case?)
What is your recursive step? Is there more than one recursive step?
Should we start from ones > tens > hundreds > thousands > .... > billions? Or should we start with the largest magnitude and work our way down (study the picture above carefully).
int2words(1234567890) int2words(1)"billion" int2words(234567890) 1 "one G int2words(234)"million"int2words(567890) 1 int2words(2 +"hundred"int2words(34) int2words(567) "thousand" +int2words(890) two" "thirty-four" int2words(5) "hundred"+int2words(67) int2words(8) | + hundred" + | int2words(90) "five" "sixty-seven "eight" "ninetyStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started