note:本文所有代码在python3环境下执行,可用jupyter notebook
计算思维的概念
2006年,美国CMU计算机系主任周以真提出
计算思维(Computational Thinking)
运用计算机科学基础概念求解问题、设计系统和理解人
类行为
1.生活中计算思维的应用
我们拥有:四个灶,锅碗瓢盆,食物原料。
我们完成:肉菜、素菜、甜点。
考虑因素:好吃、不能凉、搭配素菜
计算思维的人:有限资源、设定并行流程、得出最好效果计算思维的应用IPO
输入I: 四个灶,一定数量的锅碗瓢盆,食物原料。
处理P: 做饭过程统筹设计
输出O: 肉菜,素菜,甜点。
计算思维的本质
抽象(Abstraction),自动化(Automation)
实证思维,逻辑思维,计算思维
随计算机科学发展而提出
理解计算机特征
将计算特征抽象为计算问题
程序设计实现问题的自动求解计算机模拟解决问题
模拟现实世界计算过程提供一般情况下无法获得的信息
简单的模拟可以揭示某些困难问题的本质规律。
天气预报
飞行器设计
电影特效
核试验模拟示例:体育竞技分析
基本规则
两个球员,交替用球拍击球
发球权,回合
未能进行一次击打回合结束
首先达到15分赢的比赛
该问题的IPO模式
输入I: 两个球员(A和B)的能力值,模拟比赛的 场次
处理P: 模拟比赛过程
输出O: 球员A和B分别赢得球赛的概率
一个期望的输出结果
模拟比赛数量:500
球员A获胜场次:268(53.6%)
球员B获胜场次:232(46.4%)
自顶向下设计
- 基本思想
顶层设计(第一阶段)举例
步骤1:答应程序的介绍性信息
printIntro()
步骤2: 获得程序运行所需的参数:ProA, ProB, n,
probA,probB, n = getInputs()
步骤3: 模拟n次比赛
winsA, winsB = simNGames(n, probA, prpbB)
步骤4: 输出球员A和B获胜比赛的次数和概率
printSummary(winsA,sinsB)
代码:
def main(): printIntro() probA,probB,n = getInputs() winsA, winsB = simNGames(n,probA,probB) printSummary(winsA,winsB)
- 第二阶段:
# printIntro()函数;
def printIntro():
print('This programe simulates a game between two')
print('There ara two player, A and B')
print('Probability(a number between 0 and 1)is used')
# getInputs()函数;
def getInputs():
a = eval(input('What is the prob.player A wins?'))
b = eval(input('What is the prob.player B wins?'))
n = eval(input("How many games to simulate?"))
return a, b, n
# simNGames()函数(核心)
def simNGames(n,probA,probB):
winsA = 0
winsB = 0
for i in range(n):
scoreA,scoreB = simOneGame(probA,probB)
if scoreA > scoreB:
winsA = winsA + 1
else:
winsB = winsB + 1
return winsA, winsB
- 第三阶段
# simOneGame()函数;
def simOneGame(probA,probB):
scoreA = 0
scoreB = 0
serving = 'A'
while <condition>:
<todo>
# simOneGame()函数完整代码:
def simOneGame(probA,probB):
scoreA = 0
scoreB = 0
serving = 'A'
while not gameOver(scoreA,scoreB):
if serving == 'A':
if random() < probA:
scoreA = scoreA + 1
else:
serving = 'B'
else:
if random() < probB:
scoreB = scoreB + 1
else:
serving = 'A'
return scoreA,scoreB
# gameOver()函数:
def gameOver(a,b):
return a==15 or b==15
# printSummary()函数
def printSummary():
n = winsA + winsB
print('\nGames simulated:%d'%n)
print('WinsforA:{0}({1:0.1%})'.format(winsA,winsA/n))
print('Wins for B:{0}({1:0.1%})'.format(winsB,winsB/n))
设计过程总结
自顶向下设计:
步骤1:将算法表达为一系列小问题;
步骤2:为每个小问题设计接口;
步骤3:通过将算法表达为接口关联的多个小问题来细化算法;
步骤4:为每个小问题重复上述过程。
自底向上的执行
自顶向下的设计
从顶层开始分解问题为更小的问题进行求解
自底想上的执行
从底层模块开始一个一个进行测试
程序写好后,需要通过运行程序进行测试
单元测试
小规模程序
直接运行
中等规模
从就结构图底层开始,逐步上升
先运行每个基本函数,在测试整体函数
较大规模
高级软件测试方法体育竞技分析框架
#matchSim.py from randon import random def main(): def printIntro(): def getInput(): def simNGames(): def simOneGame(): def gameOver(): if __name__ == '__main__': main()
体育竞技分析的例子
gameOver单元测试 (1):>>>importmatchSim >>>matchSim.gameOver(0,0) False (2): >>>matchSim.gameOver(5,10) False (3): >>>matchSim.gameOver(15,3) True (4): >>>matchSim.gameOver(3,15) True simOneGame单元测试 (1) >>>importmatchSim >>>matchSim.simOneGame(.5,.5) (13,15) (2) >>>matchSim.simOneGame(.5,.5) (15,11) (3) >>>matchSim.simOneGame(.3,.3) (15,11) (4) >>>matchSim.simOneGame(.3,.3) (11,15)
模拟结果:
>>>matchSim.py What is the prob.player A wins? .65 What is the prob.player B wins? .6 How many games to simulate? 5000 Games simulated:5000 wins for A:3360(67.2%) wins for B:1640(32.8%)
软件开发
软件
能够完成预定功能和性能的可执行的计算机程序,支持程序正常运行的数据,以及描述程序的操作和使用的文档
软件工程
将系统的,严格约束的,可量化的方法应用于软件的开发,运行和维护。
将工程化应用于软件
软件开发生命周期
确定问题