Skip to content

Commit

Permalink
add basic category
Browse files Browse the repository at this point in the history
add basic category
  • Loading branch information
math4mad committed Oct 1, 2023
1 parent 43a1c0a commit 266c35b
Show file tree
Hide file tree
Showing 7 changed files with 283 additions and 1 deletion.
11 changes: 11 additions & 0 deletions _freeze/basic/1-dice/execute-results/html.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"hash": "ba26fa11c8c097bfed3fe1d8ba31a3bf",
"result": {
"markdown": "---\ntitle: \"1-basic-probability-dice \"\n---\n\n## 1. define tools\n### 1.1 Dice🎲\n\n::: {.cell execution_count=1}\n``` {.julia .cell-code}\nemoji_dice1=[\"1️⃣\",\"2️⃣\",\"3️⃣\",\"4️⃣\",\"5️⃣\",\"6️⃣\"]\nemoji_dice2=[\"1️⃣\",\"2️⃣\",\"3️⃣\",\"4️⃣\",\"5️⃣\",\"6️⃣\"]\ndice1=Vector(1:6)\ndice2=Vector(1:6)\nshow(dice1)\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n[1, 2, 3, 4, 5, 6]\n```\n:::\n:::\n\n\n::: {.cell execution_count=2}\n``` {.julia .cell-code}\nusing PrettyTables\n\nmatrix=[[i,j] for i in dice1, j in dice2]\ndisplay(matrix)\ntwo_dice_space=vec(matrix)\ntwo_dice_size=length(two_dice_space)\n\"two_dice_size\"=>two_dice_size\n```\n\n::: {.cell-output .cell-output-display}\n```\n6×6 Matrix{Vector{Int64}}:\n [1, 1] [1, 2] [1, 3] [1, 4] [1, 5] [1, 6]\n [2, 1] [2, 2] [2, 3] [2, 4] [2, 5] [2, 6]\n [3, 1] [3, 2] [3, 3] [3, 4] [3, 5] [3, 6]\n [4, 1] [4, 2] [4, 3] [4, 4] [4, 5] [4, 6]\n [5, 1] [5, 2] [5, 3] [5, 4] [5, 5] [5, 6]\n [6, 1] [6, 2] [6, 3] [6, 4] [6, 5] [6, 6]\n```\n:::\n\n::: {.cell-output .cell-output-display execution_count=3}\n```\n\"two_dice_size\" => 36\n```\n:::\n:::\n\n\n## 2.question \n\n### 2.1 两个骰子乘积为4\n```julia\n cond(arr::Array,cond_number=4)=*(arr...)==cond_number\n```\n\n::: {.cell execution_count=3}\n``` {.julia .cell-code}\ncond1(arr::Array,cond_number=4)::Bool=*(arr...)==cond_number\nres1=filter(arr->cond1(arr),two_dice_space)\nshow(\"两个骰子乘积为4\"=>res1)\nprintln(\"\\n\")\nshow(\"两个骰子乘积为4概率\"=>length(res1)//two_dice_size)\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n\"两个骰子乘积为4\" => [[4, 1], [2, 2], [1, 4]]\n\n\"两个骰子乘积为4概率\" => 1//12\n```\n:::\n:::\n\n\n### 2.2 两个骰子点数合计等于小于 5\n\n::: {.cell execution_count=4}\n``` {.julia .cell-code}\ncond2(arr::Array)::Bool=sum(arr)<=5\nres2=filter(arr->cond2(arr),two_dice_space)\nshow(\"两个骰子点数合计等于小于5\"=>length(res2))\nprintln(\"\\n\")\nshow(\"两个骰子点数合计等于小于5的概率\"=>length(res2)//two_dice_size)\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n\"两个骰子点数合计等于小于5\" => 10\n\n\"两个骰子点数合计等于小于5的概率\" => 5//18\n```\n:::\n:::\n\n\n### 2.3 两个骰子点数合计大于等于 10\n\n::: {.cell execution_count=5}\n``` {.julia .cell-code}\ncond3(arr::Array)::Bool=sum(arr)>=10\nres3=filter(arr->cond3(arr),two_dice_space)\nshow(\"点数合计大于等于10\"=>res3)\nprintln(\"\\n\")\nshow(\"点数合计大于等于10的概率\"=>length(res3)//two_dice_size)\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n\"点数合计大于等于10\" => [[6, 4], [5, 5], [6, 5], [4, 6], [5, 6], [6, 6]]\n\n\"点数合计大于等于10的概率\" => 1//6\n```\n:::\n:::\n\n\n### 2.4 两个骰子点数相同\n\n::: {.cell execution_count=6}\n``` {.julia .cell-code}\ncond4(arr::Array)::Bool=arr[1]==arr[2]\nres4=filter(arr->cond4(arr),two_dice_space)\nshow(\"两个骰子点数相同\"=>res4)\nprintln(\"\\n\")\nshow(\"两个骰子点数相同的概率\"=>length(res4)//two_dice_size)\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n\"两个骰子点数相同\" => [[1, 1], [2, 2], [3, 3], [4, 4], [5, 5], [6, 6]]\n\n\"两个骰子点数相同的概率\" => 1//6\n```\n:::\n:::\n\n\n### 2.5 两个骰子点数合计等于 10\n\n::: {.cell execution_count=7}\n``` {.julia .cell-code}\ncond5(arr::Array)::Bool=sum(arr)==10\nres5=filter(arr->cond5(arr),two_dice_space)\nshow(\"点数合计等于10\"=>res5)\nprintln(\"\\n\")\nshow(\"点数合计等于10的概率\"=>length(res5)//two_dice_size)\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n\"点数合计等于10\" => [[6, 4], [5, 5], [4, 6]]\n\n\"点数合计等于10的概率\" => 1//12\n```\n:::\n:::\n\n\n### 2.6 点数之和被5整除\n\n::: {.cell execution_count=8}\n``` {.julia .cell-code}\ncond6(arr::Array)::Bool=sum(arr)|>d->mod(d,5)==0\nres6=filter(arr->cond6(arr),two_dice_space)\nshow(\"点数之和被5整除\"=>res6)\nprintln(\"\\n\")\nshow(\"点数之和被5整除的概率\"=>length(res6)//two_dice_size)\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n\"点数之和被5整除\" => [[4, 1], [3, 2], [2, 3], [1, 4], [6, 4], [5, 5], [4, 6]]\n\n\"点数之和被5整除的概率\" => 7//36\n```\n:::\n:::\n\n\n",
"supporting": [
"1-dice_files"
],
"filters": [],
"includes": {}
}
}
11 changes: 11 additions & 0 deletions _freeze/basic/1/execute-results/html.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"hash": "521b3e8805bd0249765284ca1cab2aff",
"result": {
"markdown": "---\ntitle: \"1-basic-probability \"\n---\n\n## 1. define tools\n### 1.1 Dice🎲\n\n::: {.cell execution_count=1}\n``` {.julia .cell-code}\nemoji_dice1=[\"1️⃣\",\"2️⃣\",\"3️⃣\",\"4️⃣\",\"5️⃣\",\"6️⃣\"]\nemoji_dice2=[\"1️⃣\",\"2️⃣\",\"3️⃣\",\"4️⃣\",\"5️⃣\",\"6️⃣\"]\ndice1=Vector(1:6)\ndice2=Vector(1:6)\nshow(dice1)\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n[1, 2, 3, 4, 5, 6]\n```\n:::\n:::\n\n\n::: {.cell execution_count=2}\n``` {.julia .cell-code}\nusing PrettyTables\n\nmatrix=[[i,j] for i in dice1, j in dice2]\ndisplay(matrix)\ntwo_dice_space=vec(matrix)\ntwo_dice_size=length(two_dice_space)\n\"two_dice_size\"=>two_dice_size\n```\n\n::: {.cell-output .cell-output-display}\n```\n6×6 Matrix{Vector{Int64}}:\n [1, 1] [1, 2] [1, 3] [1, 4] [1, 5] [1, 6]\n [2, 1] [2, 2] [2, 3] [2, 4] [2, 5] [2, 6]\n [3, 1] [3, 2] [3, 3] [3, 4] [3, 5] [3, 6]\n [4, 1] [4, 2] [4, 3] [4, 4] [4, 5] [4, 6]\n [5, 1] [5, 2] [5, 3] [5, 4] [5, 5] [5, 6]\n [6, 1] [6, 2] [6, 3] [6, 4] [6, 5] [6, 6]\n```\n:::\n\n::: {.cell-output .cell-output-display execution_count=265}\n```\n\"two_dice_size\" => 36\n```\n:::\n:::\n\n\n## 2.question \n\n### 2.1 两个骰子乘积为4\n```julia\n cond(arr::Array,cond_number=4)=*(arr...)==cond_number\n```\n\n::: {.cell execution_count=3}\n``` {.julia .cell-code}\ncond1(arr::Array,cond_number=4)::Bool=*(arr...)==cond_number\nres1=filter(arr->cond1(arr),two_dice_space)\nshow(\"两个骰子乘积为4\"=>res1)\nprintln(\"\\n\")\nshow(\"两个骰子乘积为4概率\"=>length(res1)//two_dice_size)\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n\"两个骰子乘积为4\" => [[4, 1], [2, 2], [1, 4]]\n\n\"两个骰子乘积为4概率\" => 1//12\n```\n:::\n:::\n\n\n### 2.2 两个骰子点数合计等于小于 5\n\n::: {.cell execution_count=4}\n``` {.julia .cell-code}\ncond2(arr::Array)::Bool=sum(arr)<=5\nres2=filter(arr->cond2(arr),two_dice_space)\nshow(\"两个骰子点数合计等于小于5\"=>length(res2))\nprintln(\"\\n\")\nshow(\"两个骰子点数合计等于小于5的概率\"=>length(res2)//two_dice_size)\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n\"两个骰子点数合计等于小于5\" => 10\n\n\"两个骰子点数合计等于小于5的概率\" => 5//18\n```\n:::\n:::\n\n\n### 2.3 两个骰子点数合计大于等于 10\n\n::: {.cell execution_count=5}\n``` {.julia .cell-code}\ncond3(arr::Array)::Bool=sum(arr)>=10\nres3=filter(arr->cond3(arr),two_dice_space)\nshow(\"点数合计大于等于10\"=>res3)\nprintln(\"\\n\")\nshow(\"点数合计大于等于10的概率\"=>length(res3)//two_dice_size)\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n\"点数合计大于等于10\" => [[6, 4], [5, 5], [6, 5], [4, 6], [5, 6], [6, 6]]\n\n\"点数合计大于等于10的概率\" => 1//6\n```\n:::\n:::\n\n\n### 2.4 两个骰子点数相同\n\n::: {.cell execution_count=6}\n``` {.julia .cell-code}\ncond4(arr::Array)::Bool=arr[1]==arr[2]\nres4=filter(arr->cond4(arr),two_dice_space)\nshow(\"两个骰子点数相同\"=>res4)\nprintln(\"\\n\")\nshow(\"两个骰子点数相同的概率\"=>length(res4)//two_dice_size)\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n\"两个骰子点数相同\" => [[1, 1], [2, 2], [3, 3], [4, 4], [5, 5], [6, 6]]\n\n\"两个骰子点数相同的概率\" => 1//6\n```\n:::\n:::\n\n\n### 2.5 两个骰子点数合计等于 10\n\n::: {.cell execution_count=7}\n``` {.julia .cell-code}\ncond5(arr::Array)::Bool=sum(arr)==10\nres5=filter(arr->cond5(arr),two_dice_space)\nshow(\"点数合计等于10\"=>res5)\nprintln(\"\\n\")\nshow(\"点数合计等于10的概率\"=>length(res5)//two_dice_size)\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n\"点数合计等于10\" => [[6, 4], [5, 5], [4, 6]]\n\n\"点数合计等于10的概率\" => 1//12\n```\n:::\n:::\n\n\n### 2.6 点数之和被5整除\n\n::: {.cell execution_count=8}\n``` {.julia .cell-code}\ncond6(arr::Array)::Bool=sum(arr)|>d->mod(d,5)==0\nres6=filter(arr->cond6(arr),two_dice_space)\nshow(\"点数之和被5整除\"=>res6)\nprintln(\"\\n\")\nshow(\"点数之和被5整除的概率\"=>length(res6)//two_dice_size)\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n\"点数之和被5整除\" => [[4, 1], [3, 2], [2, 3], [1, 4], [6, 4], [5, 5], [4, 6]]\n\n\"点数之和被5整除的概率\" => 7//36\n```\n:::\n:::\n\n\n",
"supporting": [
"1_files"
],
"filters": [],
"includes": {}
}
}
11 changes: 11 additions & 0 deletions _freeze/basic/2-marble/execute-results/html.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"hash": "326c70e02db5e7178bdc27acf3b18398",
"result": {
"markdown": "---\ntitle: \"2-basic-probability-marble\"\n---\n\n## 1. define tools\n### 1.1 Marble\n\n\n\n\n## 2. question\n### 2.1 包内有🔴:4\\n 🔵:3\\n 🟢:7,连续取出两个红球的概率\n\n概率路径图:\n\n```{mermaid}\n%%| fig-width: 6.5\nflowchart LR\n Urn[\"👜\"]---bag(\"🔴:4\\n 🔵:3\\n 🟢:7\")\n bag==>bag2(\"🔴:3\\n 🔵:3\\n 🟢:7\")\n bag==r1:4/14 ==>r1(\"🔴\")\n bag2==r2:3/13 ==>r2(\"🔴\")\n \n```\n\n\n>连续取出两个红球,等于第一次取出红球的概率和第二次取出红球的概率乘积\n>\n>因为第一取出红球以后,包内的红球数和总球数都发生了改变,红球变为 3, 总的为 13\n\n::: {.cell execution_count=1}\n``` {.julia .cell-code}\nr1=4//14\nr2=3//13\nboth_red=r1*r2\n\"连续取出两个红球的概率\"=>both_red\n```\n\n::: {.cell-output .cell-output-display execution_count=16}\n```\n\"连续取出两个红球的概率\" => 6//91\n```\n:::\n:::\n\n\n### 2.2 包内有🔴:15\\n 🔵:18\\n 🟢:10,连续取出两个红球的概率\n和 2.1 方法一样,只不过比例不同\n\n```{mermaid}\n%%| fig-width: 6.5\nflowchart LR\n Urn[\"👜\"]---bag(\"🔴:15\\n 🔵:18\\n 🟢:10\")\n bag==>bag2(\"🔴:14\\n 🔵:18\\n 🟢:10\")\n bag==r1:15/43 ==>r1(\"🔴\")\n bag2==r2:14/42 ==>r2(\"🔴\")\n \n```\n\n::: {.cell execution_count=2}\n``` {.julia .cell-code}\n\"定义袋子类型\"\nstruct Bag\n red::Int\n blue::Int\n green::Int\nend\n\n\"\"\"\n枚举限定球种类\n@ref:snippetslab://snippet/6016DF64-9B90-420E-83F7-7C6FF7D4A139\n\"\"\"\n@enum Ball red = 1 blue = 2 green = 3\n\nfunction new_bag_space_prob(bag::Bag, pick::Ball=red)::Tuple{Bag,Real}\n (; red, blue, green) = bag # struct 解构\n ball = Int(pick) # @enum 类型需要转换为整型\n prob_size = +(red, blue, green) # 合计球总数\n res::Tuple{Bag,Real} = if ball == 1\n (Bag([red - 1, blue, green]...), red // prob_size)\n elseif ball == 2\n (Bag([red, blue - 1, green]...), blue // prob_size)\n else\n (Bag([red, blue, green - 1]...), green // prob_size)\n end\n return res\nend\n\nbag = Bag(15, 18, 10)\nres1 = new_bag_space_prob(bag, red)\nres2 = new_bag_space_prob(res1[1], red)\nboth_red_ball=res1[2] * res2[2]\n\"有🔴:15,🔵:18, 🟢:10时,取出两个红球的概率\"=>both_red_ball\n```\n\n::: {.cell-output .cell-output-display execution_count=17}\n```\n\"有🔴:15,🔵:18, 🟢:10时,取出两个红球的概率\" => 5//43\n```\n:::\n:::\n\n\n### 2.3 包内有🔴:13 🔵:3 🟢:12,连续取出两个红球的概率\n\n\n```{mermaid}\n%%| fig-width: 6.5\nflowchart LR\n Urn[\"👜\"]---bag(\"🔴:13\\n 🔵:3\\n 🟢:12\")\n bag==>bag2(\"🔴:12\\n 🔵:3\\n 🟢:12\")\n bag==r1:13/28 ==>r1(\"🔴\")\n bag2==r2:12/27 ==>r2(\"🔴\")\n \n```\n\n::: {.cell execution_count=3}\n``` {.julia .cell-code}\nbag2 = Bag(13, 3, 12)\nres3 = new_bag_space_prob(bag2, red)\nres4 = new_bag_space_prob(res3[1], red)\nboth_red_ball2=res3[2] * res4[2]\n\"有🔴:13 🔵:3 🟢:12时,取出两个红球的概率\"=>both_red_ball2\n```\n\n::: {.cell-output .cell-output-display execution_count=18}\n```\n\"有🔴:13 🔵:3 🟢:12时,取出两个红球的概率\" => 13//63\n```\n:::\n:::\n\n\n",
"supporting": [
"2-marble_files"
],
"filters": [],
"includes": {}
}
}
3 changes: 2 additions & 1 deletion _quarto.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ website:
text: Home
- section: "AFCP"
contents: exercises/*

- section : "Turing"
contents: turing/*
- section : "Basic"
contents: basic/*

format:
html:
Expand Down
89 changes: 89 additions & 0 deletions basic/1-dice.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
---
title: "1-basic-probability-dice "
---

## 1. define tools
### 1.1 Dice🎲


```{julia}
emoji_dice1=["1️⃣","2️⃣","3️⃣","4️⃣","5️⃣","6️⃣"]
emoji_dice2=["1️⃣","2️⃣","3️⃣","4️⃣","5️⃣","6️⃣"]
dice1=Vector(1:6)
dice2=Vector(1:6)
show(dice1)
```

```{julia}
using PrettyTables
matrix=[[i,j] for i in dice1, j in dice2]
display(matrix)
two_dice_space=vec(matrix)
two_dice_size=length(two_dice_space)
"two_dice_size"=>two_dice_size
```

## 2.question

### 2.1 两个骰子乘积为4
```julia
cond(arr::Array,cond_number=4)=*(arr...)==cond_number
```
```{julia}
cond1(arr::Array,cond_number=4)::Bool=*(arr...)==cond_number
res1=filter(arr->cond1(arr),two_dice_space)
show("两个骰子乘积为4"=>res1)
println("\n")
show("两个骰子乘积为4概率"=>length(res1)//two_dice_size)
```

### 2.2 两个骰子点数合计等于小于 5
```{julia}
cond2(arr::Array)::Bool=sum(arr)<=5
res2=filter(arr->cond2(arr),two_dice_space)
show("两个骰子点数合计等于小于5"=>length(res2))
println("\n")
show("两个骰子点数合计等于小于5的概率"=>length(res2)//two_dice_size)
```
### 2.3 两个骰子点数合计大于等于 10
```{julia}
cond3(arr::Array)::Bool=sum(arr)>=10
res3=filter(arr->cond3(arr),two_dice_space)
show("点数合计大于等于10"=>res3)
println("\n")
show("点数合计大于等于10的概率"=>length(res3)//two_dice_size)
```

### 2.4 两个骰子点数相同
```{julia}
cond4(arr::Array)::Bool=arr[1]==arr[2]
res4=filter(arr->cond4(arr),two_dice_space)
show("两个骰子点数相同"=>res4)
println("\n")
show("两个骰子点数相同的概率"=>length(res4)//two_dice_size)
```

### 2.5 两个骰子点数合计等于 10
```{julia}
cond5(arr::Array)::Bool=sum(arr)==10
res5=filter(arr->cond5(arr),two_dice_space)
show("点数合计等于10"=>res5)
println("\n")
show("点数合计等于10的概率"=>length(res5)//two_dice_size)
```

### 2.6 点数之和被5整除
```{julia}
cond6(arr::Array)::Bool=sum(arr)|>d->mod(d,5)==0
res6=filter(arr->cond6(arr),two_dice_space)
show("点数之和被5整除"=>res6)
println("\n")
show("点数之和被5整除的概率"=>length(res6)//two_dice_size)
```






105 changes: 105 additions & 0 deletions basic/2-marble.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
---
title: "2-basic-probability-marble"
---

## 1. define tools
### 1.1 Marble




## 2. question
### 2.1 包内有🔴:4\n 🔵:3\n 🟢:7,连续取出两个红球的概率

概率路径图:
```{mermaid}
%%| fig-width: 6.5
flowchart LR
Urn["👜"]---bag("🔴:4\n 🔵:3\n 🟢:7")
bag==>bag2("🔴:3\n 🔵:3\n 🟢:7")
bag==r1:4/14 ==>r1("🔴")
bag2==r2:3/13 ==>r2("🔴")
```

>连续取出两个红球,等于第一次取出红球的概率和第二次取出红球的概率乘积
>
>因为第一取出红球以后,包内的红球数和总球数都发生了改变,红球变为 3, 总的为 13
```{julia}
r1=4//14
r2=3//13
both_red=r1*r2
"连续取出两个红球的概率"=>both_red
```

### 2.2 包内有🔴:15\n 🔵:18\n 🟢:10,连续取出两个红球的概率
和 2.1 方法一样,只不过比例不同
```{mermaid}
%%| fig-width: 6.5
flowchart LR
Urn["👜"]---bag("🔴:15\n 🔵:18\n 🟢:10")
bag==>bag2("🔴:14\n 🔵:18\n 🟢:10")
bag==r1:15/43 ==>r1("🔴")
bag2==r2:14/42 ==>r2("🔴")
```
```{julia}
"定义袋子类型"
struct Bag
red::Int
blue::Int
green::Int
end
"""
枚举限定球种类
@ref:snippetslab://snippet/6016DF64-9B90-420E-83F7-7C6FF7D4A139
"""
@enum Ball red = 1 blue = 2 green = 3
function new_bag_space_prob(bag::Bag, pick::Ball=red)::Tuple{Bag,Real}
(; red, blue, green) = bag # struct 解构
ball = Int(pick) # @enum 类型需要转换为整型
prob_size = +(red, blue, green) # 合计球总数
res::Tuple{Bag,Real} = if ball == 1
(Bag([red - 1, blue, green]...), red // prob_size)
elseif ball == 2
(Bag([red, blue - 1, green]...), blue // prob_size)
else
(Bag([red, blue, green - 1]...), green // prob_size)
end
return res
end
bag = Bag(15, 18, 10)
res1 = new_bag_space_prob(bag, red)
res2 = new_bag_space_prob(res1[1], red)
both_red_ball=res1[2] * res2[2]
"有🔴:15,🔵:18, 🟢:10时,取出两个红球的概率"=>both_red_ball
```


### 2.3 包内有🔴:13 🔵:3 🟢:12,连续取出两个红球的概率

```{mermaid}
%%| fig-width: 6.5
flowchart LR
Urn["👜"]---bag("🔴:13\n 🔵:3\n 🟢:12")
bag==>bag2("🔴:12\n 🔵:3\n 🟢:12")
bag==r1:13/28 ==>r1("🔴")
bag2==r2:12/27 ==>r2("🔴")
```

```{julia}
bag2 = Bag(13, 3, 12)
res3 = new_bag_space_prob(bag2, red)
res4 = new_bag_space_prob(res3[1], red)
both_red_ball2=res3[2] * res4[2]
"有🔴:13 🔵:3 🟢:12时,取出两个红球的概率"=>both_red_ball2
```




54 changes: 54 additions & 0 deletions basic/_2-marble.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@


"定义袋子类型"
struct Bag
red::Int
blue::Int
green::Int
end

"""
枚举限定球种类
@ref:snippetslab://snippet/6016DF64-9B90-420E-83F7-7C6FF7D4A139
"""
@enum Ball red = 1 blue = 2 green = 3



"""
new_bag_space_prob(bag::Bag, pick::Ball=red)::Tuple{Bag,Real}
从 bag 中取出 pick球以后, 返回取出该球的概率和取出该球之后的概率空间
```
(; red, blue, green) = bag # struct 解构
ball = Int(pick) # @enum 类型需要转换为整型
prob_size = +(red, blue, green) # 合计球总数
res::Tuple{Bag,Real}=(Bag([red - 1, blue, green]...), red // prob_size)
```
如果取出的球为红色,定义新的(Bag([red - 1, blue, green]...) eg.
"""
function new_bag_space_prob(bag::Bag, pick::Ball=red)::Tuple{Bag,Real}
(; red, blue, green) = bag # struct 解构
ball = Int(pick) # @enum 类型需要转换为整型
prob_size = +(red, blue, green) # 合计球总数
res::Tuple{Bag,Real} = if ball == 1
(Bag([red - 1, blue, green]...), red // prob_size)
elseif ball == 2
(Bag([red, blue - 1, green]...), blue // prob_size)
else
(Bag([red, blue, green - 1]...), green // prob_size)
end
return res
end


bag = Bag(15, 18, 10)

res1 = new_bag_space_prob(bag, red)
res2 = new_bag_space_prob(res1[1], red)
both_red_ball=res1[2] * res2[2]

"取出两个红球的概率"=>both_red_ball

0 comments on commit 266c35b

Please sign in to comment.