Question
1. Converting Number Words into Integers (PYTHON) Write a program that convert sequences of numbers written out as words (e.g., 'five million three hundred forty
1. Converting Number Words into Integers (PYTHON)
Write a program that convert sequences of numbers written out as words (e.g., 'five million three hundred forty five thousand seven hundred three') into numbers (e.g., 5345703). We have gone over the algorithm in class or will do so shortly. However, it is also repeated here. Please note that your program does not have to handle commas or other words like "and" (although, you can deal with these by simply deleting them if you want). Your goal is to handle the following cases (see the global variable sample_arabic_number_strings in the number_program_input.py file):
Five hundred million two hundred three thousand seventeen
One billion seventy three
One hundred ninety two thousand seven hundred thirty one
We will divide this program up into the following parts. It is suggested that you make a new output list for each part and derive that output list from the output of the previous stage, i.e., input --> output1 --> output2 --> output3 --> ... --> final_output. There are other ways to do this, but this method is easier to debug because it is easy to look at the output of each stage. Note: it is not be necessary for your program to work for numbers of one trillion or greater.
Make a list of numbers
Download the file: number_program_input.py.
def word_to_number(word): if word == 'one': return(1) elif word == 'two': return(2) elif word == 'three': return(3) elif word == 'four': return(4) elif word == 'five': return(5) elif word == 'six': return(6) elif word == 'seven': return(7) elif word == 'eight': return(8) elif word == 'nine': return(9) elif word == 'ten': return(10) elif word == 'eleven': return(11) elif word == 'twelve': return(12) elif word == 'thirteen': return(13) elif word == 'fourteen': return(14) elif word == 'fifteen': return(15) elif word == 'sixteen': return(16) elif word == 'seventeen': return(17) elif word == 'eighteen': return(18) elif word == 'nineteen': return(19) elif word == 'twenty': return(20) elif word == 'thirty': return(30) elif word == 'forty': return(40) elif word == 'fifty': return(50) elif word == 'sixty': return(60) elif word == 'seventy': return(70) elif word == 'eighty': return(80) elif word == 'ninety': return(90) elif word == 'hundred': return(100) elif word == 'thousand': return(1000) elif word == 'million': return(1000000) elif word == 'billion': return(1000000000)
This contains both some simple functions and variables to use for both the regular assignment and the extra credit assignment Split the input by spaces (using the "split" method), creating a list of words, e.g., 'Five hundred million two hundred three thousand seventeen'.split(' ') -> ['Five', 'hundred', 'million', 'two', 'hundred', 'three', 'thousand', 'seventeen']
Use the word_to_number function in number_program_input.py to create a list of integers from the list of words.
Initialize a new list as the empty list []
For each word in the list of words, derive one integer and append it to the new list
Remember to be careful about the difference between upper and lower case because word_to_number will return None, instead of an integer if its input is upper case.
Example: ['Five', 'hundred', 'million', 'two', 'hundred', 'three', 'thousand', 'seventeen'] -> [5, 100, 1000000, 2, 100, 3, 1000 17]
Combine numbers less than 1000: Combine the numbers in the list from the previous step together in the following 2 passes. The output from the first pass will be the input to the second pass. The output (output2) to the second pass will be the input to later stages. In the end, you will add up the numbers in the last output list and return the answer. See examples from the slides 29 to 32 in the class lectures slides about sequences (part 1). One of the walk-throughs combines the steps 1 and 2 below. The advantage of keeping them separate is that it is easier to adapt your system for the extra credit problem.
Go through the list created in the previous step in a loop. Build a new output list such that:
items that are greater than 999 (thousand, million, etc.) are unchanged from the input list
go through numbers between items that are larger than 999 and combined them together such that
if lower_number precedes higher_number, replace the combination by lower_number * higher_number, e.g., [5, 100] is replaced by 500 if higher_number precedes lower_number, put both in the output unchanged.
Example: [5, 100, 1000000, 2, 100, 3, 1000 17] should be converted to: [500, 1000000, 200, 3, 1000, 17] In the example, the subsequences [5, 100] and [2, 100] both meet the condition that the first item is less than the second.
One way to implement this is to use a variable to temporarily store numbers that are less than 1000. In this description, I will call this variable hold:
At each iteration in a for loop, your program does some checks about current_number (the next number in the loop) and the value of hold:
if current_number is greater than 999, then:
append the non-zero value of hold to output
and append the current_number to output (right after hold)
and set hold to 0
else if hold equals zero
set hold to current_number
else if current_number is greater than hold
set hold to the product of hold and current_number
else
append the value of hold to output
and append current_number to output
and set hold to 0
When the loop is complete, if hold is non-zero, append it to output
For example, your program may need to "see" what comes after a 5, before appending it to the next output list. If 100 follows the 5, 500 should be appended to output, replacing both 5 and 100 in the original sequence. The proposed strategy is to hold on to the 5 until checking what the next number is.
Go through the output of the previous step to produce a new output list:
items that are greater than 999 (thousand, million, etc.) are unchanged
go through numbers between those items that are larger than 999 and add them together, storing the sum
For example, [200, 3] is replaced by 203 in the next output list (use hold similarly as in the previous step)
Example: [500, 1000000, 200, 3, 1000, 17] should be converted to [500, 1000000, 203, 1000, 17]
This step is similar to the previous one, except: (a) this time you are looking to add numbers together rather than multiplying; (b) you are looking for pairs of numbers such that the first is greater than the second, rather than less than.
Combine numbers greater than 999 with numbers 999 or less. Go through the new list twice and perform the following operations. This list consists of instances of 1000, 1000000, etc. plus integers between 1 and 999.
On the first pass, multiply the terms from 1-999 with following terms greater than 999, e.g., [430, 1000000, 567, 1000, 35] --> [430000000, 567000, 35]
On the final pass, add the remaining terms together to get a new sum [430000000, 567000, 35] --> 430567035 . Return the answer (verify with the above examples that your program works correctly) and debug as necessary.
Other notes
Note that the 1st 2 steps of part 2 can be combined into one step. However, then it would be more difficult to generalize the algorithm to the Chinese case in the extra credit.
Note that there are other ways of doing this task, but all of them involve looping through the numbers several times and comparing two consecutive positions. The program described above uses the variable hold to look back at previous items while cycling through a loop. hold keeps track of intermediate calculations, while output records parts of the problem which have been completed. There is an alternative strategy in which you look forward -- for each item in the list, you look forward at the next item before deciding what to do with it. If you choose to do this strategy instead, you will have to be careful about not looking off the end of the list (past the last item) because it will lead to the system crashing ("index out of range" errors). Other aspects of this method will also be different.
Step 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