-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLec10.py
More file actions
109 lines (92 loc) · 2.57 KB
/
Copy pathLec10.py
File metadata and controls
109 lines (92 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# MIT600《计算机科学及编程导论》(2008年秋季)样码
# 第十讲
# 翻译制作:ocourse.org
# 课程讨论版:http://ocourse.org/bbs/forum.php?mod=forumdisplay&fid=29
# by yoeo24
def merge(left,right):
"""Assum left and right are sorted lists,
return a new sorted list containing the same elements
as (left + right) would contain."""
result = []
i,j = 0,0
while i < len(left) and j < len(right):
if left[i] <= right[j]:
result.append(left[i])
i = i + 1
else:
result.append(right[j])
j = j + 1
while (i < len(left)):
result.append(left[i])
i = i + 1
while (j < len(right)):
result.append(right[j])
j = j + 1
return result
def mergesort(L):
"""Return a new sort list containing the same elements as L"""
print L
if len(L) < 2:
return L[:]
else:
middle = len(L)/2
left = mergesort(L[:middle])
right = mergesort(L[middle:])
together = merge(left,right)
print 'merged', together
return together
def create(smallest, largest):
intSet = []
for i in range(smallest, largest+1): intSet.append(None)
return intSet
def insert(intSet, e):
intSet[e] = 1
def member(intSet, e):
return intSet[e] == 1
def hashChar(c):
# c is a char
# function returns a different integer in the range 0-255
# for each possible value of c
return ord(c)
def cSetCreate():
cSet = []
for i in range(0, 255): cSet.append(None)
return cSet
def cSetInsert(cSet, e):
cSet[hashChar(e)] = 1
def cSetMember(eSet, e):
return cSet[hashChar(e)] == 1
def readFloat(requestMsg, errorMsg):
while True:
val = raw_input(requestMsg)
try:
val = float(val)
return val
except:
print(errorMsg)
#print readFloat('Enter float: ', 'Not a float.')
def readVal(valType, requestMsg, errorMsg):
while True:
val = raw_input(requestMsg)
try:
val = valType(val)
return val
except:
print(errorMsg)
#print readVal(int, 'Enter int: ', 'Not an int.')
def getGrades(fname):
try:
gradesFile = open(fname, 'r')
except IOError:
print 'Could not open', fnmae
raise 'GetGradesError'
grades = []
for line in gradesFile: grades.append(float(line))
return grades
try:
grades = getGrades('q1grades.txt')
grades.sort()
median = grades[len(grades)/2]
print 'Median grade is', median
except 'GetGradesError':
print 'Whoops'