2020-06-20 課題

メモ:先に進む前に録画してあるか確認しよう

自分用メモ

Matplotlib

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-4, 4, 201)
y1 = x**2 - 2 * x + 1
y2 = x**3 + 2 * x + 3
y3 = x**4 - x**2 - 2 * x + 3

plt.plot(x, y1, label="y1")
plt.plot(x, y2, label="y2")
plt.plot(x, y3, label="y3")

plt.grid()
plt.legend()
#plt.axes().set_aspect('equal', 'datalim') # アスペクト比を合わせる
plt.show()

自然言語処理100本ノック

第1章: 準備運動 00. 文字列の逆順

文字列"stressed"の文字を逆に(末尾から先頭に向かって)並べた文字列を得よ.

1
2
print("".join(list(reversed("stressed"))))
print("stressed"[::-1])
1
2
desserts
desserts
1
reversed("stressed")
1
<reversed at 0x7fddcfffd5f8>
1
"".join(reversed("stressed"))
1
'desserts'
1
list(reversed("stressed"))
1
['d', 'e', 's', 's', 'e', 'r', 't', 's']
1
"|".join(list(reversed("stressed")))
1
'd|e|s|s|e|r|t|s'
1
"stressed"[::2]
1
'srse'

第1章: 準備運動 01. 「パタトクカシーー」

  • 「パタトクカシーー」という文字列の1,3,5,7文字目を取り出して連結した文字列を得よ.
1
print("パタトクカシーー"[1::2])
1
タクシー
1
print("パタトクカシーー"[0::2])
1
パトカー

02. 「パトカー」+「タクシー」=「パタトクカシーー」

「パトカー」+「タクシー」の文字を先頭から交互に連結して文字列「パタトクカシーー」を得よ.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
s1 = "パトカー"
s2 = "タクシー"


print("".join([s1[i] + s2[i] for i in range(4)]))


s = ""
for i in range(4):
    print(s)
    s = s + s1[i]+ s2[i]
print(s)
1
2
3
4
5
6
パタトクカシーー

パタ
パタトク
パタトクカシ
パタトクカシーー

03 円周率

"Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."という文を単語に分解し,各単語の(アルファベットの)文字数を先頭から出現順に並べたリストを作成せよ.

1
2
s = "Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."
print(list(map(lambda x: len(x), s.split())))
1
[3, 1, 4, 1, 6, 9, 2, 7, 5, 3, 5, 8, 9, 7, 9]
1
2
s = "Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."
s.split()
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
['Now',
 'I',
 'need',
 'a',
 'drink,',
 'alcoholic',
 'of',
 'course,',
 'after',
 'the',
 'heavy',
 'lectures',
 'involving',
 'quantum',
 'mechanics.']
1
2
s = "Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."
print(list(map(lambda x: len(x), s.replace(",", "").replace(".", "").split())))
1
[3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9]
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
s = "Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."
print(s.replace(",", ""))
print(s.replace(",", "").replace(".", ""))
print(s.replace(",", "").replace(".", "").split())

xs = []
for x in s.replace(",", "").replace(".", "").split():
    print(len(x))
    xs.append(len(x))
print(xs)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
Now I need a drink alcoholic of course after the heavy lectures involving quantum mechanics.
Now I need a drink alcoholic of course after the heavy lectures involving quantum mechanics
['Now', 'I', 'need', 'a', 'drink', 'alcoholic', 'of', 'course', 'after', 'the', 'heavy', 'lectures', 'involving', 'quantum', 'mechanics']
3
1
4
1
5
9
2
6
5
3
5
8
9
7
9
[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文字を取り出し,取り出した文字列から単語の位置(先頭から何番目の単語か)への連想配列(辞書型もしくはマップ型)を作成せよ.

(言っていることがよくわからなくて何度も読み返した。)

1
2
3
4
5
6
7
8
9
numbers = [1,5,6,7,8,9,15,16,19]
numbers = list(map(lambda x: x - 1, numbers))

def f(i,v):
    return v[0] if i in numbers else v[0:2]

s = "Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can."
s = s.replace(",", "").replace(".", "").split()
print({f(i,v): i for i, v in enumerate(s)})
1
{'H': 0, 'He': 1, 'Li': 2, 'Be': 3, 'B': 4, 'C': 5, 'N': 6, 'O': 7, 'F': 8, 'Ne': 9, 'Na': 10, 'Mi': 11, 'Al': 12, 'Si': 13, 'P': 14, 'S': 15, 'Cl': 16, 'Ar': 17, 'K': 18, 'Ca': 19}
1
2
3
4
5
s = ""
if i in numbers:
    s = v[0]
else:
    s = v[0:2]

内包表記をループで書き直した

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
numbers = [1,5,6,7,8,9,15,16,19]
numbers = list(map(lambda x: x - 1, numbers))

def f(i,v):
    return v[0] if i in numbers else v[0:2]

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

dic = {}
for i,v in enumerate(s):
    dic[f(i,v)] = i

print(dic)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
0
Hi

1
He

2
Lied

3
Because

4
Boron

5
Could

6
Not

7
Oxidize

8
Fluorine

9
New

10
Nations

11
Might

12
Also

13
Sign

14
Peace

15
Security

16
Clause

17
Arthur

18
King

19
Can

{'H': 0, 'He': 1, 'Li': 2, 'Be': 3, 'B': 4, 'C': 5, 'N': 6, 'O': 7, 'F': 8, 'Ne': 9, 'Na': 10, 'Mi': 11, 'Al': 12, 'Si': 13, 'P': 14, 'S': 15, 'Cl': 16, 'Ar': 17, 'K': 18, 'Ca': 19}

プログラミングの一般論

web システムの事例

データ構造とアルゴリズム

(連結)リストと配列

リストの特徴

配列の特徴

ベクター(参考