Skip to content

Commit

Permalink
Merge pull request #14 from cdlab-sit/higurashi-takuto
Browse files Browse the repository at this point in the history
Higurashi takuto
  • Loading branch information
higurashi-takuto authored Feb 21, 2019
2 parents a083933 + 1627d77 commit 2f091f4
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions higurashi-takuto/00.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# 文字列"stressed"の文字を逆に(末尾から先頭に向かって)並べた文字列を得よ.

string = 'stressed'

# スライス[開始:終了:ステップ] 開始/終了は省略すると最初から最後まで。ステップはマイナスをつけると最後から
string_backward = string[::-1]

Expand Down
1 change: 1 addition & 0 deletions higurashi-takuto/01.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# 「パタトクカシーー」という文字列の1,3,5,7文字目を取り出して連結した文字列を得よ.

string = 'パタトクカシーー'

# スライス[開始:終了:ステップ]
string_step = string[::2]

Expand Down
12 changes: 12 additions & 0 deletions higurashi-takuto/03.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# "Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."という文を単語に分解し,各単語の(アルファベットの)文字数を先頭から出現順に並べたリストを作成せよ.

import re

string = 'Now I need a drink, alcoholic of course, '\
'after the heavy lectures involving quantum mechanics.'

# 正規表現で,.を消してスペースで分割、その後文字列の長さを見る。
string = re.sub(r'[^\w\s]', '', string)
num_char = list(map(len, string.split(' ')))

print(num_char)
25 changes: 25 additions & 0 deletions higurashi-takuto/04.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# "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文字を取り出し,取り出した文字列から単語の位置(先頭から何番目の単語か)への連想配列(辞書型もしくはマップ型)を作成せよ.

import re

string = 'Hi He Lied Because Boron Could Not Oxidize Fluorine. '\
'New Nations Might Also Sign Peace Security Clause. Arthur King Can.'

# 正規表現で,.を消してスペースで分割。
string = re.sub(r'[^\w\s]', '', string)
word = string.split(' ')

# 本当はscope = [0, 4, 5, 6, 7, 8, 14, 15, 18]の一行で18行目までは省略できる。
scope_range = [0, range(4, 9), range(14, 16), 18]
scope = []
for i in scope_range:
if isinstance(i, int):
scope.append(i)
else:
scope.extend(i)

# 辞書内包表記のif elseはkey, valueそれぞれに必要。
element2num = {_word[:1] if i in scope else _word[:2]: i + 1
for i, _word in enumerate(word)}

print(element2num)

0 comments on commit 2f091f4

Please sign in to comment.