00. 文字列の逆順
文字列”stressed”の文字を逆に(末尾から先頭に向かって)並べた文字列を得よ.
reverse("stressed")
"desserts"
"stressed"[end:-1:1]
"desserts"
01. 「パタトクカシーー」
「パタトクカシーー」という文字列の1,3,5,7文字目を取り出して連結した文字列を得よ.
"パタトクカシーー"[1:6:19]
"パトカー"
02. 「パトカー」+「タクシー」=「パタトクカシーー」
「パトカー」+「タクシー」の文字を先頭から交互に連結して文字列「パタトクカシーー」を得よ.
join([join([a, b]) for (a, b) in zip("パトカー", "タクシー")])
"パタトクカシーー"
03. 円周率
“Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics.”という文を単語に分解し,各単語の(アルファベットの)文字数を先頭から出現順に並べたリストを作成せよ.
str = "Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."
[length(s) for s in split(replace(str, r"[,.]"=>""))]
15-element Vector{Int64}:
3
1
4
1
5
9
2
6
5
3
5
8
9
7
9
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文字を取り出し,取り出した文字列から単語の位置(先頭から何番目の単語か)への連想配列(辞書型もしくはマップ型)を作成せよ.
str = "Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can."
s = [s[1:2-(i in Set([1, 5, 6, 7, 8, 9, 15, 16, 19]))] for (i, s) in enumerate(split(replace(str, "."=>"")))]
Dict(k for k = zip(s, 1:20))
Dict{SubString{String}, Int64} with 20 entries:
"Na" => 11
"Cl" => 17
"Si" => 14
"C" => 6
"P" => 15
"Ne" => 10
"Li" => 3
"O" => 8
"Al" => 13
"B" => 5
"S" => 16
"Ar" => 18
"N" => 7
"Mi" => 12
"Ca" => 20
"He" => 2
"Be" => 4
"K" => 19
"H" => 1
"F" => 9
05. n-gram
与えられたシーケンス(文字列やリストなど)からn-gramを作る関数を作成せよ.この関数を用い,”I am an NLPer”という文から単語bi-gram,文字bi-gramを得よ.
str = "I am an NLPer"
s = split(str)
[s[i:i+1] for i in 1:length(s)-1]
3-element Vector{Vector{SubString{String}}}:
["I", "am"]
["am", "an"]
["an", "NLPer"]
c = replace(str, " "=>"")
[c[i:i+1] for i in 1:length(c)-1]
9-element Vector{String}:
"Ia"
"am"
"ma"
"an"
"nN"
"NL"
"LP"
"Pe"
"er"
06. 集合
“paraparaparadise”と”paragraph”に含まれる文字bi-gramの集合を,それぞれ, XとYとして求め,XとYの和集合,積集合,差集合を求めよ.さらに,’se’というbi-gramがXおよびYに含まれるかどうかを調べよ.
x = "paraparaparadise"
X = [x[i:i+1] for i in 1:length(x)-1]
y = "paragraph"
Y = [y[i:i+1] for i in 1:length(y)-1];
union(X, Y)
11-element Vector{String}:
"pa"
"ar"
"ra"
"ap"
"ad"
"di"
"is"
"se"
"ag"
"gr"
"ph"
intersect(X, Y)
4-element Vector{String}:
"pa"
"ar"
"ra"
"ap"
setdiff(X, Y)
4-element Vector{String}:
"ad"
"di"
"is"
"se"
"se" in X
true
"se" in Y
false
07. テンプレートによる文生成
引数x, y, zを受け取り「x時のyはz」という文字列を返す関数を実装せよ.さらに,x=12, y=”気温”, z=22.4として,実行結果を確認せよ.
template(x, y, z) = "$(x)時の$(y)は$(z)"
template (generic function with 1 method)
print(template(12, "気温", 22.4))
12時の気温は22.4
08. 暗号文
与えられた文字列の各文字を,以下の仕様で変換する関数 cipher を実装せよ。
- 英小文字ならば (219 - 文字コード) の文字に置換
- その他の文字はそのまま出力
この関数を用い,英語のメッセージを暗号化・復号化せよ。
cipher(c) = islowercase(c) ? Char(219 - Int(c)) : c
str = "Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."
a = join(cipher(c) for c in str)
"Nld I mvvw z wirmp, zoxlslorx lu xlfihv, zugvi gsv svzeb ovxgfivh rmeloermt jfzmgfn nvxszmrxh."
join(cipher(c) for c in a)
"Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."
09. Typoglycemia
スペースで区切られた単語列に対して,各単語の先頭と末尾の文字は残し,それ以外の文字の順序をランダムに並び替えるプログラムを作成せよ.ただし,長さが4以下の単語は並び替えないこととする.適当な英語の文(例えば”I couldn’t believe that I could actually understand what I was reading : the phenomenal power of the human mind .”)を与え,その実行結果を確認せよ.
str = "I couldn't believe that I could actually understand what I was reading : the phenomenal power of the human mind ."
using Random
join([(length(s) <= 4) ? s : join([s[1], join(shuffle(split(s[2:end-1], ""))), s[end]]) for s in split(str)], " ")
"I cu'lndot bieleve that I culod acuatlly uaedrstnnd what I was riaendg : the pahmoeennl peowr of the hmaun mind ."