LoginSignup
1
0

More than 1 year has passed since last update.

Atcoderサンプルテストを自動化~Pythonバージョン~

Posted at

概要

テストの自動化をこの記事を参考にpython用に作成してみました。
便利…。

ターミナルで打つと

rn a
---------------情報----------------------
パス : /c/Programming/python/atcoder/abc/191
大会レベル : abc
大会番号 : 191
問題番号 : a
URL : https://atcoder.jp/contests/abc191/tasks/abc191_a

[INFO] online-judge-tools 11.5.1 (+ online-judge-api-client 10.10.0)
[INFO] 2 cases found
[WARNING] GNU time is not available: time

[INFO] sample-1
[INFO] time: 0.035234 sec
[SUCCESS] AC

[INFO] sample-2
[INFO] time: 0.033582 sec
[SUCCESS] AC

[INFO] slowest: 0.035234 sec  (for sample-1)
[SUCCESS] test success: 2 cases

ただし自分用のテストケースはまだ非対応なので今後対応予定

詳細は元記事を参照してほしいのでこちらでは実際のコードだけ記載します。

script.sh

script.sh
#!/bin/sh
# 第一引数 問題番号
#  cd abc/190

# 現在のパスを取得
path=$(cd $(dirname $0); pwd)

# コンテストレベル
contestLevel=${path##*atcoder/}
contestLevel=${contestLevel%/*}

# コンテスト番号
contestNumber=$(echo "$path" | sed -e 's/.*\/\([^\/]*\)$/\1/')

# 問題番号
questionNumber="$1"

# AtCoderのURL
URL="https://atcoder.jp/contests/${contestLevel}${contestNumber}/tasks/${contestLevel}${contestNumber}_${questionNumber}"

# サンプルデータダウンロード
oj login "${URL}"
oj dl "${URL}"


echo ""
echo "---------------情報----------------------"

echo "パス : "${path}
echo "大会レベル : "${contestLevel}
echo "大会番号 : "${contestNumber}
echo "問題番号 : "${questionNumber}
echo "URL : "${URL}
echo ""

# サンプルテスト
oj t -c "python ${questionNumber}.py"

# 新しくできたファイルを削除
rm -f a.out
rm -rf test

coppy.sh

coppy.sh
#!/bin/sh

# 第一引数 コンテストレベル
# 第二引数 コンテスト番号
# sh coppy.sh abc 191

# 引数が正しく入力されていない場合
if [ ! $# -eq 2 ];then
  echo """
  -------ERROR-------
  コンテストレベルもしくは番号が正しく入力されていません
  AtCoder ABC 第190回コンテストなら
  sh coppy.sh abc 190
  と入力してください
  """
  exit
fi

# 現在のパスを取得 
path=$(cd $(dirname $0); pwd)

# コンテストレベル
contestLevel="$1"

# コンテスト番号
contestNumber="$2"

# コピー 上書き時警告表示
if [ -e ${path}"/"${contestLevel}"/"${contestNumber} ];then
  echo "同じ名前のファイルが以下のパスで見つかりました"
  echo ${path}"/"${contestLevel}"/"${contestNumber}
  read -p "上書きしますか? (y/N): " yn
  case "$yn" in
    [yY]*) rm -rf ${path}"/"${contestLevel}"/"${contestNumber}
              cp -r coppy ${contestNumber}
              mv ${contestNumber} ${contestLevel}
              echo "完了しました";;
    *) echo "キャンセルしました";;
  esac
else
  cp -r -i coppy ${contestNumber}
  mv ${contestNumber} ${contestLevel}
  echo "完了しました"
fi

最後に

あとはエイリアスの設定とかして終わり

vi ~/.bashrc
# 追加
alias rn='sh script.sh'

# 反映
source ~/.bashrc

便利なのでぜひ使ってみてください。

1
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
1
0