Project Euler Problem 017
Statement
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.
Solution
For this I built a decoder for one-digit, two-digit and three-digit numbers that of course are connected.
Then it is just an iteration.
def one_number_decoder(n): if n == '0': return 0 if n == '1': return 3 if n == '2': return 3 if n == '3': return 5 if n == '4': return 4 if n == '5': return 4 if n == '6': return 3 if n == '7': return 5 if n == '8': return 5 if n == '9': return 4 def two_number_decoder(n): x = n[0] y = n[1] if x == '0': return one_number_decoder(n[1]) if x == '1': if y == '0': return 3 if y == '1': return 6 if y == '2': return 6 if y == '3': return len('thriteen') if y == '4': return len('fourteen') if y == '5': return len('fifteen') if y == '6': return len('sixteen') if y == '7': return len('seventeen') if y == '8': return len('eighteen') if y == '9': return len('nineteen') if x == '2': res = len('twenty') if x == '3': res = len('thrity') if x == '4': res = len('forty') if x == '5': res = len('fifty') if x == '6': res = len('sixty') if x == '7': res = len('seventy') if x == '8': res = len('eighty') if x == '9': res = len('ninety') return res + one_number_decoder(y) def three_number_decoder(n): x = n[0] res = one_number_decoder(x) res += len('hundred') additional = two_number_decoder(n[1:3]) if additional > 0: res += 3 #Adding 'and' word res += additional return res if __name__ == '__main__': result = 0 for n in range(1,1000): if n < 10: result += one_number_decoder(str(n)) if n >= 10 and n < 100: result += two_number_decoder(str(n)) if n >= 100 and n < 1000: result += three_number_decoder(str(n)) result += len('onethousand') print("The result is:", result)
The Python is available for download here.