Python练习题答案: 概率赢得无限的网球比赛【难度:4级】--景越Python编程实例训练营,1000道上机题等你来挑战

概率赢得无限的网球比赛【难度:4级】:

答案1:

def match_probability(p, q):
    l = p**4*(1+4*q+10*q**2)
    r = q**4*(1+4*p+10*p**2)
    m = 1-l-r
    return [l+m*p**2/(1-2*p*q),r+m*q**2/(1-2*p*q)]

答案2:

import math 

def nchoosex(x,n):
    return math.factorial(x)/math.factorial(n)/math.factorial(x-n)

def match_probability(p,q):
    P = 0
    Q = 0
    
    # probability of winning on 4
    P4 = math.pow(p,4)
    P += P4
    
    # probability of losing on 4
    Q4 = math.pow(q,4)
    Q += Q4
    
    # probability of winning on 5
    P5 = math.pow(p,4) * math.pow(q,1)
    # you must win point 5, so you must have lost 1 of the first (4 choose 1)
    P5 *= nchoosex(4,1)
    P += P5
    
    # probability of losing on 5
    Q5 = math.pow(q,4) * math.pow(p,1)
    # you must lose point 5, so you must have won 1 of the first (4 choose 1)
    Q5 *= nchoosex(4,1)
    Q += Q5
    
    # probability of winning on 6
    P6 = math.pow(p,4) * math.pow(q,2)
    # you must win point 6, so you must have lost 2 of the first 5 (5 choose 2)
    P6 *= nchoosex(5,2)
    P += P6
    
    # probability of losing on 6
    Q6 = math.pow(q,4) * math.pow(p,2)
    # you must lose point 6, so you must have won 2 of the first 5 (5 choose 2)
    Q6 *= nchoosex(5,2)
    Q += Q6
    
    # P represents the probability of winning in 6 (or fewer) points
    # Q represents the probability of losing in 6 (or fewer) points
    # 1-P-Q represents the probability of making it to match 6 with a 3-3 score
    PROB_TIE_AT_6 = 1-P-Q
    
    # following points must be played in pairs
    # the probability of player 1 winning each pair is p**2
    # the probability of player 2 winning each pair is q**2
    # the probability each pair will be tied is PROB_PAIR_TIES = 1-p**2-q**2
    PROB_PAIR_TIES = 1-math.pow(p,2)-math.pow(q,2)
    
    # the probability of making it to each subsequent pair
    # is given as PROB_TIE_AT_6 * PROB_PAIR_TIES ** i
    # where i is the number of pairs past the first (7-8)
    
    # the probability of winning on point 8 is
    P8 = PROB_TIE_AT_6 * math.pow(p,2)
    # the probability of losing on point 8 is
    Q8 = PROB_TIE_AT_6 * math.pow(q,2)
    
    # the probability of winning any given pair is
    # Pi = p**2 * PROB_TIE_AT_6 * PROB_PAIR_TIES ** i
    
    CONST = math.pow(p,2) * PROB_TIE_AT_6
    for i in range(0,100):
        P += CONST * math.pow(PROB_PAIR_TIES,i)
        
    return P,1-P​

答案3:

from math import factorial as fact

def match_probability(p, q):
    l = p**4*(1+4*q+10*q**2)
    r = q**4*(1+4*p+10*p**2)
    m = 1-l-r
    return [l+m*p**2/(1-2*p*q),r+m*q**2/(1-2*p*q)]

答案4:

def match_probability(p, q):
    prob=(-8*p**7+28*p**6-34*p**5+15*p**4)/(2*p**2-2*p+1)
    return prob,1-prob​

答案5:

def match_probability(p, q):
    a1 = p**4
    a2 = 4 * p**4 * q
    a3 = 10 * p**4 * q**2
    an = 20 * p**5 * q**3 / (1 - 2 *p* q)
    result = a1 + a2 + a3 + an
    return result, (1 - result)



Python基础训练营景越Python基础训练营QQ群

在这里插入图片描述
欢迎各位同学加群讨论,一起学习,共同成长!

原文链接:加载失败,请重新获取