尝试用python解概率题,并祝大小朋友儿童节快乐

article/2025/7/7 1:05:10

实题操作

1. 三个人独立地去破译一份密码,每人能独立译出这份密码的概率分别为1/5, 1/3, 1/4。则这份密码被译出的概率为(3/5)。

def success():p = 1/5,1/3,1/4t = 1for i in p:t *= 1-ireturn 1-tprint(f'成功概率:{success():.3f}')

2. 甲、乙、丙三人向同一飞机射击,假设他们的命中率都是0.4;又若只有一人命中时,飞机坠毁的概率为 0.2;恰有两人命中时,飞机坠毁的概率为 0.6;若三人同时命中,飞机必坠毁。求飞机坠毁的概率为(202/625)。

p1 = [1-0.4,0.4]        # 不中和击种的概率
p2 = [0,0.2,0.6,1.0]    # 不同人数击中的坠机概率
p0 = 0                  # 初始总概率for i in range(2):for j in range(2):for k in range(2):t = p1[i] * p1[j] * p1[k] * p2[i+j+k]p0 += tprint(f'{i} {j} {k} : {p1[i]} * {p1[j]} * {p1[k]} * {p2[i+j+k]:.1f} = {t:.4f}')import fractions 
print(f'\nTotal : {fractions.Fraction(str(0.3232))}')'''
0 0 0 : 0.6 * 0.6 * 0.6 * 0.0 = 0.0000
0 0 1 : 0.6 * 0.6 * 0.4 * 0.2 = 0.0288
0 1 0 : 0.6 * 0.4 * 0.6 * 0.2 = 0.0288
0 1 1 : 0.6 * 0.4 * 0.4 * 0.6 = 0.0576
1 0 0 : 0.4 * 0.6 * 0.6 * 0.2 = 0.0288
1 0 1 : 0.4 * 0.6 * 0.4 * 0.6 = 0.0576
1 1 0 : 0.4 * 0.4 * 0.6 * 0.6 = 0.0576
1 1 1 : 0.4 * 0.4 * 0.4 * 1.0 = 0.0640Total : 202/625
'''

3. 甲、乙、丙三人向同一飞机射击,假设他们的命中率分别是:0.4, 0.5, 0.7;又若只有一人命中时,飞机坠毁的概率为 0.2;恰有两人命中时,飞机坠毁的概率为 0.6;若三人同时命中,飞机必坠毁。求飞机坠毁的概率为(229/500)。

继上题,只是3人的命中率不同;代码稍作修改即可:

p1 = [[1-0.4,0.4],[1-0.5,0.5],[1-0.7,0.7]]
p2 = [0,0.2,0.6,1.0]    # 不同人数击中的坠机概率
p0 = 0                  # 初始总概率for i in range(2):for j in range(2):for k in range(2):t = p1[0][i] * p1[1][j] * p1[2][k] * p2[i+j+k]p0 += tprint(f'{i} {j} {k} : {p1[0][i]:.1f} * {p1[1][j]:.1f} * {p1[2][k]:.1f} * {p2[i+j+k]:.1f} = {t:.4f}')import fractions 
print(f'\nTotal : {fractions.Fraction(str(round(p0,5)))}')'''
0 0 0 : 0.6 * 0.5 * 0.3 * 0.0 = 0.0000
0 0 1 : 0.6 * 0.5 * 0.7 * 0.2 = 0.0420
0 1 0 : 0.6 * 0.5 * 0.3 * 0.2 = 0.0180
0 1 1 : 0.6 * 0.5 * 0.7 * 0.6 = 0.1260
1 0 0 : 0.4 * 0.5 * 0.3 * 0.2 = 0.0120
1 0 1 : 0.4 * 0.5 * 0.7 * 0.6 = 0.0840
1 1 0 : 0.4 * 0.5 * 0.3 * 0.6 = 0.0360
1 1 1 : 0.4 * 0.5 * 0.7 * 1.0 = 0.1400Total : 229/500
'''

实用模块之类方法函数

小数转分数(以下基本是为了凑字数,不喜勿喷忽略即可)

fractions.Fraction

 |      Examples
 |      --------
 |      
 |      >>> Fraction(10, -8)
 |      Fraction(-5, 4)
 |      >>> Fraction(Fraction(1, 7), 5)
 |      Fraction(1, 35)
 |      >>> Fraction(Fraction(1, 7), Fraction(2, 3))
 |      Fraction(3, 14)
 |      >>> Fraction('314')
 |      Fraction(314, 1)
 |      >>> Fraction('-35/4')
 |      Fraction(-35, 4)
 |      >>> Fraction('3.1415') # conversion from numeric string
 |      Fraction(6283, 2000)
 |      >>> Fraction('-47e-2') # string may include a decimal exponent
 |      Fraction(-47, 100)
 |      >>> Fraction(1.47)  # direct construction from float (exact conversion)
 |      Fraction(6620291452234629, 4503599627370496)
 |      >>> Fraction(2.25)
 |      Fraction(9, 4)
 |      >>> Fraction(Decimal('1.47'))
 |      Fraction(147, 100)
 |      >>> Fraction('8.125')
 |      Fraction(65, 8)
 |      >>> print(Fraction('8.125'))
 |      65/8
 |      >>> print(Fraction(0.125))
 |      1/8

另外解决概率题经常要用到排列、组合函数:

itertools.combinations

Help on class combinations in module itertools:

class combinations(builtins.object)
 |  combinations(iterable, r)
 |  
 |  Return successive r-length combinations of elements in the iterable.
 |  
 |  combinations(range(4), 3) --> (0,1,2), (0,1,3), (0,2,3), (1,2,3)
 |  
 |  Methods defined here:
 |  
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |  
 |  __iter__(self, /)
 |      Implement iter(self).
 |  
 |  __next__(self, /)
 |      Implement next(self).
 |  
 |  __reduce__(...)
 |      Return state information for pickling.
 |  
 |  __setstate__(...)
 |      Set state information for unpickling.
 |  
 |  __sizeof__(...)
 |      Returns size in memory, in bytes.
 |  
 |  ----------------------------------------------------------------------
 |  Static methods defined here:
 |  
 |  __new__(*args, **kwargs) from builtins.type
 |      Create and return a new object.  See help(type) for accurate signature.
 

itertools.permutations

Help on class permutations in module itertools:

class permutations(builtins.object)
 |  permutations(iterable, r=None)
 |  
 |  Return successive r-length permutations of elements in the iterable.
 |  
 |  permutations(range(3), 2) --> (0,1), (0,2), (1,0), (1,2), (2,0), (2,1)
 |  
 |  Methods defined here:
 |  
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |  
 |  __iter__(self, /)
 |      Implement iter(self).
 |  
 |  __next__(self, /)
 |      Implement next(self).
 |  
 |  __reduce__(...)
 |      Return state information for pickling.
 |  
 |  __setstate__(...)
 |      Set state information for unpickling.
 |  
 |  __sizeof__(...)
 |      Returns size in memory, in bytes.
 |  
 |  ----------------------------------------------------------------------
 |  Static methods defined here:
 |  
 |  __new__(*args, **kwargs) from builtins.type
 |      Create and return a new object.  See help(type) for accurate signature.
 

还有一个可以取重复值的组合公式,知道的比较少:

tertools.combinations_with_replacement

Help on class combinations_with_replacement in module itertools:

class combinations_with_replacement(builtins.object)
 |  combinations_with_replacement(iterable, r)
 |  
 |  Return successive r-length combinations of elements in the iterable allowing individual elements to have successive repeats.
 |  
 |  combinations_with_replacement('ABC', 2) --> AA AB AC BB BC CC"
 |  
 |  Methods defined here:
 |  
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |  
 |  __iter__(self, /)
 |      Implement iter(self).
 |  
 |  __next__(self, /)
 |      Implement next(self).
 |  
 |  __reduce__(...)
 |      Return state information for pickling.
 |  
 |  __setstate__(...)
 |      Set state information for unpickling.
 |  
 |  __sizeof__(...)
 |      Returns size in memory, in bytes.
 |  
 |  ----------------------------------------------------------------------
 |  Static methods defined here:
 |  
 |  __new__(*args, **kwargs) from builtins.type
 |      Create and return a new object.  See help(type) for accurate signature.
 


