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

number_to_words(1000)

"""
Explanation: 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
End of explanation
"""


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

assert True # use this for grading the number_to_words tests.

"""
Explanation: Now write a set of assert tests for your number_to_words function that verifies that it is working as expected.
End of explanation
"""


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
        

"""
Explanation: 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.
End of explanation
"""


# YOUR CODE HERE
assert count_letters(5) == 19

assert True # use this for grading the count_letters tests.

"""
Explanation: Now write a set of assert tests for your count_letters function that verifies that it is working as expected.
End of explanation
"""


answer = count_letters(1000)
print(answer)

assert True # use this for gradig the answer to the original question.

"""
Explanation: Finally used your count_letters function to solve the original question.
End of explanation
"""


