Project Euler: Problem 17

https://projecteuler.net/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 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.

First write a number_to_words(n) function that takes an integer n between 1 and 1000 inclusive and returns a list of words for the number as described above


In [77]:
def number_to_words(n):
    """Given a number n between 1-1000 inclusive return a list of words for the number."""
    wrds = []
    ones = {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'}
    tens = {2:'twenty',3:'thirty',4:'forty',5:'fifty',6:'sixty',7:'seventy',8:'eighty',
            9:'ninety'}
    hundred = {1:'onehundred',2:'twohundred',3:'threehundred',4:'fourhundred',
               5:'fivehundred',6:'sixhundred',7:'sevenhundred',8:'eighthundred',
               9:'ninehundred'}
    
    if n<20:
        x=1
        while x<=n:
            wrds.append(ones[x])
            x+=1   
    elif n<100:
        x=1
        while x<20:
            wrds.append(ones[x])
            x+=1
        t=2
        while t*10 <= n:    
            wrds.append(tens[t]) 
            a=1
            while x<n-t+2 and a<10:
                wrds.append(tens[t] +ones[a])
                a+=1
                x+=1
            t+=1    
    elif n<1000:
        x=1
        while x<20:
            wrds.append(ones[x])
            x+=1
        t=2
        while t*10 <= 99:    
            wrds.append(tens[t]) 
            a=1
            while x<99-t+2 and a<10:
                wrds.append(tens[t] +ones[a])
                a+=1
                x+=1
            t+=1
        h=1    
        while  h*100<=n:
            wrds.append(hundred[h])
            x=(h*100)+1
            while x<=n and x<(h*100)+20:
                b=x-(h*100)
                wrds.append(hundred[h]+'and'+ones[b])
                x+=1
            t=2
            while ((h*100)+(t*10)) <= n and t<10:    
                wrds.append(hundred[h]+'and'+tens[t]) 
                a=1
                while x<n-t+2 and a<10:
                    wrds.append(hundred[h]+'and'+tens[t]+ones[a])
                    a+=1
                    x+=1
                t+=1
            h+=1
    elif n==1000:
        n=999
        x=1
        while x<20:
            wrds.append(ones[x])
            x+=1
        t=2
        while t*10 <= 99:    
            wrds.append(tens[t]) 
            a=1
            while x<99-t+2 and a<10:
                wrds.append(tens[t] +ones[a])
                a+=1
                x+=1
            t+=1
        h=1    
        while  h*100<=n:
            wrds.append(hundred[h])
            x=(h*100)+1
            while x<=n and x<(h*100)+20:
                b=x-(h*100)
                wrds.append(hundred[h]+'and'+ones[b])
                x+=1
            t=2
            while ((h*100)+(t*10)) <= n and t<10:    
                wrds.append(hundred[h]+'and'+tens[t]) 
                a=1
                while x<n-t+2 and a<10:
                    wrds.append(hundred[h]+'and'+tens[t]+ones[a])
                    a+=1
                    x+=1
                t+=1
            h+=1
        wrds.append('onethousand')
        
        
    return wrds

In [78]:
number_to_words(1000)


Out[78]:
['one',
 'two',
 'three',
 'four',
 'five',
 'six',
 'seven',
 'eight',
 'nine',
 'ten',
 'eleven',
 'twelve',
 'thirteen',
 'fourteen',
 'fifteen',
 'sixteen',
 'seventeen',
 'eighteen',
 'nineteen',
 'twenty',
 'twentyone',
 'twentytwo',
 'twentythree',
 'twentyfour',
 'twentyfive',
 'twentysix',
 'twentyseven',
 'twentyeight',
 'twentynine',
 'thirty',
 'thirtyone',
 'thirtytwo',
 'thirtythree',
 'thirtyfour',
 'thirtyfive',
 'thirtysix',
 'thirtyseven',
 'thirtyeight',
 'thirtynine',
 'forty',
 'fortyone',
 'fortytwo',
 'fortythree',
 'fortyfour',
 'fortyfive',
 'fortysix',
 'fortyseven',
 'fortyeight',
 'fortynine',
 'fifty',
 'fiftyone',
 'fiftytwo',
 'fiftythree',
 'fiftyfour',
 'fiftyfive',
 'fiftysix',
 'fiftyseven',
 'fiftyeight',
 'fiftynine',
 'sixty',
 'sixtyone',
 'sixtytwo',
 'sixtythree',
 'sixtyfour',
 'sixtyfive',
 'sixtysix',
 'sixtyseven',
 'sixtyeight',
 'sixtynine',
 'seventy',
 'seventyone',
 'seventytwo',
 'seventythree',
 'seventyfour',
 'seventyfive',
 'seventysix',
 'seventyseven',
 'seventyeight',
 'seventynine',
 'eighty',
 'eightyone',
 'eightytwo',
 'eightythree',
 'eightyfour',
 'eightyfive',
 'eightysix',
 'eightyseven',
 'eightyeight',
 'eightynine',
 'ninety',
 'ninetyone',
 'ninetytwo',
 'ninetythree',
 'ninetyfour',
 'ninetyfive',
 'ninetysix',
 'ninetyseven',
 'ninetyeight',
 'ninetynine',
 'onehundred',
 'onehundredandone',
 'onehundredandtwo',
 'onehundredandthree',
 'onehundredandfour',
 'onehundredandfive',
 'onehundredandsix',
 'onehundredandseven',
 'onehundredandeight',
 'onehundredandnine',
 'onehundredandten',
 'onehundredandeleven',
 'onehundredandtwelve',
 'onehundredandthirteen',
 'onehundredandfourteen',
 'onehundredandfifteen',
 'onehundredandsixteen',
 'onehundredandseventeen',
 'onehundredandeighteen',
 'onehundredandnineteen',
 'onehundredandtwenty',
 'onehundredandtwentyone',
 'onehundredandtwentytwo',
 'onehundredandtwentythree',
 'onehundredandtwentyfour',
 'onehundredandtwentyfive',
 'onehundredandtwentysix',
 'onehundredandtwentyseven',
 'onehundredandtwentyeight',
 'onehundredandtwentynine',
 'onehundredandthirty',
 'onehundredandthirtyone',
 'onehundredandthirtytwo',
 'onehundredandthirtythree',
 'onehundredandthirtyfour',
 'onehundredandthirtyfive',
 'onehundredandthirtysix',
 'onehundredandthirtyseven',
 'onehundredandthirtyeight',
 'onehundredandthirtynine',
 'onehundredandforty',
 'onehundredandfortyone',
 'onehundredandfortytwo',
 'onehundredandfortythree',
 'onehundredandfortyfour',
 'onehundredandfortyfive',
 'onehundredandfortysix',
 'onehundredandfortyseven',
 'onehundredandfortyeight',
 'onehundredandfortynine',
 'onehundredandfifty',
 'onehundredandfiftyone',
 'onehundredandfiftytwo',
 'onehundredandfiftythree',
 'onehundredandfiftyfour',
 'onehundredandfiftyfive',
 'onehundredandfiftysix',
 'onehundredandfiftyseven',
 'onehundredandfiftyeight',
 'onehundredandfiftynine',
 'onehundredandsixty',
 'onehundredandsixtyone',
 'onehundredandsixtytwo',
 'onehundredandsixtythree',
 'onehundredandsixtyfour',
 'onehundredandsixtyfive',
 'onehundredandsixtysix',
 'onehundredandsixtyseven',
 'onehundredandsixtyeight',
 'onehundredandsixtynine',
 'onehundredandseventy',
 'onehundredandseventyone',
 'onehundredandseventytwo',
 'onehundredandseventythree',
 'onehundredandseventyfour',
 'onehundredandseventyfive',
 'onehundredandseventysix',
 'onehundredandseventyseven',
 'onehundredandseventyeight',
 'onehundredandseventynine',
 'onehundredandeighty',
 'onehundredandeightyone',
 'onehundredandeightytwo',
 'onehundredandeightythree',
 'onehundredandeightyfour',
 'onehundredandeightyfive',
 'onehundredandeightysix',
 'onehundredandeightyseven',
 'onehundredandeightyeight',
 'onehundredandeightynine',
 'onehundredandninety',
 'onehundredandninetyone',
 'onehundredandninetytwo',
 'onehundredandninetythree',
 'onehundredandninetyfour',
 'onehundredandninetyfive',
 'onehundredandninetysix',
 'onehundredandninetyseven',
 'onehundredandninetyeight',
 'onehundredandninetynine',
 'twohundred',
 'twohundredandone',
 'twohundredandtwo',
 'twohundredandthree',
 'twohundredandfour',
 'twohundredandfive',
 'twohundredandsix',
 'twohundredandseven',
 'twohundredandeight',
 'twohundredandnine',
 'twohundredandten',
 'twohundredandeleven',
 'twohundredandtwelve',
 'twohundredandthirteen',
 'twohundredandfourteen',
 'twohundredandfifteen',
 'twohundredandsixteen',
 'twohundredandseventeen',
 'twohundredandeighteen',
 'twohundredandnineteen',
 'twohundredandtwenty',
 'twohundredandtwentyone',
 'twohundredandtwentytwo',
 'twohundredandtwentythree',
 'twohundredandtwentyfour',
 'twohundredandtwentyfive',
 'twohundredandtwentysix',
 'twohundredandtwentyseven',
 'twohundredandtwentyeight',
 'twohundredandtwentynine',
 'twohundredandthirty',
 'twohundredandthirtyone',
 'twohundredandthirtytwo',
 'twohundredandthirtythree',
 'twohundredandthirtyfour',
 'twohundredandthirtyfive',
 'twohundredandthirtysix',
 'twohundredandthirtyseven',
 'twohundredandthirtyeight',
 'twohundredandthirtynine',
 'twohundredandforty',
 'twohundredandfortyone',
 'twohundredandfortytwo',
 'twohundredandfortythree',
 'twohundredandfortyfour',
 'twohundredandfortyfive',
 'twohundredandfortysix',
 'twohundredandfortyseven',
 'twohundredandfortyeight',
 'twohundredandfortynine',
 'twohundredandfifty',
 'twohundredandfiftyone',
 'twohundredandfiftytwo',
 'twohundredandfiftythree',
 'twohundredandfiftyfour',
 'twohundredandfiftyfive',
 'twohundredandfiftysix',
 'twohundredandfiftyseven',
 'twohundredandfiftyeight',
 'twohundredandfiftynine',
 'twohundredandsixty',
 'twohundredandsixtyone',
 'twohundredandsixtytwo',
 'twohundredandsixtythree',
 'twohundredandsixtyfour',
 'twohundredandsixtyfive',
 'twohundredandsixtysix',
 'twohundredandsixtyseven',
 'twohundredandsixtyeight',
 'twohundredandsixtynine',
 'twohundredandseventy',
 'twohundredandseventyone',
 'twohundredandseventytwo',
 'twohundredandseventythree',
 'twohundredandseventyfour',
 'twohundredandseventyfive',
 'twohundredandseventysix',
 'twohundredandseventyseven',
 'twohundredandseventyeight',
 'twohundredandseventynine',
 'twohundredandeighty',
 'twohundredandeightyone',
 'twohundredandeightytwo',
 'twohundredandeightythree',
 'twohundredandeightyfour',
 'twohundredandeightyfive',
 'twohundredandeightysix',
 'twohundredandeightyseven',
 'twohundredandeightyeight',
 'twohundredandeightynine',
 'twohundredandninety',
 'twohundredandninetyone',
 'twohundredandninetytwo',
 'twohundredandninetythree',
 'twohundredandninetyfour',
 'twohundredandninetyfive',
 'twohundredandninetysix',
 'twohundredandninetyseven',
 'twohundredandninetyeight',
 'twohundredandninetynine',
 'threehundred',
 'threehundredandone',
 'threehundredandtwo',
 'threehundredandthree',
 'threehundredandfour',
 'threehundredandfive',
 'threehundredandsix',
 'threehundredandseven',
 'threehundredandeight',
 'threehundredandnine',
 'threehundredandten',
 'threehundredandeleven',
 'threehundredandtwelve',
 'threehundredandthirteen',
 'threehundredandfourteen',
 'threehundredandfifteen',
 'threehundredandsixteen',
 'threehundredandseventeen',
 'threehundredandeighteen',
 'threehundredandnineteen',
 'threehundredandtwenty',
 'threehundredandtwentyone',
 'threehundredandtwentytwo',
 'threehundredandtwentythree',
 'threehundredandtwentyfour',
 'threehundredandtwentyfive',
 'threehundredandtwentysix',
 'threehundredandtwentyseven',
 'threehundredandtwentyeight',
 'threehundredandtwentynine',
 'threehundredandthirty',
 'threehundredandthirtyone',
 'threehundredandthirtytwo',
 'threehundredandthirtythree',
 'threehundredandthirtyfour',
 'threehundredandthirtyfive',
 'threehundredandthirtysix',
 'threehundredandthirtyseven',
 'threehundredandthirtyeight',
 'threehundredandthirtynine',
 'threehundredandforty',
 'threehundredandfortyone',
 'threehundredandfortytwo',
 'threehundredandfortythree',
 'threehundredandfortyfour',
 'threehundredandfortyfive',
 'threehundredandfortysix',
 'threehundredandfortyseven',
 'threehundredandfortyeight',
 'threehundredandfortynine',
 'threehundredandfifty',
 'threehundredandfiftyone',
 'threehundredandfiftytwo',
 'threehundredandfiftythree',
 'threehundredandfiftyfour',
 'threehundredandfiftyfive',
 'threehundredandfiftysix',
 'threehundredandfiftyseven',
 'threehundredandfiftyeight',
 'threehundredandfiftynine',
 'threehundredandsixty',
 'threehundredandsixtyone',
 'threehundredandsixtytwo',
 'threehundredandsixtythree',
 'threehundredandsixtyfour',
 'threehundredandsixtyfive',
 'threehundredandsixtysix',
 'threehundredandsixtyseven',
 'threehundredandsixtyeight',
 'threehundredandsixtynine',
 'threehundredandseventy',
 'threehundredandseventyone',
 'threehundredandseventytwo',
 'threehundredandseventythree',
 'threehundredandseventyfour',
 'threehundredandseventyfive',
 'threehundredandseventysix',
 'threehundredandseventyseven',
 'threehundredandseventyeight',
 'threehundredandseventynine',
 'threehundredandeighty',
 'threehundredandeightyone',
 'threehundredandeightytwo',
 'threehundredandeightythree',
 'threehundredandeightyfour',
 'threehundredandeightyfive',
 'threehundredandeightysix',
 'threehundredandeightyseven',
 'threehundredandeightyeight',
 'threehundredandeightynine',
 'threehundredandninety',
 'threehundredandninetyone',
 'threehundredandninetytwo',
 'threehundredandninetythree',
 'threehundredandninetyfour',
 'threehundredandninetyfive',
 'threehundredandninetysix',
 'threehundredandninetyseven',
 'threehundredandninetyeight',
 'threehundredandninetynine',
 'fourhundred',
 'fourhundredandone',
 'fourhundredandtwo',
 'fourhundredandthree',
 'fourhundredandfour',
 'fourhundredandfive',
 'fourhundredandsix',
 'fourhundredandseven',
 'fourhundredandeight',
 'fourhundredandnine',
 'fourhundredandten',
 'fourhundredandeleven',
 'fourhundredandtwelve',
 'fourhundredandthirteen',
 'fourhundredandfourteen',
 'fourhundredandfifteen',
 'fourhundredandsixteen',
 'fourhundredandseventeen',
 'fourhundredandeighteen',
 'fourhundredandnineteen',
 'fourhundredandtwenty',
 'fourhundredandtwentyone',
 'fourhundredandtwentytwo',
 'fourhundredandtwentythree',
 'fourhundredandtwentyfour',
 'fourhundredandtwentyfive',
 'fourhundredandtwentysix',
 'fourhundredandtwentyseven',
 'fourhundredandtwentyeight',
 'fourhundredandtwentynine',
 'fourhundredandthirty',
 'fourhundredandthirtyone',
 'fourhundredandthirtytwo',
 'fourhundredandthirtythree',
 'fourhundredandthirtyfour',
 'fourhundredandthirtyfive',
 'fourhundredandthirtysix',
 'fourhundredandthirtyseven',
 'fourhundredandthirtyeight',
 'fourhundredandthirtynine',
 'fourhundredandforty',
 'fourhundredandfortyone',
 'fourhundredandfortytwo',
 'fourhundredandfortythree',
 'fourhundredandfortyfour',
 'fourhundredandfortyfive',
 'fourhundredandfortysix',
 'fourhundredandfortyseven',
 'fourhundredandfortyeight',
 'fourhundredandfortynine',
 'fourhundredandfifty',
 'fourhundredandfiftyone',
 'fourhundredandfiftytwo',
 'fourhundredandfiftythree',
 'fourhundredandfiftyfour',
 'fourhundredandfiftyfive',
 'fourhundredandfiftysix',
 'fourhundredandfiftyseven',
 'fourhundredandfiftyeight',
 'fourhundredandfiftynine',
 'fourhundredandsixty',
 'fourhundredandsixtyone',
 'fourhundredandsixtytwo',
 'fourhundredandsixtythree',
 'fourhundredandsixtyfour',
 'fourhundredandsixtyfive',
 'fourhundredandsixtysix',
 'fourhundredandsixtyseven',
 'fourhundredandsixtyeight',
 'fourhundredandsixtynine',
 'fourhundredandseventy',
 'fourhundredandseventyone',
 'fourhundredandseventytwo',
 'fourhundredandseventythree',
 'fourhundredandseventyfour',
 'fourhundredandseventyfive',
 'fourhundredandseventysix',
 'fourhundredandseventyseven',
 'fourhundredandseventyeight',
 'fourhundredandseventynine',
 'fourhundredandeighty',
 'fourhundredandeightyone',
 'fourhundredandeightytwo',
 'fourhundredandeightythree',
 'fourhundredandeightyfour',
 'fourhundredandeightyfive',
 'fourhundredandeightysix',
 'fourhundredandeightyseven',
 'fourhundredandeightyeight',
 'fourhundredandeightynine',
 'fourhundredandninety',
 'fourhundredandninetyone',
 'fourhundredandninetytwo',
 'fourhundredandninetythree',
 'fourhundredandninetyfour',
 'fourhundredandninetyfive',
 'fourhundredandninetysix',
 'fourhundredandninetyseven',
 'fourhundredandninetyeight',
 'fourhundredandninetynine',
 'fivehundred',
 'fivehundredandone',
 'fivehundredandtwo',
 'fivehundredandthree',
 'fivehundredandfour',
 'fivehundredandfive',
 'fivehundredandsix',
 'fivehundredandseven',
 'fivehundredandeight',
 'fivehundredandnine',
 'fivehundredandten',
 'fivehundredandeleven',
 'fivehundredandtwelve',
 'fivehundredandthirteen',
 'fivehundredandfourteen',
 'fivehundredandfifteen',
 'fivehundredandsixteen',
 'fivehundredandseventeen',
 'fivehundredandeighteen',
 'fivehundredandnineteen',
 'fivehundredandtwenty',
 'fivehundredandtwentyone',
 'fivehundredandtwentytwo',
 'fivehundredandtwentythree',
 'fivehundredandtwentyfour',
 'fivehundredandtwentyfive',
 'fivehundredandtwentysix',
 'fivehundredandtwentyseven',
 'fivehundredandtwentyeight',
 'fivehundredandtwentynine',
 'fivehundredandthirty',
 'fivehundredandthirtyone',
 'fivehundredandthirtytwo',
 'fivehundredandthirtythree',
 'fivehundredandthirtyfour',
 'fivehundredandthirtyfive',
 'fivehundredandthirtysix',
 'fivehundredandthirtyseven',
 'fivehundredandthirtyeight',
 'fivehundredandthirtynine',
 'fivehundredandforty',
 'fivehundredandfortyone',
 'fivehundredandfortytwo',
 'fivehundredandfortythree',
 'fivehundredandfortyfour',
 'fivehundredandfortyfive',
 'fivehundredandfortysix',
 'fivehundredandfortyseven',
 'fivehundredandfortyeight',
 'fivehundredandfortynine',
 'fivehundredandfifty',
 'fivehundredandfiftyone',
 'fivehundredandfiftytwo',
 'fivehundredandfiftythree',
 'fivehundredandfiftyfour',
 'fivehundredandfiftyfive',
 'fivehundredandfiftysix',
 'fivehundredandfiftyseven',
 'fivehundredandfiftyeight',
 'fivehundredandfiftynine',
 'fivehundredandsixty',
 'fivehundredandsixtyone',
 'fivehundredandsixtytwo',
 'fivehundredandsixtythree',
 'fivehundredandsixtyfour',
 'fivehundredandsixtyfive',
 'fivehundredandsixtysix',
 'fivehundredandsixtyseven',
 'fivehundredandsixtyeight',
 'fivehundredandsixtynine',
 'fivehundredandseventy',
 'fivehundredandseventyone',
 'fivehundredandseventytwo',
 'fivehundredandseventythree',
 'fivehundredandseventyfour',
 'fivehundredandseventyfive',
 'fivehundredandseventysix',
 'fivehundredandseventyseven',
 'fivehundredandseventyeight',
 'fivehundredandseventynine',
 'fivehundredandeighty',
 'fivehundredandeightyone',
 'fivehundredandeightytwo',
 'fivehundredandeightythree',
 'fivehundredandeightyfour',
 'fivehundredandeightyfive',
 'fivehundredandeightysix',
 'fivehundredandeightyseven',
 'fivehundredandeightyeight',
 'fivehundredandeightynine',
 'fivehundredandninety',
 'fivehundredandninetyone',
 'fivehundredandninetytwo',
 'fivehundredandninetythree',
 'fivehundredandninetyfour',
 'fivehundredandninetyfive',
 'fivehundredandninetysix',
 'fivehundredandninetyseven',
 'fivehundredandninetyeight',
 'fivehundredandninetynine',
 'sixhundred',
 'sixhundredandone',
 'sixhundredandtwo',
 'sixhundredandthree',
 'sixhundredandfour',
 'sixhundredandfive',
 'sixhundredandsix',
 'sixhundredandseven',
 'sixhundredandeight',
 'sixhundredandnine',
 'sixhundredandten',
 'sixhundredandeleven',
 'sixhundredandtwelve',
 'sixhundredandthirteen',
 'sixhundredandfourteen',
 'sixhundredandfifteen',
 'sixhundredandsixteen',
 'sixhundredandseventeen',
 'sixhundredandeighteen',
 'sixhundredandnineteen',
 'sixhundredandtwenty',
 'sixhundredandtwentyone',
 'sixhundredandtwentytwo',
 'sixhundredandtwentythree',
 'sixhundredandtwentyfour',
 'sixhundredandtwentyfive',
 'sixhundredandtwentysix',
 'sixhundredandtwentyseven',
 'sixhundredandtwentyeight',
 'sixhundredandtwentynine',
 'sixhundredandthirty',
 'sixhundredandthirtyone',
 'sixhundredandthirtytwo',
 'sixhundredandthirtythree',
 'sixhundredandthirtyfour',
 'sixhundredandthirtyfive',
 'sixhundredandthirtysix',
 'sixhundredandthirtyseven',
 'sixhundredandthirtyeight',
 'sixhundredandthirtynine',
 'sixhundredandforty',
 'sixhundredandfortyone',
 'sixhundredandfortytwo',
 'sixhundredandfortythree',
 'sixhundredandfortyfour',
 'sixhundredandfortyfive',
 'sixhundredandfortysix',
 'sixhundredandfortyseven',
 'sixhundredandfortyeight',
 'sixhundredandfortynine',
 'sixhundredandfifty',
 'sixhundredandfiftyone',
 'sixhundredandfiftytwo',
 'sixhundredandfiftythree',
 'sixhundredandfiftyfour',
 'sixhundredandfiftyfive',
 'sixhundredandfiftysix',
 'sixhundredandfiftyseven',
 'sixhundredandfiftyeight',
 'sixhundredandfiftynine',
 'sixhundredandsixty',
 'sixhundredandsixtyone',
 'sixhundredandsixtytwo',
 'sixhundredandsixtythree',
 'sixhundredandsixtyfour',
 'sixhundredandsixtyfive',
 'sixhundredandsixtysix',
 'sixhundredandsixtyseven',
 'sixhundredandsixtyeight',
 'sixhundredandsixtynine',
 'sixhundredandseventy',
 'sixhundredandseventyone',
 'sixhundredandseventytwo',
 'sixhundredandseventythree',
 'sixhundredandseventyfour',
 'sixhundredandseventyfive',
 'sixhundredandseventysix',
 'sixhundredandseventyseven',
 'sixhundredandseventyeight',
 'sixhundredandseventynine',
 'sixhundredandeighty',
 'sixhundredandeightyone',
 'sixhundredandeightytwo',
 'sixhundredandeightythree',
 'sixhundredandeightyfour',
 'sixhundredandeightyfive',
 'sixhundredandeightysix',
 'sixhundredandeightyseven',
 'sixhundredandeightyeight',
 'sixhundredandeightynine',
 'sixhundredandninety',
 'sixhundredandninetyone',
 'sixhundredandninetytwo',
 'sixhundredandninetythree',
 'sixhundredandninetyfour',
 'sixhundredandninetyfive',
 'sixhundredandninetysix',
 'sixhundredandninetyseven',
 'sixhundredandninetyeight',
 'sixhundredandninetynine',
 'sevenhundred',
 'sevenhundredandone',
 'sevenhundredandtwo',
 'sevenhundredandthree',
 'sevenhundredandfour',
 'sevenhundredandfive',
 'sevenhundredandsix',
 'sevenhundredandseven',
 'sevenhundredandeight',
 'sevenhundredandnine',
 'sevenhundredandten',
 'sevenhundredandeleven',
 'sevenhundredandtwelve',
 'sevenhundredandthirteen',
 'sevenhundredandfourteen',
 'sevenhundredandfifteen',
 'sevenhundredandsixteen',
 'sevenhundredandseventeen',
 'sevenhundredandeighteen',
 'sevenhundredandnineteen',
 'sevenhundredandtwenty',
 'sevenhundredandtwentyone',
 'sevenhundredandtwentytwo',
 'sevenhundredandtwentythree',
 'sevenhundredandtwentyfour',
 'sevenhundredandtwentyfive',
 'sevenhundredandtwentysix',
 'sevenhundredandtwentyseven',
 'sevenhundredandtwentyeight',
 'sevenhundredandtwentynine',
 'sevenhundredandthirty',
 'sevenhundredandthirtyone',
 'sevenhundredandthirtytwo',
 'sevenhundredandthirtythree',
 'sevenhundredandthirtyfour',
 'sevenhundredandthirtyfive',
 'sevenhundredandthirtysix',
 'sevenhundredandthirtyseven',
 'sevenhundredandthirtyeight',
 'sevenhundredandthirtynine',
 'sevenhundredandforty',
 'sevenhundredandfortyone',
 'sevenhundredandfortytwo',
 'sevenhundredandfortythree',
 'sevenhundredandfortyfour',
 'sevenhundredandfortyfive',
 'sevenhundredandfortysix',
 'sevenhundredandfortyseven',
 'sevenhundredandfortyeight',
 'sevenhundredandfortynine',
 'sevenhundredandfifty',
 'sevenhundredandfiftyone',
 'sevenhundredandfiftytwo',
 'sevenhundredandfiftythree',
 'sevenhundredandfiftyfour',
 'sevenhundredandfiftyfive',
 'sevenhundredandfiftysix',
 'sevenhundredandfiftyseven',
 'sevenhundredandfiftyeight',
 'sevenhundredandfiftynine',
 'sevenhundredandsixty',
 'sevenhundredandsixtyone',
 'sevenhundredandsixtytwo',
 'sevenhundredandsixtythree',
 'sevenhundredandsixtyfour',
 'sevenhundredandsixtyfive',
 'sevenhundredandsixtysix',
 'sevenhundredandsixtyseven',
 'sevenhundredandsixtyeight',
 'sevenhundredandsixtynine',
 'sevenhundredandseventy',
 'sevenhundredandseventyone',
 'sevenhundredandseventytwo',
 'sevenhundredandseventythree',
 'sevenhundredandseventyfour',
 'sevenhundredandseventyfive',
 'sevenhundredandseventysix',
 'sevenhundredandseventyseven',
 'sevenhundredandseventyeight',
 'sevenhundredandseventynine',
 'sevenhundredandeighty',
 'sevenhundredandeightyone',
 'sevenhundredandeightytwo',
 'sevenhundredandeightythree',
 'sevenhundredandeightyfour',
 'sevenhundredandeightyfive',
 'sevenhundredandeightysix',
 'sevenhundredandeightyseven',
 'sevenhundredandeightyeight',
 'sevenhundredandeightynine',
 'sevenhundredandninety',
 'sevenhundredandninetyone',
 'sevenhundredandninetytwo',
 'sevenhundredandninetythree',
 'sevenhundredandninetyfour',
 'sevenhundredandninetyfive',
 'sevenhundredandninetysix',
 'sevenhundredandninetyseven',
 'sevenhundredandninetyeight',
 'sevenhundredandninetynine',
 'eighthundred',
 'eighthundredandone',
 'eighthundredandtwo',
 'eighthundredandthree',
 'eighthundredandfour',
 'eighthundredandfive',
 'eighthundredandsix',
 'eighthundredandseven',
 'eighthundredandeight',
 'eighthundredandnine',
 'eighthundredandten',
 'eighthundredandeleven',
 'eighthundredandtwelve',
 'eighthundredandthirteen',
 'eighthundredandfourteen',
 'eighthundredandfifteen',
 'eighthundredandsixteen',
 'eighthundredandseventeen',
 'eighthundredandeighteen',
 'eighthundredandnineteen',
 'eighthundredandtwenty',
 'eighthundredandtwentyone',
 'eighthundredandtwentytwo',
 'eighthundredandtwentythree',
 'eighthundredandtwentyfour',
 'eighthundredandtwentyfive',
 'eighthundredandtwentysix',
 'eighthundredandtwentyseven',
 'eighthundredandtwentyeight',
 'eighthundredandtwentynine',
 'eighthundredandthirty',
 'eighthundredandthirtyone',
 'eighthundredandthirtytwo',
 'eighthundredandthirtythree',
 'eighthundredandthirtyfour',
 'eighthundredandthirtyfive',
 'eighthundredandthirtysix',
 'eighthundredandthirtyseven',
 'eighthundredandthirtyeight',
 'eighthundredandthirtynine',
 'eighthundredandforty',
 'eighthundredandfortyone',
 'eighthundredandfortytwo',
 'eighthundredandfortythree',
 'eighthundredandfortyfour',
 'eighthundredandfortyfive',
 'eighthundredandfortysix',
 'eighthundredandfortyseven',
 'eighthundredandfortyeight',
 'eighthundredandfortynine',
 'eighthundredandfifty',
 'eighthundredandfiftyone',
 'eighthundredandfiftytwo',
 'eighthundredandfiftythree',
 'eighthundredandfiftyfour',
 'eighthundredandfiftyfive',
 'eighthundredandfiftysix',
 'eighthundredandfiftyseven',
 'eighthundredandfiftyeight',
 'eighthundredandfiftynine',
 'eighthundredandsixty',
 'eighthundredandsixtyone',
 'eighthundredandsixtytwo',
 'eighthundredandsixtythree',
 'eighthundredandsixtyfour',
 'eighthundredandsixtyfive',
 'eighthundredandsixtysix',
 'eighthundredandsixtyseven',
 'eighthundredandsixtyeight',
 'eighthundredandsixtynine',
 'eighthundredandseventy',
 'eighthundredandseventyone',
 'eighthundredandseventytwo',
 'eighthundredandseventythree',
 'eighthundredandseventyfour',
 'eighthundredandseventyfive',
 'eighthundredandseventysix',
 'eighthundredandseventyseven',
 'eighthundredandseventyeight',
 'eighthundredandseventynine',
 'eighthundredandeighty',
 'eighthundredandeightyone',
 'eighthundredandeightytwo',
 'eighthundredandeightythree',
 'eighthundredandeightyfour',
 'eighthundredandeightyfive',
 'eighthundredandeightysix',
 'eighthundredandeightyseven',
 'eighthundredandeightyeight',
 'eighthundredandeightynine',
 'eighthundredandninety',
 'eighthundredandninetyone',
 'eighthundredandninetytwo',
 'eighthundredandninetythree',
 'eighthundredandninetyfour',
 'eighthundredandninetyfive',
 'eighthundredandninetysix',
 'eighthundredandninetyseven',
 'eighthundredandninetyeight',
 'eighthundredandninetynine',
 'ninehundred',
 'ninehundredandone',
 'ninehundredandtwo',
 'ninehundredandthree',
 'ninehundredandfour',
 'ninehundredandfive',
 'ninehundredandsix',
 'ninehundredandseven',
 'ninehundredandeight',
 'ninehundredandnine',
 'ninehundredandten',
 'ninehundredandeleven',
 'ninehundredandtwelve',
 'ninehundredandthirteen',
 'ninehundredandfourteen',
 'ninehundredandfifteen',
 'ninehundredandsixteen',
 'ninehundredandseventeen',
 'ninehundredandeighteen',
 'ninehundredandnineteen',
 'ninehundredandtwenty',
 'ninehundredandtwentyone',
 'ninehundredandtwentytwo',
 'ninehundredandtwentythree',
 'ninehundredandtwentyfour',
 'ninehundredandtwentyfive',
 'ninehundredandtwentysix',
 'ninehundredandtwentyseven',
 'ninehundredandtwentyeight',
 'ninehundredandtwentynine',
 'ninehundredandthirty',
 'ninehundredandthirtyone',
 'ninehundredandthirtytwo',
 'ninehundredandthirtythree',
 'ninehundredandthirtyfour',
 'ninehundredandthirtyfive',
 'ninehundredandthirtysix',
 'ninehundredandthirtyseven',
 'ninehundredandthirtyeight',
 'ninehundredandthirtynine',
 'ninehundredandforty',
 'ninehundredandfortyone',
 'ninehundredandfortytwo',
 'ninehundredandfortythree',
 'ninehundredandfortyfour',
 'ninehundredandfortyfive',
 'ninehundredandfortysix',
 'ninehundredandfortyseven',
 'ninehundredandfortyeight',
 'ninehundredandfortynine',
 'ninehundredandfifty',
 'ninehundredandfiftyone',
 'ninehundredandfiftytwo',
 'ninehundredandfiftythree',
 'ninehundredandfiftyfour',
 'ninehundredandfiftyfive',
 'ninehundredandfiftysix',
 'ninehundredandfiftyseven',
 'ninehundredandfiftyeight',
 'ninehundredandfiftynine',
 'ninehundredandsixty',
 'ninehundredandsixtyone',
 'ninehundredandsixtytwo',
 'ninehundredandsixtythree',
 'ninehundredandsixtyfour',
 'ninehundredandsixtyfive',
 'ninehundredandsixtysix',
 'ninehundredandsixtyseven',
 'ninehundredandsixtyeight',
 'ninehundredandsixtynine',
 'ninehundredandseventy',
 'ninehundredandseventyone',
 'ninehundredandseventytwo',
 'ninehundredandseventythree',
 'ninehundredandseventyfour',
 'ninehundredandseventyfive',
 'ninehundredandseventysix',
 'ninehundredandseventyseven',
 'ninehundredandseventyeight',
 'ninehundredandseventynine',
 'ninehundredandeighty',
 'ninehundredandeightyone',
 'ninehundredandeightytwo',
 'ninehundredandeightythree',
 'ninehundredandeightyfour',
 'ninehundredandeightyfive',
 'ninehundredandeightysix',
 'ninehundredandeightyseven',
 'ninehundredandeightyeight',
 'ninehundredandeightynine',
 'ninehundredandninety',
 'ninehundredandninetyone',
 'ninehundredandninetytwo',
 'ninehundredandninetythree',
 'ninehundredandninetyfour',
 'ninehundredandninetyfive',
 'ninehundredandninetysix',
 'ninehundredandninetyseven',
 'ninehundredandninetyeight',
 'ninehundredandninetynine',
 'onethousand']

Now write a set of assert tests for your number_to_words function that verifies that it is working as expected.


In [79]:
assert len(number_to_words(582)) == 582
assert len(number_to_words(1000)) == 1000
assert number_to_words(5) == ['one', 'two', 'three', 'four', 'five']

In [80]:
assert True # use this for grading the number_to_words tests.

Now define a count_letters(n) that returns the number of letters used to write out the words for all of the the numbers 1 to n inclusive.


In [81]:
def count_letters(n):
    """Count the number of letters used to write out the words for 1-n inclusive."""
    x=0
    nums = number_to_words(n)
    for number in nums:
        x+=len(number)
    return x

Now write a set of assert tests for your count_letters function that verifies that it is working as expected.


In [82]:
# YOUR CODE HERE
assert count_letters(5) == 19

In [83]:
assert True # use this for grading the count_letters tests.

Finally used your count_letters function to solve the original question.


In [84]:
answer = count_letters(1000)
print(answer)


21124

In [85]:
assert True # use this for gradig the answer to the original question.