题目:

Probably Dice

Battle is full of randomnesses. You should observe randomness in a controlled setting to prepare for this inevitability. We'll start by rolling the dice.

Typically, when using multiple dice, you simply roll them and sum up all the result. To get started with your investigation of dice probability, write a function that takes the number of dice, the number of sides per die and a target number and returns the probability of getting a total roll of exactly the target value. The result should be given with four digits precision as ±0.0001. For example, if you roll 2 six-sided dice, the probability of getting exactly a 3 is 2/36 or 5.56%, which you should return as ≈0.0556.

For each test, assume all the dice are the same and are numbered from 1 to the number of sides, inclusive. So a 4-sided die (D4) would have an equal chance of rolling a 1, 2, 3 or 4. A 20-sided die (D20) would have an equal chance of rolling any number from 1 to 20.

Tips: Be careful if you want to use a brute-force solution -- you could have a very, very long wait for edge cases.

Input: Three arguments. The number of dice, the number of sides per die and the target number as integers.

Output: The probability of getting exactly target number on a single roll of the given dice as a float.

我的解法:

 # -*- coding: utf-8 -*-
from collections import Counter #一个骰子出现点数的可能集
def probability1(size):
pro = [i + 1 for i in range(size)]
return pro # print(probability(6)) #求n个骰子出现点数的可能集
def probability_dice(dice_num, size):
pro1 = probability1(size)
pro3 = [x + y for x in pro1 for y in pro1]
for i in range(dice_num - 2):
pro3 = [x + y for x in pro1 for y in pro3]
return pro3 # probability_dice(2,6) def probability(dice_num, size, number):
gen = probability_dice(dice_num, size)
count1 = Counter(gen)
prob_num = count1[number]/len(gen)
prob_num = round(prob_num, 4)
return prob_num

问题:循环次数过多,最后一个测试是(10,10,50),运行出的结果list长度为10^10,程序跑不动了,需要进一步优化算法

 

【Checkio Exercise】Probably Dice的更多相关文章

  1. 【Checkio Exercise】Robot Sort

    Robot Sort All of the refined ingots should be sorted by size in each lot while passing by on a conv ...

  2. 【Checkio Exercise】Three Point Circle

    计算三角形外接圆的函数: Three Point Circle If we want to build new silos, then we need to make more formal and ...

  3. 【codeforces 534C】Polycarpus' Dice

    [题目链接]:http://codeforces.com/contest/534/problem/C [题意] 给你n个奇怪的骰子,第i个骰子有di个面; 然后给你n个骰子的点数之和; 问你每一个骰子 ...

  4. 【Python CheckiO 题解】SP

    题目描述 [Speech Module]:输入一个数字,将其转换成英文表达形式,字符串中的所有单词必须以一个空格字符分隔. [输入]:一个数字(int) [输出]:代表数字的英文字符串(str) [前 ...

  5. 【Python基础】lpthw - Exercise 44 继承与组合

    一.继承 原则:大部分使用继承的场合都可以用组合取代或者简化,而多重继承则需要不惜一切的避免. 1. 什么是继承 继承:用于指明一个类的大部分或者全部功能都是从一个父类获得的.此时,父类的实例的所有动 ...

  6. 【Machine Learning】单参数线性回归 Linear Regression with one variable

        最近开始看斯坦福的公开课<Machine Learning>,对其中单参数的Linear Regression(未涉及Gradient Descent)做个总结吧. [设想]    ...

  7. 【精解】EOS智能合约演练

    EOS,智能合约,abi,wasm,cleos,eosiocpp,开发调试,钱包,账户,签名权限 热身 本文旨在针对EOS智能合约进行一个完整的实操演练,过程中深入熟悉掌握整个EOS智能合约的流程,过 ...

  8. 【学习笔记】JS知识点整理

    1 原型/原型链 1-1 原型 定义:原型是function对象的一个属性,定义了构造函数制造出的对象的公共祖先.通过该构造函数产生的对象,可以继承该原型的属性和方法. 原型是一个对象. 可以利用原型 ...

  9. 【RL系列】Multi-Armed Bandit笔记补充(一)

    在此之前,请先阅读上一篇文章:[RL系列]Multi-Armed Bandit笔记 本篇的主题就如标题所示,只是上一篇文章的补充,主要关注两道来自于Reinforcement Learning: An ...

随机推荐

  1. WordPress 安装插件导致 HTTP 500 内部服务器错误的问题

      春节这几天忙着过节,一直没有看网站,今天登陆上来看到插件有更新,点开更新后,悲剧发生了.页面就无法加载,出现错误无法加载了,着实让我慌了慌(想到重来就郁闷) Chrome:该网页无法正常工作www ...

  2. 利用rqalpha帅选股票 1

    最近股市好了,然后过了3100点后躺着赚钱已经不容易了,股票又太多,想着现在也是做这个东西,倒不如再进一步,把之前研究的量化交易利用起来. rqalpha很早就开源了,之前看过,貌似用来选股什么的很好 ...

  3. Shell常见问题整理

    1. 使用shell进行程序设计的原因是什么? 可以快速.简单的完成编程,非常适合于编写一些执行相对简单的任务的小工具.如果有一个简单的构想,可以通过它检查自己的想法是否可行.还可以使用shell对进 ...

  4. Day8 函数指针做函数参数

    课堂笔记 课程回顾         多态 virtual关键字 纯虚函数 virtual func() = 0;         提前布局vptr指针 面向接口编程 延迟绑定 多态的析构函数的虚函数. ...

  5. Hibernate HQL一对多 在一方的查询

    首先说一句:是版本的问题! 在多对多或者多对一,从一中查找多中查询某些语句时容易出现 我写的hql为: from Department as d where d.employees.name='Tom ...

  6. pandas replace 替换功能function

    list like replace method dict like replace method regex expression import pandas as pd import numpy ...

  7. vue里使用create、mounted调用方法的正确姿势

    参考:https://blog.csdn.net/Honnyee/article/details/82421844

  8. JAVA课程课后作业之使用递归完成回文

    一.思路 1.我的想法是利用数组的做法来进行,先是用scanner录入一个String类 2.然后就是将String转化成char数组 3.递归的就是第一个和最后一个对比,然后第一个加一,最后一个减一 ...

  9. a链接QQ客服 在小框口中打开 感觉不错

    <a href="javascript:;" onClick="javascript:window.open('http://wpa.qq.com/msgrd?v= ...

  10. kubernetes组成

    kubernetes组成 k8s主要包括: kubectl 客户端命令行工具: 将接收的命令,发送给kube-apiserver,作为对整个平台操作的入口. kube-apiserver REST A ...