#Skip to menu

Number letter counts

First read the problem description.
special_cases = {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",  15: "fifteen", 18: "eighteen",
                 20: "twenty", 30: "thirty", 40: "forty", 50: "fifty", 60: "sixty", 70: "seventy", 80: "eighty",
                 90: "ninety"}


def number_to_words(n):
    assert n <= 1000
    if special_cases.get(n):
        return special_cases[n]
    if 14 <= n <= 19:
        return special_cases[n % 10] + "teen"
    if 20 <= n <= 99:
        return special_cases[n // 10 * 10] + "-" + special_cases[n % 10]
    if 100 <= n <= 999:
        if n % 100 == 0:
            return special_cases[n // 100] + " " + "hundred"
        return special_cases[n // 100] + " " + "hundred and " + number_to_words(n % 100)
    if n == 1000:
        return "one thousand"


all_numbers = ""
for i in range(1, 1000+1):
    all_numbers += number_to_words(i)
all_numbers = all_numbers.replace('-', '')
all_numbers = all_numbers.replace(' ', '')
len(all_numbers)
21124

Source code of the solution(s):