Skip to content

Commit

Permalink
Merge pull request #10 from shenxiangzhuang/add/run-code
Browse files Browse the repository at this point in the history
Changed:use markdown-exec to run python code
  • Loading branch information
shenxiangzhuang authored Oct 5, 2023
2 parents 54c44dc + 5abbe40 commit 5711b37
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 66 deletions.
38 changes: 6 additions & 32 deletions docs/fifty/1_the_sock_drawer.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}$
Expand Down Expand Up @@ -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}$
Expand All @@ -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}$
Expand All @@ -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}$
Expand Down
41 changes: 10 additions & 31 deletions docs/fifty/2_successive_wins.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

## Solutions

### Solution1
### Solution1: Analysis

??? success "Solution1: Analysis"

Expand All @@ -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:
Expand Down Expand Up @@ -140,7 +140,7 @@
即Elmer选择CFC的顺序比赛赢得奖励的概率更高


### Solution2
### Solution2: Simulation


??? success "Solution2: Simulation"
Expand All @@ -149,60 +149,39 @@

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.


=== "中文"

可以通过下面的方式进行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的概率。
8 changes: 6 additions & 2 deletions docs/fifty/snippet/1_the_sock_drawer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]


Expand All @@ -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]
4 changes: 4 additions & 0 deletions docs/fifty/snippet/2_successive_wins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]


Expand All @@ -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]
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ watch:

plugins:
- search
- markdown-exec
- mkdocs-simple-hooks:
hooks:
on_post_build: "docs.hooks:copy_get"
Expand Down
44 changes: 43 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 5711b37

Please sign in to comment.