Memorandum

Python勉強中 基本的に覚書

Python プログラミング練習 2日目

言語処理100本ノック 2015

  1. 円周率 "Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."という文を単語に分解し,各単語の(アルファベットの)文字数を先頭から出現順に並べたリストを作成せよ.
#! /usr/bin/env python
# encoding:utf-8

elm = "Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."
temp = elm.split()
line = []

for i in range(len(temp)):
    word = temp[i].rstrip(',' or '.')
    line.append(len(word))

print line
  1. 元素記号 "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文字を取り出し,取り出した文字列から単語の位置(先頭から何番目の単語か)への連想配列(辞書型もしくはマップ型)を作成せよ.
#! /usr/bin/env python
# encoding:utf-8

# 入力
text = "Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can."

dic         = {}
words_list  = text.split()
uniq        = [1, 5, 6, 7, 8, 9, 15, 16, 19]

for i in range(len(words_list)):
    num = i + 1
    if num in uniq:
        dic[num] = words_list[i][0:1]
    else:
        dic[num] = words_list[i][0:2]

for k in sorted(dic):
    print k, dic[k]


print str(len(words_list))

出力結果が間違っていた場合は 後で修正

100問中、5問終了 飛ばした問題無し

モッピー!お金がたまるポイントサイト