File tree Expand file tree Collapse file tree 5 files changed +63
-23
lines changed Expand file tree Collapse file tree 5 files changed +63
-23
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ sidebar_position: 8
3
3
---
4
4
5
5
import ViewSource from " @site/src/components/ViewSource" ;
6
+ import Hint from " @site/src/components/Hint" ;
6
7
import Answer from " @site/src/components/Answer" ;
7
8
8
9
# 条件分岐
@@ -143,6 +144,20 @@ else:
143
144
144
145
絶対値を求めるプログラムを作ってみましょう。
145
146
147
+ <Hint >
148
+
149
+ 次の式を思い起こせば、作れそうです。
150
+
151
+ $$
152
+ |x|=
153
+ \begin{dcases}
154
+ x & \text{if $x\geq 0$,} \\
155
+ -x & \text{else.}
156
+ \end{dcases}
157
+ $$
158
+
159
+ </Hint >
160
+
146
161
<Answer >
147
162
<ViewSource path = " /if/abs.ipynb" />
148
163
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ sidebar_position: 10
3
3
---
4
4
5
5
import ViewSource from " @site/src/components/ViewSource" ;
6
+ import Hint from " @site/src/components/Hint" ;
6
7
import Answer from " @site/src/components/Answer" ;
7
8
8
9
# 配列
@@ -92,6 +93,16 @@ len(配列名)
92
93
生徒の英語の点数が書かれた配列を受け取って、その平均点を返す関数を作ってみましょう。
93
94
実際に、点数をそれぞれ 26 点、78 点、83 点、20 点、10 点、11 点、22 点、16 点、41 点、95 点として計算してみましょう。
94
95
96
+ <Hint >
97
+
98
+ $n$ 個のデータを $x_1,x_2,\dots,x_n$、平均点を $\bar{ x } $ とすると、次のようにして求められるのでした。
99
+
100
+ $$
101
+ \bar{x}=\frac{x_1+x_2+\dots +x_n}{n}=\frac{\sum_{i=1}^n x_i}{n}
102
+ $$
103
+
104
+ </Hint >
105
+
95
106
<Answer >
96
107
<ViewSource path = " /array/average4.ipynb" />
97
108
@@ -106,7 +117,7 @@ len(配列名)
106
117
107
118
今度は、分散を求めるプログラムを書いてみましょう。
108
119
109
- $n$ 個の観測データを $x_1,x_2,\cdots ,x_n$、平均を $\bar{ x } $ とすると、分散 $s^2$ は次のように与えられるとします。
120
+ $n$ 個のデータを $x_1,x_2,\dots ,x_n$、平均を $\bar{ x } $ とすると、分散 $s^2$ は次のように与えられるとします。
110
121
111
122
$$
112
123
s^2=\frac{\sum_{i=1}^n(x_i-\bar{x})^2}{n}
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ sidebar_position: 11
3
3
---
4
4
5
5
import ViewSource from " @site/src/components/ViewSource" ;
6
+ import Hint from " @site/src/components/Hint" ;
6
7
import Answer from " @site/src/components/Answer" ;
7
8
8
9
# 多次元配列
@@ -57,6 +58,12 @@ A、B、C の 3 人の生徒がいて、それぞれの国語、数学、英語
57
58
| ** B** | 73 | 53 | 84 |
58
59
| ** C** | 63 | 48 | 64 |
59
60
61
+ <Hint >
62
+
63
+ まずそれぞれの生徒の得点の合計を求める関数を作ってから、その最高点を求める関数を作ると良いでしょう。
64
+
65
+ </Hint >
66
+
60
67
<Answer >
61
68
<ViewSource path = " /multi-array/total_score_max.ipynb" />
62
69
</Answer >
Original file line number Diff line number Diff line change @@ -74,8 +74,35 @@ $n$ 番目まで Fizz Buzz での正しい解を表示するプログラムを
74
74
75
75
最大公約数を求めるプログラムを作ってみましょう。最大公約数(greatest common divisor)は、GCD とよく略されます。
76
76
77
+ <Hint >
78
+
79
+ ユークリッドの互除法を用いれば簡単にできます。
80
+
81
+ ユークリッドの互除法を用いると、次のように最大公約数が計算できるのでした。
82
+ $\mathrm{ gcd } (a, b)$ を $a$ と $b$ の最大公約数とします。
83
+
84
+ 例:$30$ と $18$ の最大公約数を求める。
85
+
86
+ $$
87
+ \begin{align*}
88
+ \mathrm{gcd}(30, 18) &= \mathrm{gcd}(18, 30 - 18\times 1) \\
89
+ &= \mathrm{gcd}(18, 12) \\
90
+ &= \mathrm{gcd}(12, 18 - 12\times 1) \\
91
+ &= \mathrm{gcd}(12, 6) \\
92
+ &= \mathrm{gcd}(6, 12 - 6\times 2) \\
93
+ &= \mathrm{gcd}(6, 0) \\
94
+ &= 6
95
+ \end{align*}
96
+ $$
97
+
98
+ よって、最大公約数は $6$
99
+
100
+ </Hint >
101
+
77
102
<Answer >
78
103
104
+ ユークリッドの互除法を使うと、最大公約数を求めるプログラムは次のようになります。
105
+
79
106
<ViewSource path = " /practice/gcd.ipynb" />
80
107
81
108
` a, b = b, a ` は、次と同じ意味です。
86
113
b = tmp
87
114
```
88
115
89
- 再帰を使うと次のようにもできます。再帰は後の項で説明します 。
116
+ 再帰を使うと次のようにもできます。再帰を使ったプログラムは、後の項で紹介します 。
90
117
91
118
<ViewSource path = " /practice/gcd_recursive.ipynb" />
92
119
Original file line number Diff line number Diff line change 214
214
215
215
<Answer >
216
216
217
- ユークリッドの互除法を用いれば簡単にできます。$\mathrm{ gcd } (a, b)$ を $a$ と $b$ の最大公約数とします。
218
-
219
- 例:$30$ と $18$ の最大公約数を求める。
220
-
221
- $$
222
- \begin{align*}
223
- \mathrm{gcd}(30, 18) &= \mathrm{gcd}(18, 30 - 18\times 1) \\
224
- &= \mathrm{gcd}(18, 12) \\
225
- &= \mathrm{gcd}(12, 18 - 12\times 1) \\
226
- &= \mathrm{gcd}(12, 6) \\
227
- &= \mathrm{gcd}(6, 12 - 6\times 2) \\
228
- &= \mathrm{gcd}(6, 0) \\
229
- &= 6
230
- \end{align*}
231
- $$
232
-
233
- よって、最大公約数は $6$
234
-
235
- これを使うと、最大公約数を求めるプログラムは次のようになります。
236
-
237
217
<ViewSource path = " /recursion/gcd.ipynb" />
238
218
239
219
:::note
240
220
241
221
このプログラムは、はじめの引数が $a < b$ の場合でも動きます。一度目の再帰で、` gcd(b, a % b) ` = ` gcd(b, a) ` が返されるからです。
242
222
243
- つまり、$18$ と $30$ の最大公約数を求めるとき、
223
+ つまり、$18$ と $30$ の最大公約数を求めるとき、$\mathrm { gcd } (a, b)$ を $a$ と $b$ の最大公約数とすると、
244
224
245
225
$$
246
226
\begin{align*}
You can’t perform that action at this time.
0 commit comments