-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem.py
54 lines (50 loc) · 1.74 KB
/
problem.py
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
# Input follows the following form:
# metric: "problem1", "problem2"
#
# metric is one of:
# "same as" "different from" "more important than" "more immediate"
from sys import argv, exit
from collections import defaultdict
def build_weights(problems, key):
for name in problems:
attribute = problems[name][key]
beat = attribute['beat']
beaten = attribute['beaten']
if beat is not 0 or beaten is not 0:
attribute['weight'] = float(beat) / (beat + beaten)
else:
attribute['weight'] = 0.0
return
if len(argv) <= 1:
print "Please provide the input file name."
exit()
else:
try:
f = open(argv[1], 'r')
except:
print "Please enter a valid filename."
exit()
problems = defaultdict(lambda: {'count': 0,
'importance': {'beat': 0, 'beaten': 0},
'immediacy': {'beat': 0, 'beaten': 0},
'similar': defaultdict(lambda: 0)})
# Setup
for line in f:
inp = line.strip()
comp_type, pair = inp.split(': ')
a, b = pair.split(', ')
problems[a]['count'] += 1
problems[b]['count'] += 1
if comp_type is not 'similar':
problems[a][comp_type]['beat'] += 1
problems[b][comp_type]['beaten'] += 1
else:
problems[a][comp_type][b] += 1
problems[b][comp_type][a] += 1
# Calculation
build_weights(problems, 'importance')
build_weights(problems, 'immediacy')
# Output Visualization
for name in problems:
problem = problems[name]
print name, ", ", problem['immediacy']['weight'], ", ", problem['importance']['weight'], ", ", problem['count']