0

I've tried to tackle this problem by using the actual words and the len() function. I keep getting 21224 but the answer is 21124. Can someone please explain why? Here's the problem.

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.

one_nine='onetwothreefourfivesixseveneightnine'
ten_nineteen='teneleventwelvethirteenfourteenfifteensixteenseventeeneighteennineteen'
twenty_ninety_byten='twentythirtyfourtyfiftysixtyseventyeightyninety'
one_ninetynine_list=[one_nine*9,ten_nineteen,twenty_ninety_byten*10]
one_ninetynine=''.join(one_ninetynine_list)

onehundred_ninehundred_byonehundred_list=[one_nine,'hundred'*9]
onehundred_ninehundred_byonehundred=''.join(onehundred_ninehundred_byonehundred_list)
one_onethousand_list=[one_ninetynine*10,onehundred_ninehundred_byonehundred*100,'and'*891,'onethousand']
one_onethousand=''.join(one_onethousand_list)
print len(one_onethousand)

2 Answers 2

1

Check your spelling of forty. The proper way to spell it is 'forty' not 'fourty'.

Sign up to request clarification or add additional context in comments.

Comments

0

you can try with this

from num2words import num2words

list = []
for i in range(1, 1001):
    list.append(num2words(i, lang='en_GB'))

letters = 0
for element in list:
    for letter in element:
        if 97 <= ord(letter) <= 122:
            letters += 1

print(letters)

2 Comments

When I use your code in python 2.7.11 it says num2words doesn't exist. Maybe it only works in python 3? which I don't want to use by the way.
pip install num2words

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.