毕业设计网站只做前端行不行,石家庄做网站比较好的公司有哪些,wordpress 汉化,网站开发常用语言的优劣势CheckiO 是面向初学者和高级程序员的编码游戏#xff0c;使用 Python 和 JavaScript 解决棘手的挑战和有趣的任务#xff0c;从而提高你的编码技能#xff0c;本博客主要记录自己用 Python 在闯关时的做题思路和实现代码#xff0c;同时也学习学习其他大神写的代码。
Chec…
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 ≤ 102 ≤ sides ≤ 200 ≤ 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_digits4):precision 0.1 ** significant_digitsreturn correct - precision checked correct precisionassert(almost_equal(probability(2, 6, 3), 0.0556)), Basic exampleassert(almost_equal(probability(2, 6, 4), 0.0833)), More pointsassert(almost_equal(probability(2, 6, 7), 0.1667)), Maximum for two 6-sided diceassert(almost_equal(probability(2, 3, 5), 0.2222)), Small diceassert(almost_equal(probability(2, 3, 7), 0.0000)), Never!assert(almost_equal(probability(3, 6, 7), 0.0694)), Three diceassert(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_cachelru_cache(maxsizeNone)
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, sides1)])/sides大神解答 NO.3 from scipy.special import binom as b
probabilitylambda n,s,p:sum((-1)**x*b(n, x)*\b(p-s*x-1,n-1)for x in range((p-n)//s1))/s**n 大神解答 NO.4 probability,Clambda 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