LoginSignup
1
1

More than 1 year has passed since last update.

【shell】【online-judge-tools】競技プログラミングの鉄則 演習問題集のサンプルケースを自動で取得する

Last updated at Posted at 2022-10-31

Introduction

online-judge-toolsを使って、サンプルケースをテストしています。競技プログラミングの鉄則を購入し、atcoder上のサンプル問題を使って黙々と説いています。

しかし、毎回リンクをコピペして、下記のコマンドを実行するのはめんどくさいな・・・と感じるようになりました

bash
mkdir ファイル名
oj d link先をコピペ

それなら、シェルでwebスクレイピングして、自動で作ったろ!!と思い立ち、作成しました。

本当はcurlを変数に代入して、それに対してgrepしたかったのですが、思うような結果が得られなかったので、一度grepした結果をtxtに出力しています。その後テキストファイルを読み込んで、ループを回してます。

前提条件

online-judge-toolsの設定が済んでいること


oj login --check "https://atcoder.jp/"

[SUCCESS] You have already signed in

と表示されていれば、多分通ると思います。

今回使用したversion

Ubuntu 22.04 LTS
online-judge-tools 11.5.1
GNU bash, バージョン 5.1.16(1)-release (x86_64-pc-linux-gnu)

作ったスクリプト

a.sh
#競技プログラミングの鉄則URLを取得
URL=https://atcoder.jp/contests/tessoku-book/tasks

curl -fsSL ${URL} | 
grep -oP '(?<=<td class="text-center no-break"><a href=").*(?=">)' >title.txt

curl -fsSL ${URL} |
grep -oP '(?<=<td class="text-center no-break"><a href=").*(?=/a>)'|
grep -oP '(?<=">).*(?=<)'>list.txt




# 区切り文字を改行コードに指定
IFS=$'\n'
 
# ファイルを配列に読み込む
file=(`cat title.txt`)
name=(`cat list.txt`)


# 行ごとに繰り返し処理を実行
for((i=0; i<${#file[@]}; i++))
do
    mkdir ${name[i]}
    cd ${name[i]}
    oj d "https://atcoder.jp"${file[i]}
    cd ..

done

rm title.txt
rm list.txt

実行後

test/
├── A01
│   └── test
│       ├── sample-1.in
│       ├── sample-1.out
│       ├── sample-2.in
│       ├── sample-2.out
│       ├── sample-3.in
│       └── sample-3.out


・・・長いので省略

├── C20
│   └── test
│       ├── sample-1.in
│       └── sample-1.out
└── a.sh

参考サイト

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