4.5 KiB
Planning Number Words
Before you start programming, do some planning here on how you will break down this problem. Here's a hint: if you start by writing functions for smaller numbers, you will find that these functions help you with the larger numbers. For each of the cases below, explain how you would turn a number into a string. Feel free to write in sentences or in pseudocode (pseudocode is a sort of "casual programming" where you're almost writing in code, being pretty specific without worrying about syntax. For each case below, assume the integer is zero or more--don't worry about negative integers.
Integers under 10
(This one is done for you!) For an integer less than ten, you need to know the name of each digit, and look it up. You could use a big if/else statement like:
if number == 0:
return "zero"
elif number == 1:
return "one"
elif number == 1:
return "two"
A cleaner way to do this would be to make a list of digit names, from zero to nine. Then you could just look up a digit's name:
digit_names = [
"zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"
]
return digit_names[number]
Integers under 20
If the integer is under 10, then use the procedure described above. Otherwise, this could be broken into two steps. One would be to recognize the digits that the program is already familiar with. This will seperate the number in the second position of the two digit number. for example the 3 in 13 is still a 3. However they are called a different term, so they need a new way to be recognized as "teen" numbers.
This would be a similar but seperate look up list of numbers. teen_names = [ "ten", "eleven", "twelve", thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" ] return teen_names[number]
Integers under 100
If the integer is under 99 it would need to be broken into another definition since it would use the digit function above. It would also need to know the teen function in order to distinguish those numbers from the 20-99 set. First we need to identify if there are any other lists or patterns that can be created. We cold do another list and define it as "tens."
tens_names = [ "ten", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety" ] return tens_names[number]
There would also need to be code that administered hyphenated numbers that would combine two of these functions. For example twenty-five. We would need twenty and five.
Integers under 1000
By this step digits 1-9 are covered. the teens are defined, and tens are as well. The same would have to be done for 100s.
Once the 100s are defined then it is a matter of combinig the existing elements using the correct verbage. the program would also have to account for supplemental words that are needed to complete the correct numbers like "and", commas, decimals and hyphens.
It may be reasonable to group some of these into patterns. For example the 100s all are going to end in two zeros, similarly to the thousands that will all end in three zeros. This should be true, unless they are accompanied by a comma. They might be able to be groups into a combination of lists and if/then statements.
Integers under 1000000
Again this would be three similar steps. 1-99 is defined. As are the tens, which would account for two digit numbers like thirty-two. The 100s and their syntax would be completed in the previous sequence. Now, what is left is to break down another set of numbers by thousands. This should work considering that hundereds of thousands would be converted using the 100s function, and further specifications would use the tens, teens, and digits functions.
Again these might be groups becausethey fall into a patter and always have three zeros in their numerical form. However, this might be tough when it comes to hundered of thousands. that should be all of the pieces that you would need to create numbers up to 1,000,000.
digits, teens, tens, hunderds, thousands.
Negative integers down to -1 million
We won't deal with negative integers in this problem set, but how would you deal with a negative integer, using the functions above?
I would assum that the process of negative numbers would be essentially the same. One would have to code the recognition of the "-" symbol in terms of writing it out, but the overall process should be the same. Starting with negative digits, then teens, then tens, then hunderds, the thousands. I think that the real difference would occur when trying to add math code into these numbers.