[PYTHON] Project Euler 42

problem

The n-term of a triangular number is given by tn = ½n (n + 1). The first 10 terms are

1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... Is.

The sum is taken after converting the alphabet in the word to a number. We call this sum the "word value". For example, SKY is 19 + 11 + 25 = 55 = t10. The word value is a triangular number. At one point, the word is called a triangular word.

Approximately 2000 English words are written in the 16K text file words.txt. How many triangular words are there? http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2042

Answer

The response policy is as follows.

  1. Make a set of reference triangular numbers.
  2. Calculate the value from the word.
  3. Determine if the calculated value is included in the set of triangular numbers.
  4. If it is determined that it is included, add 1 to the variable ans.
def main():
  words = file2list('p042_words.txt')
  MAX = max_length(words) * 26
  tri = tri_dict(MAX)
  ans = 0
  for word in words:
    if word2num(word) in tri:
      ans += 1
  print ans

file2list is a function that opens a file and lists the read strings. If you pass a function to the list, it seems to be more versatile, but it's annoying, so it's not supported.

def file2list(filename):
  file = open(filename)
  ret = file.read().replace('"','').split(',')
  file.close()
  return ret

A function that calculates the number of characters in a word list words to determine the maximum value for creating a set of triangular numbers.

def max_length(words):
  max_len = 0
  for word in words:
    if len(word) > max_len:
      max_len = len(word)
  return max_len

A function that returns a dictionary-type object that stores a triangular number that is less than or equal to the specified maximum value.

def f(n):
  return n * (n+1) / 2

def tri_dict(max):
  n=1
  tri = {}
  while f(n)<=max:
    tri[f(n)] = True
    n+=1
  return tri

Functions that digitize words, etc. I should have used reduce. Note that ord is a built-in function that returns the integer representing the Unicode code point if the string is a unicode object, and the value of that byte if the string is an 8-bit string, for a given string of length 1. In alfa2num, the value of the byte of'A'is 65, so the value obtained by subtracting 64 is returned.

def alfa2num(s):
  return ord(s) - 64
  
def word2num(word):
  return sum([alfa2num(s) for s in word])

Recommended Posts

Project Euler 37
Project Euler 47
Project Euler 31
Project Euler 4
Project Euler 38
Project Euler 26
Project Euler 8
Project Euler 22
Project Euler 19
Project Euler 50
Project Euler 42
Project Euler 33
Project Euler 32
Project Euler 43
Project Euler 35
Project Euler 36
Project Euler 24
Project Euler 46
Project Euler 48
Project Euler 45
Project Euler 6
Project Euler 44
Project Euler 39
Project Euler 40
Project Euler 49
Project Euler 29
Project Euler 27
Project Euler 41
Project Euler 18
Project Euler 13
Project Euler 30
Project Euler 16
Project Euler 14
Project Euler 34
Project Euler 25
[Project Euler] problem1
Project Euler15 "Lattice Path"
Project Euler 2 Acceleration 2.21 Save microseconds.
Project Euler Original Method Group 1
What is Project Euler 3 Acceleration?
Functional programming in Python Project Euler 1
Project Euler 10 "Sum of Prime Numbers"
[Note] Project Euler in Python (Problem 1-22)
Functional programming in Python Project Euler 3
Project Euler # 5 "Minimum Multiples" in Python
Project Euler 4 Attempt to speed up
Functional programming in Python Project Euler 2
Project Euler 11 "Maximum product in grid"
Project Euler # 15 "Lattice Path" in Python
Project Euler # 4 "Maximum Palindrome" in Python
Project Euler 9 Retention of calculation results
Project Euler # 3 "Maximum Prime Factors" in Python
Project Euler # 11 "Maximum Product in Grid" in Python
Project Euler # 7 "1000 1st prime number" in Python
Project Euler # 16 "Sum of Powers" in Python
Project Euler # 9 "Special Pythagorean Triple" in Python
Project Euler # 14 "Longest Collatz Sequence" in Python
I wrote Project Euler 1 in one liner.
Project Euler # 2 "Even Fibonacci Numbers" in Python
Project Euler # 17 "Number of Characters" in Python
Project Euler # 1 "Multiples of 3 and 5" in Python