http://chatgpt.dhexx.cn/article/P6bduQcs.shtml

相关文章

华为OD机试用Python实现 -【组合出合法最小数】(2023-Q1 新题)

华为OD机试300题大纲 参加华为od机试,一定要注意不要完全背诵代码,需要理解之后模仿写出,通过率才会高。 华为 OD 清单查看地址:blog.csdn.net/hihell/category_12199275.html 华为OD详细说明:https://dream.blog.csdn.net/article/details/128980730 华为OD机试题解:…

华为OD机试用Python实现 -【字母组合】(2023-Q1 新题)

华为OD机试300题大纲 参加华为od机试,一定要注意不要完全背诵代码,需要理解之后模仿写出,通过率才会高。 华为 OD 清单查看地址:blog.csdn.net/hihell/category_12199275.html 华为OD详细说明:https://dream.blog.csdn.net/article/details/128980730 字母组合 | 华为…

提取文本关键词?很 easy 啊,用 Python 三行搞定

从大量文本中提取有用的关键信息是数据分析的一个重要环节。 Python 作为一门广泛应用于数据分析领域的编程语言,有着强大的文本处理库。 整理了几个用于文本关键词提取的优秀工具,一起学习下。 1、jieba库 jieba 是一个中文分词库,可以将一段文本分割为单独的单词。可以…

linux建立phyon文件,PyInstaller 来建立Linux下的Python独立执行文件

以下内容假定已安装好Python 2.4/2.5 一、下载并编译pyinstaller(只需做一次,以后可直接做第二步) 1.下载pyinstaller,现在的版本是1.3 (1)wget http://pyinstaller.hpcf.upr.edu/source/1.3/pyinstaller_1.3.tar.gz 2.解包进入源码目录 (1)tar zxv pyinstaller_1.3…

phyon数据结构

phyon常用的数据结构有:序列和元组 另外不常用的有 :字符串、Unicode字符串、buffer对象、xrange对象 1、索引:跟c中的下标是一个意思 2、分片:就是索引的范围: >> >num[5] [1, 2, 3, 4, 5] >>>…

phyon快速入门(hello phyon)

创建一个phyon项目 点击新建项目 新建phyon文件 新建hellophyon print(hello phyon)在文本中编辑print(hello phyon) 右键run 控制台打印出hello pyhon 第二次运行可直接点击右上角工具栏

学习java好还是phyon好_phyon学习第一天

一直想用phyon写个网络爬虫,太懒了,一直到现在才开始学习 phyon的强大就不用说了,第一天学习,好好加油 今天主要学下一下它的语法: 1、用过简单的计算器使用 程序的计算包括: - * / % **(幂运算符) >…

phyon快速入门(phyon基础知识)

1、创建变量 a10 b2 cab print(c)2、判断语句 # codingutf-8a 90if a > 80:print("nice") elif a > 60:print("normal") else:print("bad")***# codingutf-8***用于指定中文的编码格式 3、循环 # codingutf-8for i in range(1, 100):p…

【MQTT】使用MQTT上报温度阿里云

MQTT上报温度到阿里云 前言iniparser配置文件cJSONsqlite3数据库流程图配置信息发布端代码实现运行结果 前言 在上几篇文章中我们用MQTT.fx模拟客户端实现了与阿里云物联网平台的双向通信,接下来我们自己动手编程使用mosquitto库实现一个发布端。 iniparser配置文件…

实现跨越多个云的无缝云数据管理

By Jeffrey - 资深IT经理人,IT运营和安全顾问,历任多家知名跨国企业包括麦肯锡大中华区、通用电气公司、壳牌石油、英美烟草等公司IT总经理 云已经不是一个新鲜话题,越来越多企业甚至个人已经将数据搬到了云上,享受云带来的便捷&a…

