Problem 17

Problem: 

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 all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?

NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of “and” when writing out numbers is in compliance with British usage.


My Solution:


## took me 20 minutes to figure out forty wasn't spelled fourty
d = dict()
d[0]=''
d[1]='one'
d[2]='two'
d[3]='three'
d[4]='four'
d[5]='five'
d[6]='six'
d[7]='seven'
d[8]='eight'
d[9]='nine'
d[10]='ten'
d[11]='eleven'
d[12]='twelve'
d[13]='thirteen'
d[14]='fourteen'
d[15]='fifteen'
d[16]='sixteen'
d[17]='seventeen'
d[18]='eighteen'
d[19]='nineteen'
d[1000]='onethousand'

for n in range(20,100):
    if len(str(n))==2:
        if str(n)[0]=='2':
            d[n]='twenty'+d[int(str(n)[1])]
        if str(n)[0]=='3':
            d[n]='thirty'+d[int(str(n)[1])]
        if str(n)[0]=='4':
            d[n]='forty'+d[int(str(n)[1])]
        if str(n)[0]=='5':
            d[n]='fifty'+d[int(str(n)[1])]
        if str(n)[0]=='6':
            d[n]='sixty'+d[int(str(n)[1])]
        if str(n)[0]=='7':
            d[n]='seventy'+d[int(str(n)[1])]
        if str(n)[0]=='8':
            d[n]='eighty'+d[int(str(n)[1])]
        if str(n)[0]=='9':
            d[n]='ninety'+d[int(str(n)[1])]

for n in range(100,1000):
    if int(str(n)[1:])==0:
        d[n] = d[int(str(n)[0])]+'hundred'
    else:
        d[n]=d[int(str(n)[0])]+'hundredand'+d[int(str(n)[1:])] ## int turns e.g. 04 to 4

s=0
for v in d.values():
    s += len(v)
    #print(v)
print(s)