Skip to content

Commit

Permalink
add: fertility simulation solution
Browse files Browse the repository at this point in the history
  • Loading branch information
shenxiangzhuang committed Jun 18, 2024
1 parent ddfefd2 commit ac5a980
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/assets/git-committers/page-authors.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"cache_date": "2024-06-18", "page_authors": {"docs/index.md": {"last_commit_date": "2023-09-28", "authors": [{"login": "shenxiangzhuang", "name": "shenxiangzhuang", "url": "https://github.com/shenxiangzhuang", "avatar": "https://avatars.githubusercontent.com/u/17157965?v=4"}]}, "docs/CHANGELOG.md": {"last_commit_date": "2023-09-27", "authors": [{"login": "shenxiangzhuang", "name": "shenxiangzhuang", "url": "https://github.com/shenxiangzhuang", "avatar": "https://avatars.githubusercontent.com/u/17157965?v=4"}]}, "docs/fifty/index.md": {"last_commit_date": "2023-09-28", "authors": [{"login": "shenxiangzhuang", "name": "shenxiangzhuang", "url": "https://github.com/shenxiangzhuang", "avatar": "https://avatars.githubusercontent.com/u/17157965?v=4"}]}, "docs/fifty/1_the_sock_drawer.md": {"last_commit_date": "2023-10-05", "authors": [{"login": "shenxiangzhuang", "name": "shenxiangzhuang", "url": "https://github.com/shenxiangzhuang", "avatar": "https://avatars.githubusercontent.com/u/17157965?v=4"}]}, "docs/fifty/2_successive_wins.md": {"last_commit_date": "2023-10-05", "authors": [{"login": "shenxiangzhuang", "name": "shenxiangzhuang", "url": "https://github.com/shenxiangzhuang", "avatar": "https://avatars.githubusercontent.com/u/17157965?v=4"}]}, "docs/fifty/3_flippant_jury.md": {"last_commit_date": "2023-10-05", "authors": [{"login": "shenxiangzhuang", "name": "shenxiangzhuang", "url": "https://github.com/shenxiangzhuang", "avatar": "https://avatars.githubusercontent.com/u/17157965?v=4"}]}, "docs/fifty/4_first_success.md": {"last_commit_date": "2023-10-06", "authors": [{"login": "shenxiangzhuang", "name": "shenxiangzhuang", "url": "https://github.com/shenxiangzhuang", "avatar": "https://avatars.githubusercontent.com/u/17157965?v=4"}]}, "docs/fifty/5_coin_in_square.md": {"last_commit_date": "2023-10-06", "authors": [{"login": "shenxiangzhuang", "name": "shenxiangzhuang", "url": "https://github.com/shenxiangzhuang", "avatar": "https://avatars.githubusercontent.com/u/17157965?v=4"}]}, "docs/fifty/6_chuck_a_luck.md": {"last_commit_date": "2023-10-25", "authors": [{"login": "pre-commit-ci[bot]", "name": "pre-commit-ci[bot]", "url": "https://github.com/apps/pre-commit-ci", "avatar": "https://avatars.githubusercontent.com/in/68672?v=4"}, {"login": "shenxiangzhuang", "name": "shenxiangzhuang", "url": "https://github.com/shenxiangzhuang", "avatar": "https://avatars.githubusercontent.com/u/17157965?v=4"}]}, "docs/util/problem_template.md": {"last_commit_date": "2023-09-28", "authors": [{"login": "shenxiangzhuang", "name": "shenxiangzhuang", "url": "https://github.com/shenxiangzhuang", "avatar": "https://avatars.githubusercontent.com/u/17157965?v=4"}]}, "docs/blog/index.md": {"last_commit_date": "2024-04-15", "authors": [{"login": "shenxiangzhuang", "name": "shenxiangzhuang", "url": "https://github.com/shenxiangzhuang", "avatar": "https://avatars.githubusercontent.com/u/17157965?v=4"}]}, "docs/blog/posts/about.md": {"last_commit_date": "2024-04-15", "authors": [{"login": "shenxiangzhuang", "name": "shenxiangzhuang", "url": "https://github.com/shenxiangzhuang", "avatar": "https://avatars.githubusercontent.com/u/17157965?v=4"}]}, "docs/fifty/8_perfect_bridge_hand.md": {"last_commit_date": "2024-05-27", "authors": [{"login": "shenxiangzhuang", "name": "shenxiangzhuang", "url": "https://github.com/shenxiangzhuang", "avatar": "https://avatars.githubusercontent.com/u/17157965?v=4"}]}}}
{"cache_date": "2024-06-18", "page_authors": {"docs/index.md": {"last_commit_date": "2023-09-28", "authors": [{"login": "shenxiangzhuang", "name": "shenxiangzhuang", "url": "https://github.com/shenxiangzhuang", "avatar": "https://avatars.githubusercontent.com/u/17157965?v=4"}]}, "docs/CHANGELOG.md": {"last_commit_date": "2023-09-27", "authors": [{"login": "shenxiangzhuang", "name": "shenxiangzhuang", "url": "https://github.com/shenxiangzhuang", "avatar": "https://avatars.githubusercontent.com/u/17157965?v=4"}]}, "docs/fifty/index.md": {"last_commit_date": "2023-09-28", "authors": [{"login": "shenxiangzhuang", "name": "shenxiangzhuang", "url": "https://github.com/shenxiangzhuang", "avatar": "https://avatars.githubusercontent.com/u/17157965?v=4"}]}, "docs/fifty/1_the_sock_drawer.md": {"last_commit_date": "2023-10-05", "authors": [{"login": "shenxiangzhuang", "name": "shenxiangzhuang", "url": "https://github.com/shenxiangzhuang", "avatar": "https://avatars.githubusercontent.com/u/17157965?v=4"}]}, "docs/fifty/2_successive_wins.md": {"last_commit_date": "2023-10-05", "authors": [{"login": "shenxiangzhuang", "name": "shenxiangzhuang", "url": "https://github.com/shenxiangzhuang", "avatar": "https://avatars.githubusercontent.com/u/17157965?v=4"}]}, "docs/fifty/3_flippant_jury.md": {"last_commit_date": "2023-10-05", "authors": [{"login": "shenxiangzhuang", "name": "shenxiangzhuang", "url": "https://github.com/shenxiangzhuang", "avatar": "https://avatars.githubusercontent.com/u/17157965?v=4"}]}, "docs/fifty/4_first_success.md": {"last_commit_date": "2023-10-06", "authors": [{"login": "shenxiangzhuang", "name": "shenxiangzhuang", "url": "https://github.com/shenxiangzhuang", "avatar": "https://avatars.githubusercontent.com/u/17157965?v=4"}]}, "docs/fifty/5_coin_in_square.md": {"last_commit_date": "2023-10-06", "authors": [{"login": "shenxiangzhuang", "name": "shenxiangzhuang", "url": "https://github.com/shenxiangzhuang", "avatar": "https://avatars.githubusercontent.com/u/17157965?v=4"}]}, "docs/fifty/6_chuck_a_luck.md": {"last_commit_date": "2023-10-25", "authors": [{"login": "pre-commit-ci[bot]", "name": "pre-commit-ci[bot]", "url": "https://github.com/apps/pre-commit-ci", "avatar": "https://avatars.githubusercontent.com/in/68672?v=4"}, {"login": "shenxiangzhuang", "name": "shenxiangzhuang", "url": "https://github.com/shenxiangzhuang", "avatar": "https://avatars.githubusercontent.com/u/17157965?v=4"}]}, "docs/util/problem_template.md": {"last_commit_date": "2023-09-28", "authors": [{"login": "shenxiangzhuang", "name": "shenxiangzhuang", "url": "https://github.com/shenxiangzhuang", "avatar": "https://avatars.githubusercontent.com/u/17157965?v=4"}]}, "docs/blog/index.md": {"last_commit_date": "2024-04-15", "authors": [{"login": "shenxiangzhuang", "name": "shenxiangzhuang", "url": "https://github.com/shenxiangzhuang", "avatar": "https://avatars.githubusercontent.com/u/17157965?v=4"}]}, "docs/blog/posts/about.md": {"last_commit_date": "2024-04-15", "authors": [{"login": "shenxiangzhuang", "name": "shenxiangzhuang", "url": "https://github.com/shenxiangzhuang", "avatar": "https://avatars.githubusercontent.com/u/17157965?v=4"}]}, "docs/fifty/8_perfect_bridge_hand.md": {"last_commit_date": "2024-05-27", "authors": [{"login": "shenxiangzhuang", "name": "shenxiangzhuang", "url": "https://github.com/shenxiangzhuang", "avatar": "https://avatars.githubusercontent.com/u/17157965?v=4"}]}, "docs/mystery/index.md": {"last_commit_date": "2024-06-18", "authors": [{"login": "shenxiangzhuang", "name": "shenxiangzhuang", "url": "https://github.com/shenxiangzhuang", "avatar": "https://avatars.githubusercontent.com/u/17157965?v=4"}]}, "docs/mystery/3_fertility_policy.md": {"last_commit_date": "2024-06-18", "authors": [{"login": "shenxiangzhuang", "name": "shenxiangzhuang", "url": "https://github.com/shenxiangzhuang", "avatar": "https://avatars.githubusercontent.com/u/17157965?v=4"}]}}}
29 changes: 25 additions & 4 deletions docs/mystery/3_fertility_policy.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

