L = [2, 3, 2, 3, 2, 4]
def mean(L):
total = 0
for i in range(len(L)):
total += L[i]
return (0.0+total)/len(L)
print mean(L)
define max value on the list
def max(L):
max_so_far = L[0]
for i in range(len(L)):
if L[i] > max_so_far: max_so_far = L[i]
return max_so_far
def max(L)
search most popular name in US in 1995
f = open("yob1995.txt", "r")
maxname = "none"
maxval = 0
max2name = "none"
max2val = 0
for line in f:
(name, sex, count) = line.rsplit(",")
count = int(count)
if sex == "F":
if count > maxval:
max2name = maxname
max2val = maxval
maxval = count
maxname = name
elif count > max2val:
max2name = name
max2val = count
print maxname, max2name
print maxval, max2val
L = [31, 45, 91, 51, 66, 82, 28, 33, 11, 89, 84, 27, 36]
def partition(L, v):
smaller = []
bigger = []
for val in L:
if val < v: smaller += [val]
if val > v: bigger += [val]
return smaller + [v] + bigger
print partition(L,84)
L = [31, 45, 91, 51, 66, 82, 28, 33, 11, 89, 84, 27, 36]
def partition(L, v):
smaller = []
bigger = []
for val in L:
if val < v: smaller += [val]
if val > v: bigger += [val]
return (smaller + [v] + bigger)
def top_k(L, k):
v = L[random.randrange(len(L))]
(left,middle,right) = partition(L, y)
if len(left) == k: return left
if len(left)+1 == k: return left+[v]
if len(left) > k: return top_k(left,k)
return left+[v]+top_k(right,k-len(left)-1)
print top_k(L,5)
time1 = time.time()
charG = {}
for char1 in characters:
for book in marvelG[char1]:
make_link(charG, char1, char2)
time2 = time.time()
print "time to compute strengths: ", time2-time1
time1 = time.time()
k = 10
heap = []
for char1 in charaters:
for char2 in charG[char1]:
if characters[char1] < characters[char2]:
if len(heap) < k:
insert_heap(heap, (charG[char1][char2],(char1, char2)))
elif charG[char1][char2] > val(heap[0]):