腾讯云运维工程师认证TCA--真题(最新2022.11)

腾讯云运维工程师认证TCA题库,覆盖95%题目。 1、TSF控制台的配置中心,目前支持以下哪一种格式的配置文件? JSON格式XML格式YAML格式Properties配置格式 正确答案:C 解答:无 2、传统应用开发采用瀑布开发模型,瀑布开…

基于OpenPCDet框架的基线模型下载及性能评估

基于OpenPCDet框架的基线模型下载及性能评估 W.P. Xiao, Vision group,SHUSV 版本更新时间更新内容作者1V 1.02021.12更新基线模型W.P. Xiao, Y.Q. Wu2 目录 文章目录 基于OpenPCDet框架的基线模型下载及性能评估基线模型权重下载基线模型性能PointPillarSECONDSECO…

GPT2中文模型本地搭建(二)

GPT2中文模型本地搭建(二) 1、简单介绍1.1 bert4keras是什么,与Keras有什么关系?1.2 常用的预训练模型加载框架有几种?1.3 预训练模型常见版本 2、GPT2-ML 开源中文模型本地搭建2.1 开发环境准备2.2 下载代码2.3 下载模…

阿里云ECS服务器部署

阿里云ECS体验JavaWeb 基础步骤其他操作 第一章 基础步骤 第01节 登录阿里云服务器 在购买ECS服务器后,系统会创建一个ECS实例。每一个ECS实例对应一台已购买的云服务器。 您可以通过电脑上自带的终端工具访问云服务器,进行应用部署和环境搭建。 1. …

AI 框架部署方案之模型部署概述

0 概述 模型训练重点关注的是如何通过训练策略来得到一个性能更好的模型,其过程似乎包含着各种“玄学”,被戏称为“炼丹”。整个流程包含从训练样本的获取(包括数据采集与标注),模型结构的确定,损失函数和评…

调用百度云语音转文本

文章目录 一、创建应用二、调用方式一三、调用方式二四、音频转码工具五、ffmpeg安装六、ffmpeg 使用说明七、ffmpeg命令八、查看音频格式ffprobe使用九、pcm文件音频时长计算十、转换为m4a格式(AAC编码) 一、创建应用 https://console.bce.baidu.com/a…

阿里云代理商:阿里云跨分部抵销前营收267.6亿元,跨分部抵销后营收207.57亿元,抵销后营收环比增长达17.37%。

阿里云代理商聚搜云专业服务于阿里云ECS服务器采购、阿里云Ddos采购、阿里云waf采购、对象存储OSS、阿里云企业邮箱采购、阿里云国际站代理商、阿里云国际站充值、云安全中心(态势感知)、阿里云高可用云数据库RDS、web应用云waf防火墙、阿里云vpc企业网、…

云GIS架构的研究与实践

云GIS架构的研究与实践 摘要1.引言2.云计算(Cloud Computing)3.云GIS4. 云GIS在“多规合一”平台中的实践4.1. 实践思路4.2. 相关产品选择4.3. 私有云环境建设4.4. 云GIS环境建设4.4.1. 镜像库4.4.2. 配置规则4.4.3. 生产站点4.4.4. 部署要点 5.总结参考…

华为云Modelarts

目录 一、什么是ModelArts 二、ModelArts特点 三、ModelArts开发流程 1、进入控制台 2、创建桶 3、自动学习 总结 一、什么是ModelArts ModelArts 是面向开发者的一站式 AI 开发平台,为机器学习与深度学习提供海量数据预处理及交互式智能标注、大规模分布式训…

云原生安全介绍

1 云原生介绍 不同组织对云原生有不同的解释: 云原生概念最早出现于2010年,Paul Fremantle的一篇博客中提及一种架构,其中包括:分布式、松散、自服务、持续部署与测试。 2015年Pivotal公司的Matt Stine在《迁移至云原生应用架构…