LoginSignup
0
0

More than 1 year has passed since last update.

【Project Euler】Problem 42: 符号化された三角数

Posted at
  • 本記事はProjectEulerの「100番以下の問題の説明は記載可能」という規定に基づいて回答のヒントが書かれていますので、自分である程度考えてみてから読まれることをお勧めします。

問題 42. 符号化された三角数

原文 Problem 42: Coded triangle numbers

問題の要約:添付の単語リストを読み込み単語スコアが三角数になるものの数を求めよ

Google Colabでのファイルの読み込みは「Problem 22: 名前のスコア」を参考にしてください。今回はp042_words.txtにファイルをuploadした後のコードになります。また単語の値を返す関数charcost, wordcostもそのまま使います。

# triangle numbers up to 500
tris = [n*(n+1)//2 for n in range(1,32)]
print(tris)

#---- read words from file to name[]
f = open("p042_words.txt")
string = f.read()
f.close()
names = string.replace('"','').split(',')
print(len(names),names[:10])

print(f"Answer: {[n in tris for n in [wordcost(str) for str in names]] .count(True)}") 

(開発環境:Google Colab)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0