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.
In [1]:
number={}
number[0]="zero"
number[1]="one"
number[2]="two"
number[3]="three"
number[4]='four'
number[5]='five'
number[6]='six'
number[7]='seven'
number[8]='eight'
number[9]='nine'
number[10]='ten'
number[11]='eleven'
number[12]='twelve'
number[13]='thirteen'
number[14]='fourteen'
number[15]='fifteen'
number[16]='teen'
number[20]='twenty'
number[30]='thirty'
number[40]='forty'
number[50]='fifty'
number[60]='sixty'
number[70]='seventy'
number[80]='eighty'
number[90]='ninety'
number[17]='and'
number[100]='hundred'
number[1000]='thousand'
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 [23]:
num=[]
def number_to_words(n):
"""Given a number n between 1-1000 inclusive return a list of words for the number."""
if n<16:
return number[n]
if n>15 and n<20:
num=number[n-10]+number[16]
return num
if n>19 and n<31:
num=number[20]+number[n-20]
return num
if n>29 and n<41:
num=number[30]+number[n-30]
return num
if n>39 and n<51:
num=number[40]+number[n-40]
return num
if n>49 and n<61:
num=number[50]+number[n-50]
return num
if n>59 and n<71:
num=number[60]+number[n-60]
return num
if n>69 and n<81:
num=number[70]+number[n-70]
return num
if n>79 and n<91:
num=number[80]+number[n-80]
return num
if n>89 and n<101:
num=number[90]+number[n-90]
return num
if n>99 and n<200:
num= number[1]+number[100]+ number[17] + number_to_words(n-100)
return num
if n>199 and n<300:
num= number[2]+number[100]+ number[17] + number_to_words(n-200)
return num
if n>299 and n<400:
num= number[3]+number[100]+ number[17] + number_to_words(n-300)
return num
if n>399 and n<500:
num= number[4]+number[100]+ number[17] + number_to_words(n-400)
return num
if n>499 and n<600:
num= number[5]+number[100]+ number[17] + number_to_words(n-500)
return num
if n>599 and n<700:
num= number[6]+number[100]+ number[17] + number_to_words(n-600)
return num
if n>699 and n<800:
num= number[7]+number[100]+ number[17] + number_to_words(n-700)
return num
if n>799 and n<900:
num= number[8]+number[100]+ number[17] + number_to_words(n-800)
return num
if n>899 and n<1000:
num= number[9]+number[100]+ number[17] + number_to_words(n-900)
return num
# number
# len(number[100])
number_to_words(767)
Out[23]:
Now write a set of assert
tests for your number_to_words
function that verifies that it is working as expected.
In [24]:
# YOUR CODE HERE
assert number_to_words(342)=='threehundredandfortytwo'
assert number_to_words(115)=='onehundredandfifteen'
In [4]:
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 [22]:
def count_letters(n):
"""Count the number of letters used to write out the words for 1-n inclusive."""
count=0
while n>0:
L=number_to_words(n)
count=count+len(L)
n=n-1
return count
count_letters(115)
Out[22]:
Now write a set of assert
tests for your count_letters
function that verifies that it is working as expected.
In [21]:
# YOUR CODE HERE
assert count_letters(5)==19
assert count_letters(10)==39
In [17]:
assert True # use this for grading the count_letters tests.
Finally used your count_letters
function to solve the original question.
In [20]:
# YOUR CODE HERE
count_letters(999)
Out[20]:
In [ ]:
In [ ]: