problemset_numberwords/planning.md

3.2 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.

Define some prefixes for numbers greater than 9: teen_prefix=[ten, eleven, twelve, thir, four, fif, six, seven, eight, nine] This could go at the top of the code as it will be used later

The number will also need to be broken into 2 parts 11 = 1 and 1 The first one will be tens_digit, the second number will be ones_digit

if number is between 10 and 12 return teen_prefix[tens_digit] if number is between 13 and 19 return teen_prefix[ones_digit] + teen

Integers under 100

This time we'll need prefixes but they are a little different because it is fourty and not forty The first 2 prefixes in the list will not be used but need to be indexed prefixes= [blank, blank, thir, for, fif, six, seven, eight, nine]

The number will again need to be broken into 2 parts 37= 3 and 7 The first one will be first_digit, the second number will be second_digit

if ones_digit = 0 return prefix[tens_digit] + ty if ones_digit not equal to zero return prefix[tens_digit] + digit_name[ones_digit]

Integers under 1000

Similar story, but now 3 digits

234, 2+3+4, hundreds_digit, tens_digit, ones_digit

return digit_names[hundreds_digit] + hundred + run the function above for integers under 100 and it will get added to this string

Integers under 1000000

This will now run very similar to digits under 1000, but there will be thousands_digit, ten_thousands_digit, and hundred_thousands_digit

Ex] 984456

return digit_names[hundred_thousands_digit] + hundred + function for numbers less than 100 + thousand + function for numbers less than 1000. I think it's becoming harder to plan this out wihtout seeing how the earlier code works, but it seems logical to me and I feel like I have a plan for all the numbers.

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?

For a negative digit, you would need an if statement that if -, adds the word netative before the name