2.8 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, you still need to know the name of the integer since it has a unique name. Create a list of integers called teens_names and index from 0 to 9. Then you can look up the integer's name.
The list would be:
teens_names = ["ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]
return teens_names[number]
Integers under 100
If the integer is less than 20, then use either of the procedures described above.
Otherwise:
- Look at the first digit to know what "tens" name it's called.
- Then, if the second digit is not 0, access the digit_names list to create the secont part of the word.
Integers under 1000
If the integer is less than 100, then use one of the procedures above.
Otherwise:
- Look at the first digit and use the digit_names list for the first part of the word
- Add the word "hundred"
- Use the procedures for integers under 100 to come up with the rest of the word.
Integers under 1000000
If the integer is less than 1000, use one of the procedures above.
Otherwise:
- Look at the first three digits. Use the procedure for integers under 1000 for the first part of the name.
- Add the word thousand
- use the procedure for integers under 1000 to create the last part of the name.
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?
- Use the procedures above, but concatenate them to the word "negative"