1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【初心者】pythonで0から Project Euler を解いてみた22

Posted at

今回はEuler22問目で―す。
http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2022
「名前のスコア」
まず。問題を見たときに解ける気がしなった。
でも初心者にとってこんなもの無理やろ!!と思ったけど分解して考えて一つずつ進めていったら出来たのでくじけず頑張りましょう!!
#コード

names_deta = open('names.txt')
names = sorted(names_deta.read().replace('"','').split(','),key=str)
alp_num = {"A":1,"B":2,"C":3,"D":4,"E":5,"F":6,"G":7,"H":8,"I":9,"J":10,"K":11,"L":12,"M":13,"N":14,"O":15,"P":16,"Q":17,"R":18,"S":19,"T":20,"U":21,"V":22,"W":23,"X":24,"Y":25,"Z":26}
for i in range(len(names)):
    names[i] = list(names[i])
scoer = 0
for cha_No in range(len(names)):
    for cha_sc in range(len(names[cha_No])):
        tenp = 0
        tenp += alp_num[names[cha_No][cha_sc]]
        scoer += tenp * (cha_No + 1)
print(scoer)

まず一行でtxtを読み込み二行目で("")を取り除き、(,)で一つ一つをリストの要素にします次にalp_numでアルファベットに数値を関連付けておきます。
次のfor文で二次元配列にします
次は二重ループを使い何番目なのか、そして名前のtenpはいくつになるのかを計算して頂きます
それによって出たtenpをscoerに足していき最後に出力させて完成です

こんな感じにやってみると案外解けましたね
皆様もぜひ!w

1
0
1

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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?