generated from mwc/problemset_numberwords
76 lines
2.6 KiB
Markdown
76 lines
2.6 KiB
Markdown
# 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 is where you take over!)
|
|
digit_names2 = [
|
|
"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen",
|
|
"eighteen", "nineteen"
|
|
]
|
|
to get the digit, we devide the number by 10, and consider the remainder.
|
|
return dignt_names2 [number%10]
|
|
## Integers under 100
|
|
If the integer is under 20, use the previous procedure. Otherwise, for the numbers from 20 to 99,
|
|
we do this.
|
|
digit_names3 = [
|
|
"twenty", "thirty" , ....... "eighty", "ninety"
|
|
]
|
|
We take first digit, get the name from the digit_names3.
|
|
Digit 1 = digit_names3 [number//10-2]
|
|
add hypen and add digit names [number%10]if the second digit is not zero. Else, add nothing.
|
|
## Integers under 1000
|
|
Digit 1 = digit_names [number//100]
|
|
add "hundred"
|
|
R1 = number%100
|
|
If R1 is not zero, we add "and", then we call Integers under 100 procedure on R1.
|
|
|
|
## Integers under 1000000
|
|
Q1 = number//1000
|
|
We call Intergers under 1000 procedure on Q1
|
|
We add "thousand"
|
|
R1 = number%1000
|
|
If R1 is not zero, we call Integers under 1000 procedure on R1
|
|
|
|
|
|
## 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?
|
|
Number multiped by negative 1, we treat it as a new number.
|
|
We call the same function as before (now it's positive number)
|
|
We write "negative" in the begining of the answer.
|