Python 연습 17: 숫자를 영어 단어로 변환

의문


  • 0999 사이의 양의 정수를 받아들이고 영어로 작성된 해당 정수의 문자열 표현을 반환하는 함수를 작성하세요.

  • 예시




    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"
    


    내 시도


  • 알고리즘

  • 
      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(위 알고리즘에서 많이 수정됨)

  • 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)
    


    기타 솔루션




    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]))
    


    내 반성


  • 원래 알고리즘을 고수하면 코드가 너무 비슷할 것 같아서 내 솔루션이 내가 코딩한 것과 많이 다른 것으로 나타났습니다. 그러나 나는 내가 코딩하는 것에 대해 매우 만족합니다. 방금 배운 지식을 적용했습니다: 함수 내 함수 정의, 함수 내 함수 호출, 오류 처리. 이것은 좋은 운동입니다.

  • 신용 거래



    챌린지 온edabit

    좋은 웹페이지 즐겨찾기