文字列"stressed"の文字を逆に(末尾から先頭に向かって)並べた文字列を得よ
s = 'stressed'
rev = s[::-1]
print(rev)
desserts
「パタトクカシーー」という文字列の1,3,5,7文字目を取り出して連結した文字列を得よ.
s = 'パタトクカシーー'
s1357 = s[1-1] + s[3-1] + s[5-1] + s[7-1]
print(s1357)
パトカー
s = 'パタトクカシーー'
s1357 = s[::2]
print(s1357)
パトカー
「パトカー」+「タクシー」の文字を先頭から交互に連結して文字列「パタトクカシーー」を得よ.
s1 = 'パトカー'
s2 = 'タクシー'
s = ''.join([p + t for p, t in zip(s1, s2)])
print(s)
パタトクカシーー
"Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."という文を単語に分解し,各単語の(アルファベットの)文字数を先頭から出現順に並べたリストを作成せよ.
import re
sentense = 'Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics.'
words = re.sub(r'[.|,]', '', sentense).split()
counts = [len(w) for w in words]
print(counts)
[3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9]
"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文字を取り出し,取り出した文字列から単語の位置(先頭から何番目の単語か)への連想配列(辞書型もしくはマップ型)を作成せよ.
sentense = "Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can."
words = re.sub(r'[.|,]', '', sentense).split()
idx_list_first = [1, 5, 6, 7, 8, 9, 15, 16, 19]
# 参考:http://qiita.com/tanaka0325/items/08831b96b684d7ecb2f7
dic = {w[:2 - int(i in idx_list_first)]:i for i, w in enumerate(words, 1)}
dic
{'Al': 13, 'Ar': 18, 'B': 5, 'Be': 4, 'C': 6, 'Ca': 20, 'Cl': 17, 'F': 9, 'H': 1, 'He': 2, 'K': 19, 'Li': 3, 'Mi': 12, 'N': 7, 'Na': 11, 'Ne': 10, 'O': 8, 'P': 15, 'S': 16, 'Si': 14}
与えられたシーケンス(文字列やリストなど)からn-gramを作る関数を作成せよ.この関数を用い,"I am an NLPer"という文から単語bi-gram,文字bi-gramを得よ.
def n_gram(_in, n):
return [_in[i:i+n] for i in range(len(_in)) if len(_in[i:i+n]) >= n]
s = "I am an NLPer"
# 単語bigram
n_gram(s.split(), 2)
[['I', 'am'], ['am', 'an'], ['an', 'NLPer']]
# 文字bi-gram
n_gram(s, 2)
['I ', ' a', 'am', 'm ', ' a', 'an', 'n ', ' N', 'NL', 'LP', 'Pe', 'er']
print('mono-gram(word)', n_gram(s.split(), 1))
print('mono-gram(str)', n_gram(s, 1))
mono-gram(word) [['I'], ['am'], ['an'], ['NLPer']] mono-gram(str) ['I', ' ', 'a', 'm', ' ', 'a', 'n', ' ', 'N', 'L', 'P', 'e', 'r']
print('tri-gram(word)', n_gram(s.split(), 3))
print('tri-gram(str)', n_gram(s, 3))
tri-gram(word) [['I', 'am', 'an'], ['am', 'an', 'NLPer']] tri-gram(str) ['I a', ' am', 'am ', 'm a', ' an', 'an ', 'n N', ' NL', 'NLP', 'LPe', 'Per']
"paraparaparadise"と"paragraph"に含まれる文字bi-gramの集合を,それぞれ, XとYとして求め,XとYの和集合,積集合,差集合を求めよ.さらに,'se'というbi-gramがXおよびYに含まれるかどうかを調べよ.
s1 = "paraparaparadise"
s2 = "paragraph"
X = set(n_gram(s1, 2))
print(X)
Y = set(n_gram(s2, 2))
print(Y)
{'se', 'ap', 'ra', 'di', 'is', 'ar', 'pa', 'ad'} {'ap', 'ag', 'ph', 'ra', 'gr', 'ar', 'pa'}
# 和集合
union = X.union(Y)
print(union)
{'se', 'ap', 'ag', 'ph', 'ra', 'gr', 'di', 'is', 'ar', 'pa', 'ad'}
# 積集合
intersec = X.intersection(Y)
print(intersec)
{'ap', 'ra', 'ar', 'pa'}
# 差集合
diff_X_Y = X.difference(Y)
print(diff_X_Y)
diff_Y_X = Y.difference(X)
print(diff_Y_X)
{'di', 'ad', 'se', 'is'} {'ag', 'ph', 'gr'}
'se' in X
True
'se' in Y
False
引数x, y, zを受け取り「x時のyはz」という文字列を返す関数を実装せよ.さらに,x=12, y="気温", z=22.4として,実行結果を確認せよ.
def create_sentense_temp(x, y, z):
return '{}時の{}は{}'.format(x, y, z)
create_sentense_temp(12, '気温', 22.4)
'12時の気温は22.4'
与えられた文字列の各文字を,以下の仕様で変換する関数cipherを実装せよ.
英小文字ならば(219 - 文字コード)の文字に置換 その他の文字はそのまま出力 この関数を用い,英語のメッセージを暗号化・復号化せよ.
s='aあ'
s.islower()
True
def cipher(s:str):
# chr: アスキーコードから文字へ
# 219-ord: 文字から219アスキーコードへ
return ''.join([chr(219-ord(c)) if 'a' <= c <= 'z' else c for c in s])
cipher('Hello World')
'Hvool Wliow'
cipher('abcdefghijkelomnopqrstuvwxyz')
'zyxwvutsrqpvolnmlkjihgfedcba'
cipher(cipher('Hello World'))
'Hello World'
スペースで区切られた単語列に対して,各単語の先頭と末尾の文字は残し,それ以外の文字の順序をランダムに並び替えるプログラムを作成せよ.ただし,長さが4以下の単語は並び替えないこととする.適当な英語の文(例えば"I couldn't believe that I could actually understand what I was reading : the phenomenal power of the human mind .")を与え,その実行結果を確認せよ.
import re
import random
s = "I couldn't believe that I could actually understand what I was reading : the phenomenal power of the human mind ."
def typoglycemia(not_sort_word_length=4):
def typo(s):
words = re.sub(r'[.|,|:]', '', s).split()
return [w[0] + ''.join(random.sample(w[1:-1], len(w[1:-1]))) + w[-1] if len(w) > not_sort_word_length else w for w in words]
return typo
# [w[0] + random.shuffle(list(w[1:-2])) + w[-1] for w in words if len(w) > 4]
typo = typoglycemia(not_sort_word_length=4)
t = typo(s)
' '.join(t)
"I culod'nt beeilve that I culod aultclay unrsetadnd what I was raeding the peonhaemnl peowr of the hmaun mind"
# random shuffleは返り値なし
s = 'abcdefg'
l = list(s)
random.shuffle(l)
l
['b', 'g', 'f', 'e', 'c', 'a', 'd']