iT邦幫忙

2022 iThome 鐵人賽

DAY 18
0
自我挑戰組

Udemy課程上完你也可以開始Codewars 30天系列 第 18

[Day18] Codewars >>> Probabilities for Sums in Rolling Cubic Dice (Python)

  • 分享至 

  • xImage
  •  

題目(5kyu):

When we throw 2 classical dice (values on each side from 1 to 6) we have 36 (6 * 6) >different results.

We want to know the probability of having the sum of the results equals to 11. For that >result we have only the combination of 6 and 5. So we will have two events: {5, 6} and >{6, 5}

So the probability for that result will be:

P(11, 2) = 2/(6*6) = 1/18 (The two is because we have 2 dice)
Now, we want to calculate the probability of having the sum equals to 8. The >combinations for that result will be the following: {4,4}, {3,5}, {5,3}, {2,6}, {6,2} >with a total of five combinations.

P(8, 2) = 5/36
Things may be more complicated if we have more dices and sum values higher.
We want to know the probability of having the sum equals to 8 but having 3 dice.
Now the combinations and corresponding events are:
{2,3,3}, {3,2,3}, {3,3,2}
{2,2,4}, {2,4,2}, {4,2,2}
{1,3,4}, {1,4,3}, {3,1,4}, {4,1,3}, {3,4,1}, {4,3,1}
{1,2,5}, {1,5,2}, {2,1,5}, {5,1,2}, {2,5,1}, {5,2,1}
{1,1,6}, {1,6,1}, {6,1,1}

A total amount of 21 different combinations

So the probability is:
P(8, 3) = 21/(666) = 0.09722222222222222
Summarizing the cases we have seen with a function that receives the two arguments

rolldice_sum_prob(11, 2) == 0.0555555555 # or 1/18
rolldice_sum_prob(8, 2) == 0.13888888889# or 5/36
rolldice_sum_prob(8, 3) == 0.0972222222222 # or 7/72

And think why we have this result:
rolldice_sum_prob(22, 3) == 0

Create the function rolldice_sum_prob() for this calculation.

解題思路

題目理解:設計一函式,計算在使用dice_amount顆骰子丟擲後,點數和為sum_point的機率並返還。

在這裡介紹一個好用的python的好用模組,itertools
裡面有許多方便計算的函式可供利用,其中itertools.product()能幫助我們簡單找出所有骰子的組合可能。

lst1 = ["a","b"]
lst2 = [1,2]

for i in itertools.product(lst1,lst2):
    print(i)
#輸出:
#('a', 1)
#('a', 2)
#('b', 1)
#('b', 2)

product()可放入多個列表,找出所有列表內各index位置上的元素重新組合過的所有結果,數學上把這樣的結果稱為Cartesian Product。
由於是針對列表中的index做排列組合,就算兩列表中有相同的元素也會被計算在組合數內,例:

exsample = list(itertools.product(["a","b"],["a","c"]))
print(exsample) #輸出:[('a', 'a'), ('a', 'c'), ('b', 'a'), ('b', 'c')]

故與我們計算骰子組合數所需相同,其中itertools.product()還有一個可選參數repeat可供使用。
若函式代入列表lst並將repeat參數代入某正整數n,則函式會返還n個lst的Cartesian Product結果,例:

lst = ["a","b"]

way1 = list(itertools.product(lst,lst))
way2 = list(itertools.product(lst,repeat = 2))

print(way1 == way2) #輸出:True

故可以利用以上解題如下:

import itertools

def rolldice_sum_prob(sum_point, dice_amount):
    """函式返還在使用dice_amount顆骰子丟擲後,點數和為sum_point的機率"""
    #利用itertools.product()將不同骰子數量下,所有的骰子組合以tuple的形式存入列表中
    all_combination = list(itertools.product(range(1,7), repeat = dice_amount))
    #變數count用以儲存符合條件的組合數
    count = 0
    #利用迴圈遍歷all_combination,若其中有組合reslut的sum()結果與指定點數相同,則count計數加一
    for reslut in all_combination:
        if sum(reslut) == sum_point:
            count += 1
    #將所有符合條件的組合數除以總組合數即為出現該點數的機率
    return count/len(all_combination)

上一篇
[Day17] Codewars >>> Compute the Largest Sum of all Contiguous Subsequences (Python)
下一篇
[Day19] Codewars >>> Calculating with Functions (Python)
系列文
Udemy課程上完你也可以開始Codewars 30天30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言