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 [5]:
def number_to_words(n):
"""Given a number n between 1-1000 inclusive return a list of words for the number."""
dic_1s == ["one", 'two','three','four','five''six','seven','eight','nine']
dic_10s == ['ten','twenty', 'thirty','fourty','fifty', 'sixty','seveny','eighty','ninety']
1000 == "one thousand"
for n in range(1-1001):
if n > 99 and not (100, 200, 300, 400, 500, 600, 700, 800, 900, 1000):
return dic_1s()+ "hundred" + "and" + dic_10() + dic_1s()
Now write a set of assert tests for your number_to_words function that verifies that it is working as expected.
In [6]:
n= 3
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 [ ]:
def count_letters(n):
"""Count the number of letters used to write out the words for 1-n inclusive."""
if count_letters is int return len(string(n))
Now write a set of assert tests for your count_letters function that verifies that it is working as expected.
In [ ]:
# YOUR CODE HERE
raise NotImplementedError()
In [ ]:
assert True # use this for grading the count_letters tests.
Finally used your count_letters function to solve the original question.
In [ ]:
# YOUR CODE HERE
raise NotImplementedError()
In [ ]:
assert True # use this for gradig the answer to the original question.