山东大学《Python程序设计与大数据分析》lab8

实验八:程序设计方法论

一、实验类型:验证型、设计型
二、建议学时:4
三、实验内容:

1、阅读教材《Python 语言程序设计基础》第8章:程序设计方法论
2、验证型练习:

  • 2.1、验证8.2 实例15:体育竞技分析的整个流程和实例代码
  • 2.2、验证8.7 实例16:pip 安装脚本 (不用提交,直接在自己机器上安装后进行测试)

3、设计型练习:教材P36 程序练习题8.1,8.4

四、实验过程及代码:

①验证性练习:验证8.2 实例15:体育竞技分析的整个流程和实例代码
8.2 实例15:

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("模拟比赛场次:"))
    return a,b,n
def gameover(a,b):
    return a==15 or b==15
def simONEgame(probA,probB):
    scoreA,scoreB=0,0
    serving='A'
    while not gameover(scoreA,scoreB):
        if serving=="A":
            if random()<probA:
                scoreA+=1
            else:
                serving="B"
        else:
            if random()<probB:
                scoreB+=1
            else:
                serving="A"
    return scoreA,scoreB
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 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()
    proA,proB,n=getinputs()
    winsA,winsB=simNgames(n,proA,proB)
    printsummary(winsA,winsB)
main()

验证结果:在这里插入图片描述

②设计型练习:教材P36 程序练习题8.1,8.4
8.1.(只需根据乒乓球比赛规则进行修改即可)

from random import random
from math import fabs
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("模拟比赛场次:"))
    return a,b,n
def notgameover(a,b):
    return (a<=21 and b<=21) or fabs(a-b)<2
def simONEgame(probA,probB):
    scoreA,scoreB=0,0
    serving='A'
    while notgameover(scoreA,scoreB):
        if serving=="A":
            if random()<probA:
                scoreA+=1
            else:
                scoreB+=1
                serving="B"
        else:
            if random()<probB:
                scoreB+=1
            else:
                scoreA+=1
                serving="A"
    return scoreA,scoreB
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 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()
    proA,proB,n=getinputs()
    winsA,winsB=simNgames(n,proA,proB)
    printsummary(winsA,winsB)
main()

验证结果:在这里插入图片描述
8.4.

import jieba.posseg as ps
import jieba
import matplotlib.pyplot as plt
from scipy.misc import imread
from wordcloud import WordCloud,STOPWORDS,ImageColorGenerator
txt = open('三国演义.txt','r',encoding = 'utf-8').read()
counts = {}
def wordAndFrequency(items):
    total = 0
    word_frequency = []
    for i in range(20):
        word, count = items[i]
        a = [word, count]
        total += a[1]
        word_frequency.append(a)
    for i in range(20):
        word_frequency[i][1] = word_frequency[i][1] / total
        word_frequency[i][1] = int(word_frequency[i][1] * 100)
        word_frequency[i][0].encode('utf-8')
        word_frequency[i] = tuple(word_frequency[i])
        print(word_frequency[i])
    return word_frequency
def countFigures():
    words = ps.cut(txt)
    for w in words:
        if len(w.word) == 1:
            continue
        if w.flag == 'nr':
            rword = w.word
            if rword == '玄德' or rword == '玄德曰':
                rword = '刘备'
            if rword == '孔明' or rword == '孔明曰':
                rword = '诸葛亮'
            if rword[-1:] == '兵':
                continue
            counts[rword] = counts.get(rword, 0) + 1
    items = list(counts.items())
    items.sort(key = lambda x:x[1], reverse = True)
    for i in range(20):
        word, count = items[i]
        print('{0:<10}{1:>5}'.format(word, count))
    return wordAndFrequency(items)
def generateWordCloud(word_frequency):
    sanguo_color = imread('三国演义.jpg')
    wc=WordCloud(font_path='C:/Fonts/msyh.ttc',background_color="black", 
                   mask = sanguo_color,
                   random_state=42,
                   margin=5, width=1800, height=800)    wc.generate_from_frequencies(word_frequency)
    image_colors = ImageColorGenerator(sanguo_color)
    plt.imshow(wc.recolor(color_func=image_colors))
    plt.axis('off')
    plt.figure()
    plt.show()
word_frequency = countFigures()
generateWordCloud(word_frequency)
发布了20 篇原创文章 · 获赞 0 · 访问量 334
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 游动-白 设计师: 上身试试

分享到微信朋友圈

×

扫一扫,手机浏览