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相关推荐

  1. Python CheckiO 题解系列 丨 博客目录索引

    CheckiO 是面向初学者和高级程序员的编码游戏,使用 Python 和 JavaScript 解决棘手的挑战和有趣的任务,从而提高你的编码技能,本题解系列主要记录自己在用 Python 闯关时的做 ...

  2. 【Python CheckiO 题解】Army Battles

    CheckiO 是面向初学者和高级程序员的编码游戏,使用 Python 和 JavaScript 解决棘手的挑战和有趣的任务,从而提高你的编码技能,本博客主要记录自己用 Python 在闯关时的做题思 ...

  3. 【Python CheckiO 题解】Date and Time Converter

    CheckiO 是面向初学者和高级程序员的编码游戏,使用 Python 和 JavaScript 解决棘手的挑战和有趣的任务,从而提高你的编码技能,本博客主要记录自己用 Python 在闯关时的做题思 ...

  4. 【Python CheckiO 题解】Largest Rectangle in a Histogram

    CheckiO 是面向初学者和高级程序员的编码游戏,使用 Python 和 JavaScript 解决棘手的挑战和有趣的任务,从而提高你的编码技能,本博客主要记录自己用 Python 在闯关时的做题思 ...

  5. 【Python CheckiO 题解】Roman Numerals

    CheckiO 是面向初学者和高级程序员的编码游戏,使用 Python 和 JavaScript 解决棘手的挑战和有趣的任务,从而提高你的编码技能,本博客主要记录自己用 Python 在闯关时的做题思 ...

  6. 【Python CheckiO 题解】Multicolored Lamp

    CheckiO 是面向初学者和高级程序员的编码游戏,使用 Python 和 JavaScript 解决棘手的挑战和有趣的任务,从而提高你的编码技能,本博客主要记录自己用 Python 在闯关时的做题思 ...

  7. 【Python CheckiO 题解】Time Converter (12h to 24h)

    CheckiO 是面向初学者和高级程序员的编码游戏,使用 Python 和 JavaScript 解决棘手的挑战和有趣的任务,从而提高你的编码技能,本博客主要记录自己用 Python 在闯关时的做题思 ...

  8. 【Python CheckiO 题解】Speech Module

    CheckiO 是面向初学者和高级程序员的编码游戏,使用 Python 和 JavaScript 解决棘手的挑战和有趣的任务,从而提高你的编码技能,本博客主要记录自己用 Python 在闯关时的做题思 ...

  9. 【Python CheckiO 题解】Count Consecutive Summers

    CheckiO 是面向初学者和高级程序员的编码游戏,使用 Python 和 JavaScript 解决棘手的挑战和有趣的任务,从而提高你的编码技能,本博客主要记录自己用 Python 在闯关时的做题思 ...

最新文章

  1. Spring Boot 整合 Mybatis Annotation 注解的完整 Web 案例
  2. VUE中让由全局变量添加生成的新数组不随全局变量的变化而变化
  3. MBG 相关资源链接
  4. 原生js的attachEvent和addEventListener解决window.onload在一个页面只能执行一次的问题
  5. linux 查看ntp版本号_如何让Linux时间与internet时间同步(CentOS)?
  6. 路径总和 II—leetcode113
  7. 深入掌握JMS JMSCorrelationID与Selector
  8. springboot----静态页面templates文件访问
  9. 1618D. Array and Operations
  10. Java从零开始学三(public class和class)
  11. mysql java事物回滚吗_为什么 MySQL 回滚事务也会导致 ibd 文件增大?
  12. spring aop获取目标对象的方法对象(包括方法上的注解)(转)
  13. Luogu1007 独木桥
  14. 量子计算机退相干问题怎么办,量子退相干
  15. centos 6 安装clamav杀毒软件查毒
  16. 如何使用华为官方模拟器eNSP的12800为后续SDN实验做好准备
  17. 2019淘宝最新类目清单免费提供欢迎点赞
  18. wx2540h配置教程_H3C WX2540H系列无线控制器 安装指导-6W101
  19. 光纤通道FC存储交换机的常见问题详解
  20. [教程]安装系统解码器 - 指导教程

热门文章

  1. 5月17日 AJAX 之 XML
  2. link2001错误无法解析外部符号metaObject
  3. IBatis 映射文件 sql 中大于、小于等符号转义
  4. linux 磁盘uuid获取
  5. 在Sql Server 2005使用公用表表达式CTE简化复杂的查询语句
  6. Java虚拟机(JVM)面试题大集合
  7. mysql集群和主从区别_搭建MySQL主从集群,主从复制过程中同步延迟问题
  8. zabbix mysql设置中文乱码_解决zabbix监控因php问题导致图形界面中文乱码方法
  9. 电脑无法打开特定网页_监理检测网校电脑微信无法打开公路试验检测视频课程的处理方法...
  10. 调用线程必须为sta_Java手写分布式系统远程调用RPC框架