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

x33g5p2x  于2022-06-01 转载在 Python  
字(4.6k)|赞(0)|评价(0)|浏览(394)

实题操作

  1. 三个人独立地去破译一份密码,每人能独立译出这份密码的概率分别为1/5, 1/3, 1/4。则这份密码被译出的概率为(3/5)。
def success():
    p = 1/5,1/3,1/4
    t = 1
    for i in p:
        t *= 1-i
    return 1-t
 
print(f'成功概率:{success():.3f}')
  1. 甲、乙、丙三人向同一飞机射击,假设他们的命中率都是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 += t
			print(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.0640

Total : 202/625
'''
  1. 甲、乙、丙三人向同一飞机射击,假设他们的命中率分别是: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 += t
			print(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.1400

Total : 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.
 

相关文章