计算机科学和Python编程导论-第12课

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]: []
计算机科学和Python编程导论-第12课_第1张图片
1.png
In [6]: pylab.plot(xVals, zVals)
Out[6]: []
计算机科学和Python编程导论-第12课_第2张图片
2.png
In [7]: pylab.plot(sorted(xVals), yVals)
Out[7]: []
计算机科学和Python编程导论-第12课_第3张图片
3.png
In [8]: pylab.plot(xVals, sorted(yVals))
Out[8]: []
计算机科学和Python编程导论-第12课_第4张图片
4.png
pylab.plot(sorted(xVals), sorted(yVals))
Out[9]: []
计算机科学和Python编程导论-第12课_第5张图片
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

参考链接:
6.00.2X 计算思维和数据科学导论
edx
EDX-MITx--6.00.2x

你可能感兴趣的:(计算机科学和Python编程导论-第12课)