Python 연습 17: 숫자를 영어 단어로 변환
의문
0
와 999
사이의 양의 정수를 받아들이고 영어로 작성된 해당 정수의 문자열 표현을 반환하는 함수를 작성하세요. 예시
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
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
Reference
이 문제에 관하여(Python 연습 17: 숫자를 영어 단어로 변환), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/mathewchan/python-exercise-17-convert-number-to-english-word-55o3텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)