I'm using Munkres library from https://github.com/bmc/munkres/ to calculate maximum profit (reversed problem).
What do you think about this coding style?
def get_no_of_vowels(str):
return sum(str.lower().count(c) for c in "aeiuoy")
def get_no_of_consonants(str):
return sum(str.lower().count(c) for c in "bcdfghjklmnpqrstvwxz")
def count_letters(str):
return sum(c.isalpha() for c in str)
def compute_ss(name, item):
if not count_letters(item)%2:
ss = get_no_of_vowels(name)*1.5
else:
ss = get_no_of_consonants(name)
if gcd(count_letters(name), count_letters(item)) > 1:
ss*=1.5
return ss
if __name__ == "__main__":
with open(sys.argv[1]) as f:
for line in f:
names = line.strip().split(';')[0].split(',')
items = line.strip().split(';')[1].split(',')
dic = collections.defaultdict(list)
profit_matrix = []
for name in names:
row = []
for item in items:
row.append(compute_ss(name, item))
profit_matrix.append(row)
m = Munkres()
cost_matrix = make_cost_matrix(profit_matrix, lambda x: 1e10 - x)
indexes = m.compute(cost_matrix)
total = 0
for row, column in indexes:
value = profit_matrix[row][column]
# print '(%.2f, %.2f) -> %.2f' % (row, column, value)
total += value
print "%.2f"%total
import
statements. \$\endgroup\$ – Gareth Rees Oct 24 '13 at 16:22