-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscoring.py
More file actions
68 lines (54 loc) · 2.12 KB
/
scoring.py
File metadata and controls
68 lines (54 loc) · 2.12 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
#!/usr/bin/env python3
import logging
import numpy as np
from .parsing import parse_input, parse_output
class Score(object):
def __init__(self):
self.scores = []
def total(self):
return np.array(self.scores).sum()
def add(self, interest_factor):
self.scores.append(interest_factor)
def set_log_level(args):
if args.debug:
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.INFO)
def compute_score(file_in, file_out):
"""
Compute score (with bonus) of submission
:param file_in: input file
:param file_out: output file (solution)
:return: Score
"""
# read input and output files
input_ = parse_input(file_in)
output_ = parse_output(file_out)
solution_score = np.sum([input_[1][idx] for idx in output_])
score_ = Score()
# check if solution appears in the order items are listed in the input
i = s = 0
while s < len(output_) and i < len(input_[1]):
ip = input_[1][i]
sp = input_[1][output_[s]]
if ip == sp:
i += 1
s += 1
score_.add(sp)
else:
i += 1
assert score_.total() == solution_score, "Solution and score do not match" \
" (expected:{}, found:{})".format(solution_score, score_.total())
assert score_.total() <= input_[0], "Maximum number is reached " \
"(expected:{}, found:{}).".format(input_[0], score_.total())
return score_
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description='print score', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('file_in', type=str, help='input file e.g. a_example.in')
parser.add_argument('file_out', type=str, help='output file e.g. a_example.out')
parser.add_argument('--debug', action='store_true', help='set debug level')
args = parser.parse_args()
set_log_level(args)
score = compute_score(args.file_in, args.file_out)
print("Score for {}: {} points".format(args.file_out, score.total()))