【Python CheckiO 题解】Probably Dice
CheckiO 是面向初學者和高級程序員的編碼游戲,使用 Python 和 JavaScript 解決棘手的挑戰和有趣的任務,從而提高你的編碼技能,本博客主要記錄自己用 Python 在闖關時的做題思路和實現代碼,同時也學習學習其他大神寫的代碼。
CheckiO 官網:https://checkio.org/
我的 CheckiO 主頁:https://py.checkio.org/user/TRHX/
CheckiO 題解系列專欄:https://itrhx.blog.csdn.net/category_9536424.html
CheckiO 所有題解源代碼:https://github.com/TRHX/Python-CheckiO-Exercise
題目描述
【Probably Dice】:計算擲骰子命中點數的概率, 給定三個參數:骰子數,每個骰子的面數,要計算概率的目標數,擲 n 個骰子,將每個骰子的點數加起來,若點數和與目標數相同,則表示命中,計算所有的情況中,命中的概率,結果的精度應為四位數,即 ±0.0001,例如:擲出 2 個 6 面的骰子,則點數和為 3 的概率為 2/36 或 5.56%,您應該返回 0.0556。
 
 【鏈接】:https://py.checkio.org/mission/probably-dice/
【輸入】:三個參數:骰子數,每個骰子的面數,要計算概率的目標數,均為整數
【輸出】:命中的概率,浮點數
【前提】:1 ≤ dice_number ≤ 10;2 ≤ sides ≤ 20;0 ≤ target < 1000
【范例】:
probability(2, 6, 3) == 0.0556 # 2 six-sided dice have a 5.56% chance of totalling 3 probability(2, 6, 4) == 0.0833 probability(2, 6, 7) == 0.1667 probability(2, 3, 5) == 0.2222 # 2 three-sided dice have a 22.22% chance of totalling 5 probability(2, 3, 7) == 0 # The maximum you can roll on 2 three-sided dice is 6 probability(3, 6, 7) == 0.0694 probability(10, 10, 50) == 0.0375代碼實現
def probability(dice_number, sides, target):dic = {}def calculation(dice_number, sides, target):if dice_number > target or dice_number * sides < target:return 0if dice_number == 1:return 1if (dice_number, sides, target) in dic:return dic[(dice_number, sides, target)]else:dic[(dice_number, sides, target)] = sum(calculation(dice_number - 1, sides, target - i) for i in range(1, sides + 1))return dic[(dice_number, sides, target)]return calculation(dice_number, sides, target) / sides ** dice_numberif __name__ == '__main__':#These are only used for self-checking and are not necessary for auto-testingdef almost_equal(checked, correct, significant_digits=4):precision = 0.1 ** significant_digitsreturn correct - precision < checked < correct + precisionassert(almost_equal(probability(2, 6, 3), 0.0556)), "Basic example"assert(almost_equal(probability(2, 6, 4), 0.0833)), "More points"assert(almost_equal(probability(2, 6, 7), 0.1667)), "Maximum for two 6-sided dice"assert(almost_equal(probability(2, 3, 5), 0.2222)), "Small dice"assert(almost_equal(probability(2, 3, 7), 0.0000)), "Never!"assert(almost_equal(probability(3, 6, 7), 0.0694)), "Three dice"assert(almost_equal(probability(10, 10, 50), 0.0375)), "Many dice, many sides"大神解答
大神解答 NO.1
from numpy.polynomial.polynomial import polypowdef probability(dice_number, sides, target):""" The number of ways to obtain x as a sum of n s-sided diceis given by the coefficients of the polynomial:f(x) = (x + x^2 + ... + x^s)^n"""# power series (note that the power series starts from x^1, therefore# the first coefficient is zero)powers = [0] + [1] * sides# f(x) polynomial, computed used polypow in numpypoly = polypow(powers, dice_number)# check if the target is in valid range# if an IndexError is raised, it means that the target cannot be reached,# therefore the probability is 0try:return poly[target] / sides ** dice_numberexcept IndexError:return 0大神解答 NO.2
from functools import lru_cache@lru_cache(maxsize=None) def probability(dice_number, sides, target):if dice_number == 1:return (1 <= target <= sides**dice_number)/sidesreturn sum([probability(dice_number-1, sides, target-x)for x in range(1, sides+1)])/sides大神解答 NO.3
from scipy.special import binom as b probability=lambda n,s,p:sum((-1)**x*b(n, x)*\b(p-s*x-1,n-1)for x in range((p-n)//s+1))/s**n大神解答 NO.4
probability,C=lambda n,s,t:sum((-1)**k*C(n,k)*C(t-k*s-1,n-1)for k in range(1+(t-n)//s))/s**n,lambda n,k:n*C(n-1,k-1)//k if k else 1總結
以上是生活随笔為你收集整理的【Python CheckiO 题解】Probably Dice的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 86万的蓝牌特斯拉路上趴窝!车主爬出后备
 - 下一篇: 更方便了!杭州场所码再次升级:不用输手机