Analysis of sports events

Volleyball match analysis

(1) Analysis of N games

(2) Way of thinking: from top to bottom, a complex problem is decomposed into several problems, and then subdivided into specific small problems to solve complex problems. Bottom-up is a reverse process from top to bottom, that is, a method to solve complex problems, and gradually solve small problems to achieve the goal.

(3) System rules: The first four innings use a 25-point system. Each team only wins at least 25 points and exceeds the opponent's 2 points to win 1 inning.

                           Five games and three wins for the official competition, and 15 points for the final game

                           Scoring per goal

1. Decompose the analysis of sports competition into the following small steps

1.1 Print the introductory information of the
program 1.2 Obtain the program running parameters: probA (A's ability value), probB (B's ability value), n (match times)
1.3 Use the team AB's ability value to simulate
1.4 output of n games Games and probability of team AB winning

2. Define each step as a function to achieve

12115116-dbd74c54244ab9ad.png

Complete code

from random import random 

def printIntro ():
     print ( " No. 46 " )
     print ( " This program simulates the volleyball competition of two teams A and B " )
     print ( "The program requires the ability value of A and B (in 0 Decimal to 1) " )
     print ( " {: * ^ 70} " .format ( " Simulation start " )) 
    
def getInputs (): 
    a = eval (input ( " Please enter the team A's ability value ( 0-1): " )) 
    b = eval (input ( "Please enter team B's ability value (0-1):" )) 
    n = eval (input ( " Simulation matches: " ))
     return a, b, n 

def printSummary (winsA, winsB): 
    n = winsA + winsB
     print ( " {: * ^ 70} " .format ( " End of simulation " ))
     print ( " Start of competitive analysis, a total of {} games ". Format (n))
     print ( " Player A wins {} games, accounting for {: 0.1%} " .format (winsA, winsA / n))
     print ( " Player B wins {} games, accounting for {: 0.1%} " .format (winsB,winsB/n))

def GameOver(N,scoreA,scoreB):
    if N <= 4:
        return (scoreA>=25 and scoreB>=25 and abs(scoreA-scoreB)>=2)
    else:
        return (scoreA>=15 and abs(scoreA-scoreB)>=2) or (scoreB>=15 and abs(scoreA-scoreB)>=2)

def simAGame(N,probA,probB):
    scoreA,scoreB=0,0
    serving="A"
    while not GameOver(N,scoreA,scoreB):
        if serving=="A":
            if random() > probA:
                scoreB+=1
                serving="B"
            else:
                scoreA+=1
        else:
            if random() > probB:
                scoreA+=1
                serving="A"
            else:
                scoreB+=1
    return scoreA,scoreB
     
def simOneGame(probA,probB):
    winA,winB=0,0
    for N in range(5):
        scoreA,scoreB=simAGame(N,probA,probB)
        if scoreA > scoreB:
            winA+=1
        else:
            winB+=1
        if winA==3 or winB==3:
            break
    return winA,winB

def simNGames(n,probA,probB):
    winsA,winsB=0,0
    for i in range(n):
        winA,winB=simOneGame(probA,probB)
        if winA > winB:
            winsA+=1
        else:
            winsB+=1
    return winsA,winsB
    
def main():
    printIntro()
    probA,probB,n=getInputs()
    winsA,winsB=simNGames(n,probA,probB)
    printSummary(winsA,winsB)

main()

Guess you like

Origin www.cnblogs.com/yangsenhan/p/12747654.html
Recommended