ouyhui
BAN USER# -*- coding: utf-8 -*-
__author__ = 'ouyhui'
import heapq
class StudentScores:
def calculate(self, student_scores_file):
file = open(student_scores_file)
student_scores_map = {}
for line in file:
info = line.split(' ')
if info[1] in student_scores_map:
heap = student_scores_map[info[1]]
else:
heap = []
student_scores_map[info[1]] = heap
score = int(info[0])
if len(heap) < 5:
heapq.heappush(heap, score)
elif heap[0] < score:
heapq.heapreplace(heap, score)
file.close()
for student_name, score_heap in student_scores_map.items():
sum = 0
for score in score_heap:
sum += score
average_score = sum / len(score_heap)
print '%s\'s score:%d' % (student_name, average_score)
write by python.
# -*- coding: utf-8 -*-
__author__ = 'ouyhui'
class HostCalculate:
def calculate(self, host_file, host_stat_file):
empty_stat = [0 for i in xrange(3)]
full_stat = [0 for i in xrange(3)]
filled_stat = [0 for i in xrange(6)]
file = open(host_file)
for line in file:
split_index = line.index(',', line.index('M')+3)
host_info = line[:split_index].split(',')
slot_stats = line[split_index+1:]
empty_count = slot_stats.count('0')
slot_count = int(host_info[2])
host_type_index = int(host_info[1][1]) - 1
if empty_count == 0:
full_stat[host_type_index] = full_stat[host_type_index]+ 1
elif empty_count == slot_count:
empty_stat[host_type_index] = empty_stat[host_type_index]+ 1
else:
filled_stat[host_type_index * 2] = filled_stat[host_type_index * 2] + 1
filled_stat[host_type_index * 2 + 1] = filled_stat[host_type_index * 2 + 1] + empty_count
stat_file = open(host_stat_file, 'w')
stat_file.write('EMPTY: M1=%d;M2=%d;M3=%d;\n' % tuple(empty_stat))
stat_file.write('FULL: M1=%d;M2=%d;M3=%d;\n' % tuple(full_stat))
stat_file.write('MOST FILLED: M1=%d,%d;M2=%d,%d;M3=%d,%d;\n' % tuple(filled_stat))
stat_file.close()
code optimization
# -*- coding: utf-8 -*-
__author__ = 'ouyhui'
class BooleanSatisfiabilitySolver:
def count_ways(self, expression):
self.expression = expression
n = (len(expression) + 1) / 2
self.total_count = [1 for i in xrange(n - 1)]
for i in xrange(n - 1):
if i == 0:
continue
sum = 0
for j in xrange(i):
sum += self.total_count[j] * self.total_count[i - j -1]
self.total_count[i] = sum
self.true_count = [[-1 for j in xrange(n + 1)] for i in xrange(n + 1)]
for i in xrange(n):
self.true_count[i][i + 1] = 1 if expression[2 * i] else 0
return self.evaluate(0, n, True)
def evaluate(self, start, end, result=True):
count = 0
if self.true_count[start][end] > -1:
count = self.true_count[start][end]
else:
for i in range(start + 1, end):
operator = self.expression[2 * i - 1]
if operator == '&':
count += self.evaluate(start, i) * self.evaluate(i, end)
elif operator == '|':
count += self.total_count[i-start-1] * self.total_count[end-i-1] \
- self.evaluate(start, i, False) * self.evaluate(i, end, False)
else:
count += self.evaluate(start, i) * self.evaluate(i, end, False)
count += self.evaluate(start, i, False) * self.evaluate(i, end)
self.true_count[start][end] = count
if result:
return count
else:
return self.total_count[end - start - 1] - count
- ouyhui November 20, 2014