Skip to main content

A simple template generator for competitive programming

Project description

Online Judge Template Generator

test Documentation Status PyPI LICENSE

What is this

競技プログラミングテンプレートを作ってくれるやつです。 kyuridenamida/atcoder-tools を参考に、その本質部分だけを抜き出して再実装しました。 内部構造については How it works に書かれています。

主目的は以下のふたつです:

  • 不適切な入出力方法を用いたことによる TLE を回避すること。たとえば「Codeforces で std::endl を使って TLE する」みたいなのをなくしたい
  • ランダムケースを生成してのテストを気軽に行えるようにすること。たとえば、サンプル AC して提出してみたら謎の WA が出たとき「これランダムケース生成して愚直解と比較すれば原因分かるだろうけど、面倒なんだよな」ってなりがちですが、この面倒を半減させ高速にデバッグできるようにしたい

How to install

$ pip3 install online-judge-template-generator

Usage

oj-template コマンドは、指定された問題に対し、入出力パートを自動生成します。 入出力解析は (精度は別として) たいていのオンラインジャッジで動きます。

$ oj-template [-t TEMPLATE] URL

oj-prepare コマンドは、指定された問題やコンテストに対し、テンプレート生成やサンプルのダウンロードを一括で行います。 oj コマンド が動くやつなら何に対してでも動きます。

$ oj-prepare URL

Supported languages

oj-template が認識する組み込みのテンプレートとして以下が使えます。

  • main.cpp: C++ 解法コード
  • main.py: Python 解法コード
  • generate.py: Python ランダムケース生成器
  • generate.cpp: C++ ランダムケース生成器

(他にもいくつかありますが、それらは突然削除されることがあります。)

Generating random cases

ランダムケースの生成は、oj-prepare コマンドがデフォルトで生成する generate.py を修正した後に、次のように実行してください。

$ oj generate-input "python3 generate.py"

ファイル generate.pyoj-template -t generate.py "https://..." というコマンドの実行によっても生成できます。

また、ランダムケース生成を補助するための module onlinejudge_random が用意されています。詳細はドキュメントを確認してください。

Examples

$ oj-template https://codeforces.com/contest/1300/problem/D
...

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++ (i))
#define REP_R(i, n) for (int i = (int)(n) - 1; (i) >= 0; -- (i))
#define REP3R(i, m, n) for (int i = (int)(n) - 1; (i) >= (int)(m); -- (i))
#define ALL(x) ::std::begin(x), ::std::end(x)
using namespace std;

const string YES = "YES";
const string NO = "nO";
bool solve(int n, const vector<int64_t> & a, const vector<int64_t> & b) {
    // TODO: edit here
}

// generated by online-judge-template-generator v4.4.0 (https://github.com/kmyk/online-judge-template-generator)
int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    constexpr char endl = '\n';
    int n;
    cin >> n;
    vector<int64_t> a(n), b(n);
    REP (i, n) {
        cin >> a[i] >> b[i];
    }
    auto ans = solve(n, a, b);
    cout << (ans ? YES : NO) << endl;
    return 0;
}
$ oj-template -t generate.py https://judge.yosupo.jp/problem/staticrmq
...

#!/usr/bin/env python3
import random
import onlinejudge_random as random_oj

def main():
    N = random.randint(1, 10 ** 9)  # TODO: edit here
    a = [None for _ in range(N)]
    Q = random.randint(1, 10 ** 9)  # TODO: edit here
    l = [None for _ in range(Q)]
    r = [None for _ in range(Q)]
    for i in range(N):
        a[i] = random.randint(1, 10 ** 9)  # TODO: edit here
    for i in range(Q):
        l[i] = random.randint(1, 10 ** 9)  # TODO: edit here
        r[i] = random.randint(1, 10 ** 9)  # TODO: edit here
    print(N, Q)
    print(*[a[i] for i in range(N)])
    for i in range(Q):
        print(l[i], r[i])

if __name__ == "__main__":
    main()
$ oj-prepare https://atcoder.jp/contests/abc158
...

$ tree
.
├── abc158_a
│   ├── main.cpp
│   ├── main.py
│   ├── generate.py
│   └── test
│       ├── sample-1.in
│       ├── sample-1.in
│       ├── sample-1.out
│       ├── sample-2.in
│       ├── sample-2.out
│       ├── sample-3.in
│       └── sample-3.out
├── ...
├── ...
├── ...
├── ...
└── abc158_f
    ├── main.cpp
    ├── main.py
    ├── generate.py
    └── test
        ├── sample-1.in
        ├── sample-1.out
        ├── sample-2.in
        ├── sample-2.out
        ├── sample-3.in
        ├── sample-3.out
        ├── sample-4.in
        └── sample-4.out

13 directories, 50 files

Settings

oj-template

oj-template のためのテンプレートは -t オプションによって指摘できます。 組み込みで用意されているテンプレートの一覧は onlinejudge_template_resources/template/ で見られます。 たとえば generate.cpp を利用したい場合は oj-template -t generate.cpp https://... としてください。

テンプレートを自分で作成することもできます。 テンプレート記法は Mako のものを使います。 fastio.cpp とか customize_sample.cpp とかを見ていい感じに書いてください。 API ドキュメントは onlinejudge_template.generator package にあります。

自分で書いたテンプレートを指定するときは、文字列中にパス区切り文字 / が含まれるようにしてパスを指定してください (シェルスクリプトでの実行ファイルの指定と同様です)。 たとえば customized.py というテンプレートを書いたときは、oj-template -t ./customized.py https://...oj-template -t /path/to/customized.py https://... のように指定してください。 また、ディレクトリ ~/.config/online-judge-tools/template/ の下に ~/.config/online-judge-tools/template/customized.py のようにファイルを配置しておくことで、oj-template -t customized.py https://... のように指定できるようにもなります。~/.config/online-judge-tools/template/ を使えば組み込みのテンプレートを上書きすることができます。

oj-prepare

oj-prepare の設定は ~/.config/online-judge-tools/prepare.config.toml で行えます。 以下のように書いてください。

contest_directory = "~/Desktop/{service_domain}/{contest_id}/{problem_id}"
problem_directory = "."

[templates]
"main.py" = "main.py"
"naive.py" = "main.py"
"generate.py" = "generate.py"

設定項目:

  • problem_directory (string): 問題の URL が指定された場合は {problem_directory} にファイルが用意される。
    • default: .
    • 使える変数:
      • {problem_id}: 問題 ID (abc123_d など)
  • contest_directory (string): コンテストの URL が指定された場合は {contest_directory}/{problem_directory} にファイルが用意される。(default:
    • default: {problem_id}
    • 使える変数:
      • {problem_id}: 問題 ID (abc123_d など)
      • {contest_id}: コンテスト ID (abc123 など)
      • {service_domain}: サービスのドメイン (atcoder.jp など)
      • {service_name}: サービスの名前 (AtCoder など)
  • templates (table of string): value (右側) のテンプレートによる生成結果を key (左側) で指定したパスに配置する。
    • default: { "main.cpp" = "main.cpp", "main.py" = "main.py", "generate.py" = "generate.py" }

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page