计算机科学和Python编程导论-第12课
2018-08-15 本文已影响9人
瘦长的丰一禾
PROBLEM 3-X
In [4]: import random, pylab
...: xVals = []
...: yVals = []
...: wVals = []
...: for i in range(1000):
...: xVals.append(random.random())
...: yVals.append(random.random())
...: wVals.append(random.random())
...: xVals = pylab.array(xVals)
...: yVals = pylab.array(yVals)
...: wVals = pylab.array(wVals)
...: xVals = xVals + xVals
...: zVals = xVals + yVals
...: tVals = xVals + yVals + wVals
...:
In [5]: pylab.plot(xVals, yVals)
Out[5]: [<matplotlib.lines.Line2D at 0x113d6c50>]
1.png
In [6]: pylab.plot(xVals, zVals)
Out[6]: [<matplotlib.lines.Line2D at 0x116afa90>]
2.png
In [7]: pylab.plot(sorted(xVals), yVals)
Out[7]: [<matplotlib.lines.Line2D at 0x1173fac8>]
3.png
In [8]: pylab.plot(xVals, sorted(yVals))
Out[8]: [<matplotlib.lines.Line2D at 0x117ce3c8>]
4.png
pylab.plot(sorted(xVals), sorted(yVals))
Out[9]: [<matplotlib.lines.Line2D at 0x11a735f8>]
5.png
qinzc
PROBLEM 5-1
In [17]: import random
...:
...: def sampleQuizzes():
...: def hitsGradeWindow():
...: mid1 = random.randint(50, 81)
...: mid2 = random.randint(60, 91)
...: exam = random.randint(55, 96)
...: g = mid1*0.25 + mid2*0.25 + exam*0.5
...: if g >= 70 and g <= 75:
...: return True
...: return False
...:
...: trials = 10000.0
...: hits = 0
...: for __ in range(int(trials)):
...: if hitsGradeWindow():
...: hits += 1
...:
...: probability = hits / trials
...: return probability
...:
In [18]: sampleQuizzes()
Out[18]: 0.2523
PROBLEM 5-2
In [19]:
...: def plotQuizzes():
...: scores = generateScores(10000)
...: pylab.hist(scores, bins=7)
...: pylab.title('Distribution of Scores')
...: pylab.xlabel('Final Score')
...: pylab.ylabel('Number of Trials')
...: pylab.show()
PROBLEM 6-3
In [23]: def probTest(limit):
...: roll = 1
...: while True:
...: prob = (5**(roll-1)) / (6.0**roll)
...: if prob < limit:
...: return roll
...: roll += 1
...:
In [24]: print (probTest(0.1))
4