Exemplo n.º 1
0
def main():
    args = [a for a in sys.argv[1::]]
    # at least 1 arguments has to be passed
    assert len(args) > 0, "No argument specified"

    cwd = os.path.abspath(__file__).replace(__file__, "")

    # input path is generated by the recorded name of the file in FILE_NAMES
    # and the extension stored in IN_EXT if a single letter is passed as argument
    # otherwise the whole argument is taken as relative or full path
    in_path = args[0] if len(args[0]) > 1 else \
        f"{cwd}input/{FILE_NAMES[args[0].lower()]}{IN_EXT}"
    assert os.path.exists(in_path), "File not found"

    # output path is generated by the input path replacing "input" with "output"
    # and the extension with OUT_EXT. Also the date and time is appended
    # to ensure results are not overwritten between runs.
    # Output folder should exist before launching the script
    assert os.path.exists(f"{cwd}output"), "No output folder found"
    out_path = in_path.replace("input", "output")[:-len(IN_EXT)]
    if len(args) == 2:
        out_path = out_path + "-" + args[1]
    out_path = out_path + "-" + date.datetime.now().strftime(
        "%y%m%d-%H%M%S") + OUT_EXT

    print("RUNNING...")
    start_time = time.perf_counter()

    # a list of lines is generated from the input file
    with open(in_path, "r") as inputFile:
        lines = [x.strip() for x in inputFile.readlines()]

    # the list of lines is passed to the main algorithm
    # which returns a new list of lines as result
    out_lines = solution.compute(lines)

    # the output lines are written to the output file
    with open(out_path, "w") as outputFile:
        outputFile.write("\n".join(out_lines))

    elapsed_time = time.perf_counter() - start_time
    print(f"COMPLETED IN {elapsed_time:0.4f}s")
Exemplo n.º 2
0
 def test_compute_inc_a(self):
     self.assertEqual([1, {"a": 1, "b": 0}], compute(["inc a"], getState()))
Exemplo n.º 3
0
 def test_compute_inc_b(self):
     self.assertEqual([1, {"a": 0, "b": 1}], compute(["inc b"], getState()))
Exemplo n.º 4
0
 def test_compute_jio(self):
     self.assertEqual([6, {
         "a": 0,
         "b": 1
     }], compute(["jio a 5", "jio b 5"], getState(b=1)))
Exemplo n.º 5
0
 def test_compute_jmp_simple(self):
     self.assertEqual([5, {"a": 0, "b": 0}], compute(["jmp 5"], getState()))
Exemplo n.º 6
0
 def test_compute_tpl_b(self):
     self.assertEqual([1, {
         "a": 5,
         "b": 15
     }], compute(["tpl b"], getState(a=5, b=5)))
Exemplo n.º 7
0
 def test_compute_hlf_b(self):
     self.assertEqual([1, {
         "a": 5,
         "b": 2
     }], compute(["hlf b"], getState(a=5, b=5)))
Exemplo n.º 8
0
 def test_compute_hlf_a(self):
     self.assertEqual([1, {
         "a": 2,
         "b": 5
     }], compute(["hlf a"], getState(a=5, b=5)))