0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

AHC でローカルテストを行う(Python)

Posted at

概要

AHC のローカルテストを Windows で行う方法を残したが、後々スコアを分析したりするには機能が弱いので、Python を通したほうが良さそうな感じがした。Jupyter で、回すのも楽そうだし。

前提

  • AHC の大会で配布しているチェッカを使える状態にしてある
    • vis.exe in.txt out.txt

本編

スコアチェックを Python 上から動かす

Python の subprocess モジュールでコマンド実行する。

  • コマンド実行は subprocess.run(command) で流すが、以下を指定
    • subprocess.run のドキュメント
    • シェル実行を指定するのに shell=True
    • 出力したときのスコア(の文字列)がほしいので stdout=subprocess.PIPE
    • 戻り値が文字列ではなくて、CompletedProcess が返ってくるので、obj.stdout.decode() で文字列を取り出す

commandvis.exe in.txt out.txt を実行するので、これをこのまま文字列で指定

import subprocess

command = "vis.exe in.txt out.txt"

out = subprocess.run(command, shell=True, stdout=subprocess.PIPE)
print(out.stdout.decode())

複数のスコアチェックを Python 上から動かす

複数のファイルが有るなら、in.txt out.txt が変わるだけ。

  • 連番になっているなら for .. in range() で回してもいいし
  • Path オブジェクトでフォルダを取得しておいて、iterdir() で回す、というというのもあるか
# 確認プログラムの実行関数
def check(filename, print_cmd: bool = False):
    # ファイルパス folder_in や folder_out は、計算した結果を入れておくフォルダの Path オブジェクト
    file_in = folder_in.joinpath(filename)
    file_out = folder_out.joinpath(filename)

    command_check = f"{str(file_check)} {str(file_in)} {str(file_out)}"  # 確認プログラムの実行
    if print_cmd:
        print(command_check)

    # コマンド実行
    out = subprocess.run(command_check, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    string_out = out.stdout.decode()
    return int(string_out.split()[2])  # Score = #####


# folder_in 内のファイルを逐次処理 / スコア確認
score_total = 0
count = 0

for p in folder_in.iterdir():
    # execute(p.name)  # 計算実行
    score_total += check(p.name)   # スコア確認事項
    count += 1

これで値は取り出せました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?