Problem 17
If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.
If the 1-5 written in English, then the number of letters of English words together, we will get 19.
If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?
If all from 1-1000 (including 1000) numbers are written in English words, then how many letters need it ?
NOTE:. The Do not COUNT Spaces or hyphens the For Example, 342 (Three Hundred and Forty-TWO) the contains 23 Letters and 115 (One Hundred and Fifteen)
the contains 20 Letters at The use of "and" the when Writing OUT a Numbers IS in the Compliance with. . British usage
Note: Do not count blank characters and the hyphen, you have to include 'and' word.
def number_to_word(num: int) -> int: determine_thousand = lambda num: int(str(num)[-4]) if len(str(num)) >= 4 else 0 thousand = determine_thousand(num) determine_hundred = lambda num: int(str(num)[-3]) if len(str(num)) >= 3 else 0 hundred = determine_hundred(num) determine_ten = lambda num: int(str(num)[-2]) if len(str(num)) >= 2 else 0 ten = determine_ten(num) one = int(str(num)[-1]) word = 0 if ten == 1: word += ten_to_twenty(int(str(num)[-2:])) else: word += one_digit(one) word += ten_digit(ten) word += hundred_digit(hundred) if hundred: if ten or one: word += 3 # and word += thousand_digit(thousand) return word def one_digit(num: int) -> int: if num == 0: return 0 word = 0 if num in [1, 2, 6]: # one, two, six, ten word = 3 elif num in [3, 7, 8]: # three, seven, eight word = 5 else: # 4, 5, 9 four, five, nine word = 4 return word def ten_digit(num: int) -> int: if num == 0: return 0 word = 0 if num in [2, 3, 8, 9]: # twenty, thirty, eighty, ninety word = 6 elif num in [4, 5, 6]: # forty, fifty, sixty word = 5 elif num == 7: # seventy word = 7 return word def hundred_digit(num: int) -> int: if num == 0: return 0 word = 0 word = one_digit(num) word += 7 # hundred return word def thousand_digit(num: int) -> int: if num == 0: return 0 word = 0 word = one_digit(num) word += 8 # thousand return word def ten_to_twenty(num: int) -> int: if num == 0: return 0 word = 0 if num == 10: # ten word = 3 elif num in [11, 12]: # eleven, twelve word = 6 elif num in [13, 14, 18, 19]: # thirteen, fourteen, eighteen, nineteen word = 8 elif num in [15, 16]: # fifteen, sixteen word = 7 elif num == 17: # seventeen word = 9 return word if __name__ == '__main__': tot = 0 for i in range(1001): word = number_to_word(i) print(i, word) tot += word print(tot)