-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolver_template.py
74 lines (60 loc) · 3.07 KB
/
solver_template.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import os
import sys
sys.path.append('..')
sys.path.append('../..')
import argparse
import utils
from student_utils_sp18 import *
"""
======================================================================
Complete the following function.
======================================================================
"""
def solve(list_of_kingdom_names, starting_kingdom, adjacency_matrix, params=[]):
"""
Write your algorithm here.
Input:
list_of_kingdom_names: An list of kingdom names such that node i of the graph corresponds to name index i in the list
starting_kingdom: The name of the starting kingdom for the walk
adjacency_matrix: The adjacency matrix from the input file
Output:
Return 2 things. The first is a list of kingdoms representing the walk, and the second is the set of kingdoms that are conquered
"""
raise Exception('"solve" function not defined')
# return closed_walk, conquered_kingdoms
"""
======================================================================
No need to change any code below this line
======================================================================
"""
def solve_from_file(input_file, output_directory, params=[]):
print('Processing', input_file)
input_data = utils.read_file(input_file)
number_of_kingdoms, list_of_kingdom_names, starting_kingdom, adjacency_matrix = data_parser(input_data)
closed_walk, conquered_kingdoms = solve(list_of_kingdom_names, starting_kingdom, adjacency_matrix, params=params)
basename, filename = os.path.split(input_file)
output_filename = utils.input_to_output(filename)
output_file = f'{output_directory}/{output_filename}'
if not os.path.exists(output_directory):
os.makedirs(output_directory)
utils.write_data_to_file(output_file, closed_walk, ' ')
utils.write_to_file(output_file, '\n', append=True)
utils.write_data_to_file(output_file, conquered_kingdoms, ' ', append=True)
def solve_all(input_directory, output_directory, params=[]):
input_files = utils.get_files_with_extension(input_directory, 'in')
for input_file in input_files:
solve_from_file(input_file, output_directory, params=params)
if __name__=="__main__":
parser = argparse.ArgumentParser(description='Parsing arguments')
parser.add_argument('--all', action='store_true', help='If specified, the solver is run on all files in the input directory. Else, it is run on just the given input file')
parser.add_argument('input', type=str, help='The path to the input file or directory')
parser.add_argument('output_directory', type=str, nargs='?', default='.', help='The path to the directory where the output should be written')
parser.add_argument('params', nargs=argparse.REMAINDER, help='Extra arguments passed in')
args = parser.parse_args()
output_directory = args.output_directory
if args.all:
input_directory = args.input
solve_all(input_directory, output_directory, params=args.params)
else:
input_file = args.input
solve_from_file(input_file, output_directory, params=args.params)