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 [223]:
def number_to_words(n):
"""Given a number n between 1-1000 inclusive return a list of words for the number."""
#Create a dictionary to use throughout each if loop
numbers = {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",
20:"twenty",30:"thirty", 40:"forty", 50:"fifty",60:"sixty",70:"seventy",80:"eighty",90:"ninety",
100: "hundred", 1000:"thousand"}
#empty array to hold strings of numbers and a counter variable
a = []
s = n
while s !=0:
#convert number to string
f = str(s)
#Less than a hundred case
if s <=20:
a.append(numbers[s])
if 20<s<30:
a.append(numbers[20]+numbers[int(f[1])])
if 30<s<40:
a.append(numbers[30]+numbers[int(f[1])])
if 40<s<50:
a.append(numbers[40]+numbers[int(f[1])])
if 50<s<60:
a.append(numbers[50]+numbers[int(f[1])])
if 60<s<70:
a.append(numbers[60]+numbers[int(f[1])])
if 70<s<80:
a.append(numbers[70]+numbers[int(f[1])])
if 80<s<90:
a.append(numbers[80]+numbers[int(f[1])])
if 90<s<100:
a.append(numbers[90]+numbers[int(f[1])])
#Deals with 10s and hundreds
if s == 30:
a.append(numbers[s])
if s == 40:
a.append(numbers[s])
if s == 50:
a.append(numbers[s])
if s == 60:
a.append(numbers[s])
if s == 70:
a.append(numbers[s])
if s == 80:
a.append(numbers[s])
if s == 90:
a.append(numbers[s])
if s == 100:
a.append(numbers[1] + numbers[100])
if s == 200:
a.append(numbers[2] + numbers[100])
if s == 300:
a.append(numbers[3] + numbers[100])
if s == 400:
a.append(numbers[4] + numbers[100])
if s == 500:
a.append(numbers[5] + numbers[100])
if s == 600:
a.append(numbers[6] + numbers[100])
if s == 700:
a.append(numbers[7] + numbers[100])
if s == 800:
a.append(numbers[8] + numbers[100])
if s == 900:
a.append(numbers[9] + numbers[100])
if s == 1000:
a.append(numbers[1] + numbers[1000])
#all these if loops go through by hundreds to add to the list
if 100<s<200:
x = int(f[-2:])
l = f[0]
if x <=20:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[int(x)])
if 20<x<30:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[20]+numbers[int(f[-1:])])
if 30<x<40:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[30]+numbers[int(f[-1:])])
if 40<x<50:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[40]+numbers[int(f[-1:])])
if 50<x<60:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[50]+numbers[int(f[-1:])])
if 60<x<70:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[60]+numbers[int(f[-1:])])
if 70<x<80:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[70]+numbers[int(f[-1:])])
if 80<x<90:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[80]+numbers[int(f[-1:])])
if 90<x<100:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[90]+numbers[int(f[-1:])])
if x == 30:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 40:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 50:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 60:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 70:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 80:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 90:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if 200<s<300:
x = int(f[-2:])
l = f[0]
if x <=20:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[int(x)])
if 20<x<30:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[20]+numbers[int(f[-1:])])
if 30<x<40:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[30]+numbers[int(f[-1:])])
if 40<x<50:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[40]+numbers[int(f[-1:])])
if 50<x<60:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[50]+numbers[int(f[-1:])])
if 60<x<70:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[60]+numbers[int(f[-1:])])
if 70<x<80:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[70]+numbers[int(f[-1:])])
if 80<x<90:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[80]+numbers[int(f[-1:])])
if 90<x<100:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[90]+numbers[int(f[-1:])])
if x == 30:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 40:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 50:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 60:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 70:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 80:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 90:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if 300<s<400:
x = int(f[-2:])
l = f[0]
if x <=20:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[int(x)])
if 20<x<30:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[20]+numbers[int(f[-1:])])
if 30<x<40:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[30]+numbers[int(f[-1:])])
if 40<x<50:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[40]+numbers[int(f[-1:])])
if 50<x<60:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[50]+numbers[int(f[-1:])])
if 60<x<70:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[60]+numbers[int(f[-1:])])
if 70<x<80:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[70]+numbers[int(f[-1:])])
if 80<x<90:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[80]+numbers[int(f[-1:])])
if 90<x<100:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[90]+numbers[int(f[-1:])])
if x == 30:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 40:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 50:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 60:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 70:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 80:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 90:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if 400<s<500:
x = int(f[-2:])
l = f[0]
if x <=20:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[int(x)])
if 20<x<30:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[20]+numbers[int(f[-1:])])
if 30<x<40:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[30]+numbers[int(f[-1:])])
if 40<x<50:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[40]+numbers[int(f[-1:])])
if 50<x<60:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[50]+numbers[int(f[-1:])])
if 60<x<70:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[60]+numbers[int(f[-1:])])
if 70<x<80:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[70]+numbers[int(f[-1:])])
if 80<x<90:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[80]+numbers[int(f[-1:])])
if 90<x<100:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[90]+numbers[int(f[-1:])])
if x == 30:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 40:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 50:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 60:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 70:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 80:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 90:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if 500<s<600:
x = int(f[-2:])
l = f[0]
if x <=20:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[int(x)])
if 20<x<30:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[20]+numbers[int(f[-1:])])
if 30<x<40:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[30]+numbers[int(f[-1:])])
if 40<x<50:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[40]+numbers[int(f[-1:])])
if 50<x<60:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[50]+numbers[int(f[-1:])])
if 60<x<70:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[60]+numbers[int(f[-1:])])
if 70<x<80:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[70]+numbers[int(f[-1:])])
if 80<x<90:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[80]+numbers[int(f[-1:])])
if 90<x<100:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[90]+numbers[int(f[-1:])])
if x == 30:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 40:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 50:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 60:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 70:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 80:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 90:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if 600<s<700:
x = int(f[-2:])
l = f[0]
if x <=20:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[int(x)])
if 20<x<30:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[20]+numbers[int(f[-1:])])
if 30<x<40:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[30]+numbers[int(f[-1:])])
if 40<x<50:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[40]+numbers[int(f[-1:])])
if 50<x<60:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[50]+numbers[int(f[-1:])])
if 60<x<70:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[60]+numbers[int(f[-1:])])
if 70<x<80:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[70]+numbers[int(f[-1:])])
if 80<x<90:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[80]+numbers[int(f[-1:])])
if 90<x<100:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[90]+numbers[int(f[-1:])])
if x == 30:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 40:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 50:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 60:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 70:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 80:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 90:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if 700<s<800:
x = int(f[-2:])
l = f[0]
if x <=20:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[int(x)])
if 20<x<30:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[20]+numbers[int(f[-1:])])
if 30<x<40:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[30]+numbers[int(f[-1:])])
if 40<x<50:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[40]+numbers[int(f[-1:])])
if 50<x<60:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[50]+numbers[int(f[-1:])])
if 60<x<70:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[60]+numbers[int(f[-1:])])
if 70<x<80:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[70]+numbers[int(f[-1:])])
if 80<x<90:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[80]+numbers[int(f[-1:])])
if 90<x<100:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[90]+numbers[int(f[-1:])])
if x == 30:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 40:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 50:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 60:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 70:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 80:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 90:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if 800<s<900:
x = int(f[-2:])
l = f[0]
if x <=20:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[int(x)])
if 20<x<30:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[20]+numbers[int(f[-1:])])
if 30<x<40:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[30]+numbers[int(f[-1:])])
if 40<x<50:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[40]+numbers[int(f[-1:])])
if 50<x<60:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[50]+numbers[int(f[-1:])])
if 60<x<70:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[60]+numbers[int(f[-1:])])
if 70<x<80:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[70]+numbers[int(f[-1:])])
if 80<x<90:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[80]+numbers[int(f[-1:])])
if 90<x<100:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[90]+numbers[int(f[-1:])])
if x == 30:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 40:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 50:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 60:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 70:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 80:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 90:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if 900<s<1000:
x = int(f[-2:])
l = f[0]
if x <=20:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[int(x)])
if 20<x<30:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[20]+numbers[int(f[-1:])])
if 30<x<40:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[30]+numbers[int(f[-1:])])
if 40<x<50:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[40]+numbers[int(f[-1:])])
if 50<x<60:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[50]+numbers[int(f[-1:])])
if 60<x<70:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[60]+numbers[int(f[-1:])])
if 70<x<80:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[70]+numbers[int(f[-1:])])
if 80<x<90:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[80]+numbers[int(f[-1:])])
if 90<x<100:
a.append(numbers[int(l)] + numbers[100]+"and" + numbers[90]+numbers[int(f[-1:])])
if x == 30:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 40:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 50:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 60:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 70:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 80:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
if x == 90:
a.append(numbers[int(l)] + numbers[100]+"and"+numbers[x])
s -=1
return a
print (number_to_words(1000))
In [172]:
answer = number_to_words(110)
assert answer[0] == "onehundredandten"
answer1 = number_to_words(1000)
assert answer1[0] == "onethousand"
answer2 = number_to_words(200)
assert answer2[0] == "twohundred"
In [ ]:
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 [239]:
def count_letters(n):
"""Count the number of letters used to write out the words for 1-n inclusive."""
f = number_to_words(n)
counter = 0
sum = 0
while counter !=n:
sum += len(f[counter])
counter +=1
return sum
Now write a set of assert
tests for your count_letters
function that verifies that it is working as expected.
In [237]:
assert count_letters(1) == 3
assert count_letters(2) == 6
assert count_letters(100) == 864
In [ ]:
assert True # use this for grading the count_letters tests.
Finally used your count_letters
function to solve the original question.
In [238]:
print (count_letters(1000))
In [ ]:
assert True # use this for gradig the answer to the original question.