## Solutions

### Solution1
### Solution1: Analysis

??? success "Solution1"

Expand Down Expand Up @@ -123,7 +123,7 @@
\end{align}
$$

### Solution2
### Solution2: Analysis

??? success "Solution2"

Expand Down Expand Up @@ -164,7 +164,7 @@

### Solution3

??? success "Solution3"
??? success "Solution3: Elegant Analysis"

=== "English"

Expand All @@ -181,4 +181,25 @@
无论生育政策如何,最终都是一系列相互独立的生育事件,所以最终的男女比例是1:1。


### TODO: Solution4: Simulation
### Solution4: Simulation


??? success "Solution4"

=== "English"

We can simulate the birth process to obtain the numerical solution
of the gender ratio under the current fertility policy:

```python exec="true" source="material-block" session="fifty-3"
--8<-- "docs/mystery/snippet/3_fertility_policy.py:solution"
```


=== "中文"

我们可以通过模拟生育的过程来得到当前生育政策下性别比例的数值解:

```python exec="true" source="material-block" session="fifty-3"
--8<-- "docs/mystery/snippet/3_fertility_policy.py:solution"
```
36 changes: 36 additions & 0 deletions docs/mystery/snippet/3_fertility_policy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# --8<-- [start:solution]
import random
from enum import Enum


class Gender(int, Enum):
MALE = 0
FEMALE = 1


def birth_event() -> Gender:
if random.randint(0, 1) == 0:
return Gender.MALE
else:
return Gender.FEMALE


def family_event() -> int:
female_count = 0
while birth_event() != Gender.MALE:
female_count += 1
return female_count


def simulation(run_nums: int = 100000) -> None:
male_count, female_count = 0, 0
for _ in range(run_nums):
female_count += family_event()
male_count += 1
print(f'Males: {male_count}, Females: {female_count}\n')
print(f'Males/Female: {male_count / female_count}\n')


simulation()

# --8<-- [end:solution]

0 comments on commit ac5a980

Please sign in to comment.