杂合集代码

sin、cos函数(0, π/2)

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 2 * np.pi, 200)
y = np.sin(x)  //  or y = np.cos(x)

fig, ax = plt.subplots()
ax.plot(x, y)
plt.show()

sin函数cos函数

 

体育竞技分析
 from random import random

def printIntro():
    print("这个程序模拟两个选手A和B的某种竞技比赛")
    print("程序运行需要A和B的能力值(用0-1之间的小数表示)")

def getInputs():
    a = eval(input("请输入选手A的能力值(0-1):"))
    b = eval(input("请输入选手B的能力值(0-1):"))
    n = eval(input("模拟比赛的场次:"))

def simNGames(n, probA, probB):
    winsA, winsB = 0, 0
    for i in range(n):
        scoreA, scoreB = simOneGame(probA, probB)
        if scoreA > scoreB:
            winsA += 1
        else:
            winsB += 1
    return winsA, winsB

def gameOver(a, b):
    return a==15 or b==15

def simOneGame(proA, proB):
    scoreA, scoreB = 0, 0
    serving = "A"
    while not gameOver(scoreA, scoreB):
        if serving == "A":
            if random() < proA:
                scoreA += 1
            else:
                serving == "B"
        else:
            if random() < proB:
                scoreB += 1
            else:
                serving = "A"
    return scoreA, scoreB

def printSummary(winsA, winsB):
    n = winsA + winsB
    print("竞技分析开始,共模拟{}场比赛".format(n))
    print("选手A获胜{}场比赛,占比{:0.1%}".format(winsA, winsA/n))
    print("选手B获胜{}场比赛,占比{:0.1%}".format(winsB, winsB/n))

def main():
    printIntro()
    probA, probB, n = getInputs()
    winsA, winsB = simNGames(n, probA, probB)
    printSummary(winsA, winsB)

main()

 

jieba与wordcloud
 import jieba
s = jieba.lcut("中国是一个伟大的国家")
print(s)

///

import wordcloud
w = wordcloud.WordCloud()
w.generate("Wordle Wordle Wordle by Python")
w.to_file("SimpleWordle.png")

['中国', '是', '一个', '伟大', '的', '国家'] jieba库生成结果

wordcloud库生成图

 

基本统计值计算
 def getNum():
    nums = []
    istr = input("请输入数字(输入回车退出):")
    while istr != "":
        nums.append(eval(istr))
        istr = input("请输入数字(输入回车退出):")
    return nums

def mean(numbers): #计算平均值
    s = 0.0
    for num in numbers:
        s += num 
    return s / len(numbers)

def dev(numbers, mean):  #计算标准差
    sdev = 0.0
    for num in numbers:
        sdev += (num - mean)**2
    return pow(sdev / len(numbers), 0.5)

def median(numbers):  #计算中位数
    new = sorted(numbers)
    size = len(numbers)
    if size % 2 == 0:
        med = (new[size // 2 - 1] + new[size // 2]) / 2
    else:
        med = new[size // 2]
    return med

n = getNum()
m = mean(n)
print("平均值:{},标准差:{:.2f},中位数:{}.".format(m, dev(n, m), median(n)))
posted @ 2025-05-20 23:11  与尔5  阅读(17)  评论(0)    收藏  举报