From 5abbe402afbb2158d5bac102f134c9b30876185d Mon Sep 17 00:00:00 2001 From: Xiangzhuang Shen Date: Thu, 5 Oct 2023 16:39:35 +0800 Subject: [PATCH] Changed:use markdown-exec to run python code --- docs/fifty/1_the_sock_drawer.md | 38 ++++----------------- docs/fifty/2_successive_wins.md | 41 ++++++----------------- docs/fifty/snippet/1_the_sock_drawer.py | 8 +++-- docs/fifty/snippet/2_successive_wins.py | 4 +++ mkdocs.yml | 1 + poetry.lock | 44 ++++++++++++++++++++++++- pyproject.toml | 1 + 7 files changed, 71 insertions(+), 66 deletions(-) diff --git a/docs/fifty/1_the_sock_drawer.md b/docs/fifty/1_the_sock_drawer.md index 4be634f..ef408ce 100644 --- a/docs/fifty/1_the_sock_drawer.md +++ b/docs/fifty/1_the_sock_drawer.md @@ -86,18 +86,11 @@ We can find values that satisfy these conditions through an iterative approach: - ```python + ```python exec="true" source="material-block" session="fifty-1" --8<-- "docs/fifty/snippet/1_the_sock_drawer.py:solution1" ``` - Running the above code gives us: - - ``` - R = 3, N = 4, B = N - R = 1 - R = 15, N = 21, B = N - R = 6 - ``` - - Obtaining the final answers:: + Obtaining the final answers: (a): At least 4 socks are needed (3 red and 1 black), in this case, we have $\frac{R}{N}\frac{R-1}{N-1} = \frac{3}{4}\frac{2}{3} = \frac{1}{2}$ @@ -143,16 +136,10 @@ 可以通过遍历的方法来求出找出符合条件的值: - ```python + ```python exec="true" source="material-block" session="fifty-1" --8<-- "docs/fifty/snippet/1_the_sock_drawer.py:solution1" ``` - 运行上述代码可以得到: - ``` - R = 3, N = 4, B = N - R = 1 - R = 15, N = 21, B = N - R = 6 - ``` - 得到最终答案: (a): 最少有4只袜子(3红1黑),此时有$\frac{R}{N}\frac{R-1}{N-1} = \frac{3}{4}\frac{2}{3} = \frac{1}{2}$ @@ -170,18 +157,11 @@ We can also make full use of the powerful computing power of the computer to directly solve this problem. Let’s take a look at the "violent aesthetics" of computers! - ```python + ```python exec="true" source="material-block" session="fifty-1" --8<-- "docs/fifty/snippet/1_the_sock_drawer.py:solution2" ``` - Running the above code gives us: - - ``` - R = 3, N = 4, B = N - R = 1 - R = 15, N = 21, B = N - R = 6 - ``` - - Obtaining the final answers:: + Obtaining the final answers: (a): At least 4 socks are needed (3 red and 1 black), in this case, we have $\frac{R}{N}\frac{R-1}{N-1} = \frac{3}{4}\frac{2}{3} = \frac{1}{2}$ @@ -195,16 +175,10 @@ 上面的方法是先分析简化问题之后再求解,我们也可以充分利用计算机强大的计算能力来直接求解本问题。 让我们一起来看下计算机的“暴力美学”吧! - ```python + ```python exec="true" source="material-block" session="fifty-1" --8<-- "docs/fifty/snippet/1_the_sock_drawer.py:solution2" ``` - 运行上述代码同样可以得到: - ``` - R = 3, N = 4, B = N - R = 1 - R = 15, N = 21, B = N - R = 6 - ``` - 得到相同的答案: (a): 最少有4只袜子(3红1黑),此时有$\frac{R}{N}\frac{R-1}{N-1} = \frac{3}{4}\frac{2}{3} = \frac{1}{2}$ diff --git a/docs/fifty/2_successive_wins.md b/docs/fifty/2_successive_wins.md index a8a8a0c..c1bd5f3 100644 --- a/docs/fifty/2_successive_wins.md +++ b/docs/fifty/2_successive_wins.md @@ -37,7 +37,7 @@ ## Solutions -### Solution1 +### Solution1: Analysis ??? success "Solution1: Analysis" @@ -62,7 +62,7 @@ The probability of Elmer winning the reward in this case is: - $fcf + fc(1-f) + (1-f)cf = fc(2-f)$ + $$fcf + fc(1-f) + (1-f)cf = fc(2-f)$$ Similarly, if we consider the sequence CFC, Elmer can win the reward in the following three cases: @@ -140,7 +140,7 @@ 即Elmer选择CFC的顺序比赛赢得奖励的概率更高 -### Solution2 +### Solution2: Simulation ??? success "Solution2: Simulation" @@ -149,29 +149,19 @@ We can simulate the CFC and FCF sequences using the following approach: - ```python + ```python exec="true" source="material-block" session="fifty-2" --8<-- "docs/fifty/snippet/2_successive_wins.py:solution2" ``` - Running `simulation()` produces the following output - (since the probabilities `f` and `c` for winning against the father and the champion are randomly generated using `get_prior_prob`, the results may vary for each simulation. However, we can observe that the probability for the CFC sequence is slightly higher): + Since the probabilities `f` and `c` for winning against the father and the champion are randomly generated using `get_prior_prob`, the results may vary for each simulation. However, we can observe that the probability for the CFC sequence is slightly higher. - ```python - FCF: 0.4759, CFC: 0.6061 - ``` If we carefully analyze this simulation experiment, we can identify a limitation: it only simulates one pair of `f` and `c` probabilities. Therefore, the higher probability for CFC compared to FCF might be due to chance. To validate if CFC consistently has a higher probability than FCF, we need to simulate different configurations of `f` and `c`. - ```python + ```python exec="true" source="material-block" session="fifty-2" --8<-- "docs/fifty/snippet/2_successive_wins.py:solution2-extend" ``` - Running `simulation_extend()` produces the following output: - - ```python - (CFC win prob > FCF win prob)'s prob: 1.0 - ``` - Hence, we have sufficient data to conclude that the probability of CFC being greater than FCF is consistent. @@ -179,30 +169,19 @@ 可以通过下面的方式进行CFC,FCF两个模式的比赛模拟: - ```python + ```python exec="true" source="material-block" session="fifty-2" --8<-- "docs/fifty/snippet/2_successive_wins.py:solution2" ``` - 运行`simulation()`可以得到如下输出 - (因为这里赢得父亲和冠军的概率`f`, `c`是通过`get_prior_prob`随机生成, - 所以每次模拟的结果会不相同。但是都能够看到CFC顺序的概率更高一些): - - ```python - FCF: 0.4759, CFC: 0.6061 - ``` + 因为这里赢得父亲和冠军的概率`f`, `c`是通过`get_prior_prob`随机生成, + 所以每次模拟的结果会不相同。但是都能够看到CFC顺序的概率更高一些. 如果我们仔细思考这个模拟实验就会发现其中还有不足的地方,那就是这里只模拟了一对 `f`, `c`概率下的情况。那么这里模拟得到的CFC的概率大于FCF的概率不排除是偶然的原因。 所以我们需要模拟在不同`f`, `c`配置下是否都有CFC的概率大于FCF的概率。 - ```python + ```python exec="true" source="material-block" session="fifty-2" --8<-- "docs/fifty/snippet/2_successive_wins.py:solution2-extend" ``` - 运行`simulation_extend()`可以得到如下输出 - - ```python - (CFC win prob > FCF win prob)'s prob: 1.0 - ``` - 至此,我们有充足的数据说明CFC的概率大于FCF的概率。 diff --git a/docs/fifty/snippet/1_the_sock_drawer.py b/docs/fifty/snippet/1_the_sock_drawer.py index ec8acfe..fe37c26 100644 --- a/docs/fifty/snippet/1_the_sock_drawer.py +++ b/docs/fifty/snippet/1_the_sock_drawer.py @@ -18,9 +18,11 @@ def find_numbers(r_limit: int = 50) -> None: under_sqrt = 1 - 8 * r * (1 - r) if is_square(under_sqrt) and is_odd(int(math.sqrt(under_sqrt))): n = int((1 + int(math.sqrt(under_sqrt))) / 2) - print(f"R = {r}, N = {n}, B = N - R = {n - r}") + print(f"R = {r}, N = {n}, B = N - R = {n - r}\n") +find_numbers() + # --8<-- [end:solution1] @@ -33,7 +35,9 @@ def simulation(r_limit: int = 50, n_limit: int = 50) -> None: for r in range(1, r_limit): for n in range(r + 1, n_limit): if is_close(1 / 2, (r / n) * ((r - 1) / (n - 1))): - print(f"R = {r}, N = {n}, B = N - R = {n - r}") + print(f"R = {r}, N = {n}, B = N - R = {n - r}\n") + +simulation() # --8<-- [end:solution2] diff --git a/docs/fifty/snippet/2_successive_wins.py b/docs/fifty/snippet/2_successive_wins.py index 2429fda..fcfbba7 100644 --- a/docs/fifty/snippet/2_successive_wins.py +++ b/docs/fifty/snippet/2_successive_wins.py @@ -40,6 +40,8 @@ def simulation(run_num: int = 10000) -> None: print(f"FCF: {fcf_win_prob}, CFC: {cfc_win_prob}") +simulation() + # --8<-- [end:solution2] @@ -55,4 +57,6 @@ def simulation_extend(run_num: int = 10000) -> None: print(f"(CFC win prob > FCF win prob)'s prob: {count / 100}") +simulation_extend() + # --8<-- [end:solution2-extend] diff --git a/mkdocs.yml b/mkdocs.yml index 02d4e64..a77a8ce 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -105,6 +105,7 @@ watch: plugins: - search + - markdown-exec - mkdocs-simple-hooks: hooks: on_post_build: "docs.hooks:copy_get" diff --git a/poetry.lock b/poetry.lock index 174267a..39517d5 100644 --- a/poetry.lock +++ b/poetry.lock @@ -854,6 +854,29 @@ type = "legacy" url = "https://pypi.tuna.tsinghua.edu.cn/simple" reference = "tsinghua" +[[package]] +name = "markdown-exec" +version = "1.6.0" +description = "Utilities to execute code blocks in Markdown files." +optional = false +python-versions = ">=3.7" +files = [ + {file = "markdown_exec-1.6.0-py3-none-any.whl", hash = "sha256:7dc22dda1e94815d72529caf6ec7a44a164d68e204894a48d4d0702425517504"}, + {file = "markdown_exec-1.6.0.tar.gz", hash = "sha256:a52a84dda65c841f1a6a605d219cef9503b0a38fef8cd6c1f19b41a02d0e8d9d"}, +] + +[package.dependencies] +pygments-ansi-color = {version = "*", optional = true, markers = "extra == \"ansi\""} +pymdown-extensions = ">=9" + +[package.extras] +ansi = ["pygments-ansi-color"] + +[package.source] +type = "legacy" +url = "https://pypi.tuna.tsinghua.edu.cn/simple" +reference = "tsinghua" + [[package]] name = "markupsafe" version = "2.1.3" @@ -1526,6 +1549,25 @@ type = "legacy" url = "https://pypi.tuna.tsinghua.edu.cn/simple" reference = "tsinghua" +[[package]] +name = "pygments-ansi-color" +version = "0.3.0" +description = "" +optional = false +python-versions = ">=3.7" +files = [ + {file = "pygments-ansi-color-0.3.0.tar.gz", hash = "sha256:7018954cf5b11d1e734383a1bafab5af613213f246109417fee3f76da26d5431"}, + {file = "pygments_ansi_color-0.3.0-py3-none-any.whl", hash = "sha256:7eb063feaecadad9d4d1fd3474cbfeadf3486b64f760a8f2a00fc25392180aba"}, +] + +[package.dependencies] +pygments = "!=2.7.3" + +[package.source] +type = "legacy" +url = "https://pypi.tuna.tsinghua.edu.cn/simple" +reference = "tsinghua" + [[package]] name = "pylint" version = "2.17.6" @@ -2169,4 +2211,4 @@ reference = "tsinghua" [metadata] lock-version = "2.0" python-versions = ">=3.8,<3.13" -content-hash = "d4aef8bc67f0de8b652ca5e5baa403d809db73363df10279d01aa1c9fb2fcae4" +content-hash = "629c47cd313f654316b1888af52f8f38a9fb59d049f6b1acf74ee28e8aa68685" diff --git a/pyproject.toml b/pyproject.toml index d956fbd..9b3daef 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -62,6 +62,7 @@ pymdown-extensions = "^10.3" mkdocs-simple-hooks = "^0.1.5" mkdocs-version-annotations = "^1.0.0" mkdocs-include-markdown-plugin = "^6.0.1" +markdown-exec = {extras = ["ansi"], version = "^1.6.0"} [[tool.poetry.source]] name = "tsinghua"