LoginSignup
3
2

More than 5 years have passed since last update.

Pythonで言語処理100本ノック2015 問題04

Last updated at Posted at 2017-11-21

Today's Question

問題の出典 → 言語処理100本ノック
今回は5本目

問題04
Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can.
という文を単語に分解し,1, 5, 6, 7, 8, 9, 15, 16, 19番目の単語は先頭の1文字,それ以外の単語は先頭に2文字を取り出し,
取り出した文字列から単語の位置(先頭から何番目の単語か)への連想配列(辞書型もしくはマップ型)を作成せよ.

おっと、12番目の単語がMightになっているので
そのまま実装すると

'Mi': 12

のようになってしまうため i は削除してMghtで進めます

いざ実装

python3
"""
戦略
・条件指定されたナンバーのリストを定義し、分岐判定に使う
"""

s = "Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Mght Also Sign Peace Security Clause. Arthur King Can."
s_list = s.split()

condition_list = [1, 5, 6, 7, 8, 9, 15, 16, 19]
answer = {}
i = 0

#文字数をカウントし出現順に配列に格納する
for n in s_list:
    i += 1

    if i in condition_list:
        answer[n[:1]] = (i)
    else:
        answer[n[:2]] = (i)

print(answer)
実行結果
{'Li': 3, 'Ar': 18, 'Na': 11, 'S': 16, 'Ne': 10, 'F': 9, 'Be': 4, 'Si': 14, 'C': 6, 'Ca': 20, 'H': 1, 'P': 15, 'He': 2, 'O': 8, 'Mg': 12, 'N': 7, 'B': 5, 'Al': 13, 'Cl': 17, 'K': 19}

感想

・ハッシュのままソートできたら良い感じなのにな
・部屋の乾燥がひどく加湿器が手放せない

3
2
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
3
2