Project Euler # 17 "Number of Characters" in Python

Problem 16 "Number of characters"

If you write the numbers from 1 to 5 in English words, it is one, two, three, four, five, and a total of 3 + 3 + 5 + 4 + 4 = 19 letters are used. Then, if you write all the numbers from 1 to 1000 (one thousand) in English words, how many letters will be in total? Note: Do not count whitespace or hyphens. For example, 342 (three hundred and forty-two) counts as 23 characters, 115 (one hundred and fifteen) counts as 20 characters. Note that "and" is used. British custom.

Python


# n = 5
n = 1000

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", 1000:"one thousand"}

def number_to_word(num):
  if num in numbers:
    return numbers[num]
  elif num < 100:
    a = num % 10
    b = (num // 10) * 10
    return number_to_word(b) + "-" + number_to_word(a)
  else:
    a = num % 100
    b = num // 100
    if a == 0:
      return number_to_word(b) + " hundred"
    else:
      return number_to_word(b) + " hundred and " + number_to_word(a)

def to_character_num(word):
  return len(word.replace(" ", "").replace("-", ""))

seq = range(1, n+1)
words = map(number_to_word, seq)
result = sum(map(to_character_num, words))

print result
print result == 21124
print words[:6]
print words[-3:]

result


21124
True
['one', 'two', 'three', 'four', 'five', 'six']
['nine hundred and ninety-eight', 'nine hundred and ninety-nine', 'one thousand']

Recommended Posts

Project Euler # 17 "Number of Characters" in Python
Project Euler # 7 "1000 1st prime number" in Python
Project Euler # 16 "Sum of Powers" in Python
Project Euler # 1 "Multiples of 3 and 5" in Python
Project Euler # 8 "Maximum Product in Number String" in Python
Project Euler # 10 "sum of prime numbers" in Python
Project Euler # 12 "High Divisibility Triangular Number" in Python
Project Euler # 13 "Sum of Large Numbers" in Python
Project Euler # 6 "Difference in sum of squares" in Python
Functional programming in Python Project Euler 1
[Note] Project Euler in Python (Problem 1-22)
Functional programming in Python Project Euler 3
Functional programming in Python Project Euler 2
Project Euler # 15 "Lattice Path" in Python
Project Euler # 4 "Maximum Palindrome" in Python
Maximum number of characters in Python3 shell call (per OS)
Project Euler # 3 "Maximum Prime Factors" in Python
Project Euler # 11 "Maximum Product in Grid" in Python
Project Euler # 9 "Special Pythagorean Triple" in Python
Project Euler # 14 "Longest Collatz Sequence" in Python
Project Euler # 2 "Even Fibonacci Numbers" in Python
Count the number of Thai and Arabic characters well in Python
Prime number 2 in Python
Output the number of CPU cores in Python
How to get the number of digits in Python
Divides the character string by the specified number of characters. In Ruby and Python.
Get the size (number of elements) of UnionFind in Python
How to identify the element with the smallest number of characters in a Python list?
Count the number of characters in the text on the clipboard on mac
Get the number of specific elements in a python list
What I learned by solving 30 questions of python Project Euler
Python --Find out number of groups in the regex expression
[Homology] Count the number of holes in data with Python
Number recognition in images with Python
Pixel manipulation of images in Python
Project Euler 10 "Sum of Prime Numbers"
Division of timedelta in Python 2.7 series
Infinite prime number generator in Python3
MySQL-automatic escape of parameters in python
Handling of JSON files in Python
Implementation of life game in Python
Waveform display of audio in Python
Create Python project documentation in Sphinx
Law of large numbers in python
Study, number guessing game in Python
Implementation of original sorting in Python
Project Euler 11 "Maximum product in grid"
Reversible scrambling of integers in Python
Project Euler 9 Retention of calculation results
Project Euler 37
Project Euler 7
Project Euler 47
Project Euler 31
Project Euler 4
Project Euler 38
Project Euler 17
Project Euler 26
Project Euler 8
Project Euler 23
Project Euler 22
[Python] Let's reduce the number of elements in the result in set operations