Python exercise 17: convert number to english word

Table of Contents

Question

  • Write a function that accepts a positive integer between 0 and 999 inclusive and returns a string representation of that integer written in English.

Example

num_to_eng(0) ➞ "zero"

num_to_eng(18) ➞ "eighteen"

num_to_eng(126) ➞ "one hundred twenty six"

num_to_eng(909) ➞ "nine hundred nine"

My attempt

  • algorithm

  initialise a dictionary with number as key and zero to nine as value
  initialise a dictionary with number as key and ten to ninety as value
 >>determine the value of the number
>>>if number is betwen 0 and 9 inclusive
    return the equivalent word
>>>if number is betwen 10 and 20 inclusive
    return the equivalent word
>>>if number is between 21 and 99 inclusive:
   find the remainder of number divide by 10
>>>>if remainder is zero
    return the equivalent word
>>>>if remainder is not zero
    return the equivalent prefix for the first digit
    return the equivalent word for the second digit
    concatenate them to a string
>>>if number is between 100 to 999 inclusive:
   find the remainder of number divide by 100
>>>>if remainder is zero
    return the equivalent word
   return the equivalent prefix for the first digit
   return the equivalent prefix for the second digit
   return the equivalent word for the third digit
   concatenate them to a string
  • code(has modified a lot from the above algorithm)
def num_to_eng(number: int):  
    def convert_0_to_20(number_0_to_20: int):  
        direct_convert = {0: "zero",  
                          1: "one",  
                          2: "two",  
                          3: "three",  
                          4: "four",  
                          5: "five",  
                          6: 'six',  
                          7: "seven",  
                          8: "eight",  
                          9: "nine",  
                          10: "ten",  
                          11: "eleven",  
                          12: "twelve",  
                          13: "thirteen",  
                          14: "fourteen",  
                          15: 'fifteen',  
                          16: "sixteen",  
                          17: "seventeen",  
                          18: "eighteen",  
                          19: "nineteen",  
                          20: "twenty"}  
        return direct_convert[number_0_to_20]  

    def convert_21_to_99(number_21_to_99: int):  
        two_digits_prefix = {20: "twenty",  
                             30: "thirty",  
                             40: "forty",  
                             50: "fifty",  
                             60: 'sixty',  
                             70: "seventy",  
                             80: "eighty",  
                             90: "ninety"}  
        # find the remainder divided 10  
        remainder = number_21_to_99 % 10  
        if remainder == 0:  
            return two_digits_prefix[number_21_to_99]  
        if remainder != 0:  
            return two_digits_prefix[number_21_to_99 - remainder] + " " + convert_0_to_20(remainder)  

    def convert_100_to_999(number_100_to_999: int):  
        two_digit_part = number_100_to_999 % 100  
        first_digit = number_100_to_999 // 100  
        if two_digit_part == 0:  
            return convert_0_to_20(first_digit) + " hundred "  
        if two_digit_part != 0:  
            # if two_digit_part <20 , there will be error  
            try:  
                return convert_0_to_20(first_digit) + " hundred " + convert_21_to_99(two_digit_part)  
            except:  
                # for two_digit_part <20 , call another function at the end  
                return convert_0_to_20(first_digit) + " hundred " + convert_0_to_20(two_digit_part)  

    if number < 21:  
        return convert_0_to_20(number)  
    if 20 < number < 100:  
        return convert_21_to_99(number)  
    if 99 < number < 1000:  
        return convert_100_to_999(number)

Other solution

def num_to_eng(n):
    if n == 0:
        return 'zero'

    unit = ('','one','two','three','four','five','six','seven','eight','nine')
    tens = ('','','twenty','thirty','forty','fifty','sixty','seventy','eighty','ninety')
    teen = ('ten','eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen')
    h, t, u = '', '', ''

    if n//100:
        h = unit[n//100] + ' hundred'
        n = n%100

    if n >= 20:
        t = tens[n//10]
        n = n%10
    elif n >= 10:
        t = teen[n-10]
        n = 0

    u = unit[n]

    return ' '.join(filter(None,[h,t,u]))

My reflection

  • It turns out that my solution has a lot different from what i coded, as i feel it would be too much similar code if I stick to the original algorithm. However, i am quite happy for what I code, I have applied some knowledge that I just learned: define function inside function, call function inside function, error handling. This is a good exercise.

Credit

challenge on edabit

Leave a Reply