-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparsing.py
More file actions
57 lines (49 loc) · 1.95 KB
/
parsing.py
File metadata and controls
57 lines (49 loc) · 1.95 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
import logging
def parse_input(file_in):
"""
Parse input file
:param file_in: input file name
:return: photos, collection
"""
logging.debug("parsing {}".format(file_in))
input_items = []
with open(file_in, 'r') as f:
first_line = f.readline().strip()
l_ = list(map(int, list(first_line.split(" "))))
m = l_[0]
n = l_[1]
logging.debug("The hub needs {} slices from {} pizza types.".format(m, n))
for pid, line in enumerate(f.readlines()):
_split = line.strip().split(' ')
input_items = list(map(int, list(line.strip().split(' '))))
# check if amount of pizzas matches number of pizza types
assert len(input_items) == n
# check that only one line is read
assert pid == 0
logging.debug("parsing {}: done".format(file_in))
return m, input_items
def parse_output(file_out):
"""
Parse output file
:param file_out: output file name (solution)
:return: n, slides
"""
logging.debug("parsing {}".format(file_out))
solution = []
with open(file_out, 'r') as f:
first_line = f.readline().strip()
n = int(first_line)
for sid, line in enumerate(f.readlines()):
solution = list(map(int, line.strip().split(' ')))
# check if amount of pizzas matches number of pizza types
assert len(solution) == n, "Submission file is not valid: Incorrect number of items" \
" (expected:{}, found:{})".format(n, len(solution))
# check that only one line is read
assert sid == 0
logging.debug("parsing {}: done".format(file_out))
return solution
def write_output(file_out, solution):
logging.debug("writing solution {}".format(file_out))
with open(file_out, 'w') as f:
f.write("{}\n".format(str(len(solution))))
f.write("{}\n".format(" ".join(list(map(str, solution)))))