Skip to content

Commit

Permalink
🔧 errata 追加
Browse files Browse the repository at this point in the history
  • Loading branch information
kaityo256 committed May 9, 2024
1 parent fe9c1fb commit 3f2dbe4
Show file tree
Hide file tree
Showing 5 changed files with 359 additions and 340 deletions.
8 changes: 4 additions & 4 deletions dp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ print(f"{cal} kcal")
def search_memo(n, budget):
if n < 0:
return 0
if dic[(n, budget)] is not 0: # 追加(1)
if dic[(n, budget)] != 0: # 追加(1)
return dic[(n, budget)] # 追加(1)
c1 = 0
if prices[n] <= budget:
Expand All @@ -311,7 +311,7 @@ def search_memo(n, budget):
まず、新たな終端条件として「もしすでに調べた`(n, budget)`の組み合わせなら、計算済みの値を返す」という処理を追加したのが「追加(1)」だ。

```py
if dic[(n, budget)] is not 0: # 追加(1)
if dic[(n, budget)] != 0: # 追加(1)
return dic[(n, budget)] # 追加(1)
```

Expand Down Expand Up @@ -373,9 +373,9 @@ dic[(n, budget)] = search(n-1, budget)
def get_menu(budget, cal):
menu = []
for n in reversed(range(len(names))):
if cal is 0:
if cal == 0:
break
if dic[(n, budget)] is not dic[(n - 1, budget)]:
if dic[(n, budget)] != dic[(n - 1, budget)]:
cal -= cals[n]
budget -= prices[n]
menu.append(n)
Expand Down
8 changes: 4 additions & 4 deletions dp/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ <h3 id="メモ化再帰の実装">8. メモ化再帰の実装</h3>
class="sourceCode py"><code class="sourceCode python"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> search_memo(n, budget):</span>
<span id="cb14-2"><a href="#cb14-2" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> n <span class="op">&lt;</span> <span class="dv">0</span>:</span>
<span id="cb14-3"><a href="#cb14-3" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> <span class="dv">0</span></span>
<span id="cb14-4"><a href="#cb14-4" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> dic[(n, budget)] <span class="kw">is</span> <span class="kw">not</span> <span class="dv">0</span>: <span class="co"># 追加(1)</span></span>
<span id="cb14-4"><a href="#cb14-4" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> dic[(n, budget)] <span class="op">!=</span> <span class="dv">0</span>: <span class="co"># 追加(1)</span></span>
<span id="cb14-5"><a href="#cb14-5" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> dic[(n, budget)] <span class="co"># 追加(1)</span></span>
<span id="cb14-6"><a href="#cb14-6" aria-hidden="true" tabindex="-1"></a> c1 <span class="op">=</span> <span class="dv">0</span></span>
<span id="cb14-7"><a href="#cb14-7" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> prices[n] <span class="op">&lt;=</span> budget:</span>
Expand All @@ -511,7 +511,7 @@ <h3 id="メモ化再帰の実装">8. メモ化再帰の実装</h3>
<p>先ほどの関数<code>search</code>に、3行追加しただけだ。</p>
<p>まず、新たな終端条件として「もしすでに調べた<code>(n, budget)</code>の組み合わせなら、計算済みの値を返す」という処理を追加したのが「追加(1)」だ。</p>
<div class="sourceCode" id="cb15"><pre
class="sourceCode py"><code class="sourceCode python"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> dic[(n, budget)] <span class="kw">is</span> <span class="kw">not</span> <span class="dv">0</span>: <span class="co"># 追加(1)</span></span>
class="sourceCode py"><code class="sourceCode python"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> dic[(n, budget)] <span class="op">!=</span> <span class="dv">0</span>: <span class="co"># 追加(1)</span></span>
<span id="cb15-2"><a href="#cb15-2" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> dic[(n, budget)] <span class="co"># 追加(1)</span></span></code></pre></div>
<p>ここで、<code>n</code><code>budget</code>をまとめたタプル<code>(n, budget)</code>を辞書のキーとしている。</p>
<p>もし計算したことがない組み合わせなら、そのまま計算するが、せっかく計算したので、それを最後に覚えておく(メモしておく)のが「追加(2)」だ。</p>
Expand Down Expand Up @@ -550,9 +550,9 @@ <h3 id="解の再構成get_menuの実装">10.
class="sourceCode py"><code class="sourceCode python"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> get_menu(budget, cal):</span>
<span id="cb20-2"><a href="#cb20-2" aria-hidden="true" tabindex="-1"></a> menu <span class="op">=</span> []</span>
<span id="cb20-3"><a href="#cb20-3" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span> n <span class="kw">in</span> <span class="bu">reversed</span>(<span class="bu">range</span>(<span class="bu">len</span>(names))):</span>
<span id="cb20-4"><a href="#cb20-4" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> cal <span class="kw">is</span> <span class="dv">0</span>:</span>
<span id="cb20-4"><a href="#cb20-4" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> cal <span class="op">==</span> <span class="dv">0</span>:</span>
<span id="cb20-5"><a href="#cb20-5" aria-hidden="true" tabindex="-1"></a> <span class="cf">break</span></span>
<span id="cb20-6"><a href="#cb20-6" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> dic[(n, budget)] <span class="kw">is</span> <span class="kw">not</span> dic[(n <span class="op">-</span> <span class="dv">1</span>, budget)]:</span>
<span id="cb20-6"><a href="#cb20-6" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> dic[(n, budget)] <span class="op">!=</span> dic[(n <span class="op">-</span> <span class="dv">1</span>, budget)]:</span>
<span id="cb20-7"><a href="#cb20-7" aria-hidden="true" tabindex="-1"></a> cal <span class="op">-=</span> cals[n]</span>
<span id="cb20-8"><a href="#cb20-8" aria-hidden="true" tabindex="-1"></a> budget <span class="op">-=</span> prices[n]</span>
<span id="cb20-9"><a href="#cb20-9" aria-hidden="true" tabindex="-1"></a> menu.append(n)</span>
Expand Down
4 changes: 4 additions & 0 deletions errata/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ pandocでHTMLを作成した後、tableのwidth指定を削除しなければな
| --- | --- | --- |
| p. 27| こうして「突然キレるガンジー」が誕生したのである。 | こうして「突然キレるガンジー」が誕生した、というものだ。この話は広く信じられていたが、実際にはソフトウェア的にこのようなバグは発生しないと、Civilizationの制作者、シド・マイヤー自身が否定している。 |
| p. 27| `https://note.mu/ruiu/n/n89d18450b1bb` | 参考:Sid Meier's Memoir! A Life in Computer Games |
| p. 180| 11.6.4の「8. メモ化再帰の実装」の以下のコード(2箇所)<BR> `if dic[(n, budget)] is not 0: # 追加(1)`| <BR>`if dic[(n, budget)] != 0: # 追加(1)`|
| p. 182| 11.6.5の「10. 解の再構成 get_menuの実装」の以下のコード<BR> `if calc is 0:` | <BR>`if calc == 0:`|
| p. 182| 11.6.5の「10. 解の再構成 get_menuの実装」の以下のコード<BR> `if dic[(n, budget)] is not dic[(n - 1, budget)]:` | <BR>`if dic[(n, budget)] != dic[(n - 1, budget)]:`|
| p. 224| 以下の式の右辺第二項は$a$が不要<BR>$\displaystyle\frac{dC}{da} = \sum_i^N (2 a x_i^2 - 2a x_i y_i) = 0$ | <BR>$\displaystyle\frac{dC}{da} = \sum_i^N (2 a x_i^2 - 2 x_i y_i) = 0$|

## 第3,4刷正誤表

Expand Down
Loading

0 comments on commit 3f2dbe4

Please sign in to comment.