1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
|
%global _empty_manifest_terminate_build 0
Name: python-atcoder-tools
Version: 2.12.0
Release: 1
Summary: Convenient modules & tools for AtCoder users, written in Python 3.6
License: MIT
URL: https://github.com/kyuridenamida/atcoder-tools
Source0: https://mirrors.nju.edu.cn/pypi/web/packages/af/2d/122a100ac575bbfbd70f420af9a818fff7515950b92df3968bdafb87e230/atcoder-tools-2.12.0.tar.gz
BuildArch: noarch
Requires: python3-Jinja2
Requires: python3-beautifulsoup4
Requires: python3-colorama
Requires: python3-requests
Requires: python3-toml
%description
Python 3.6 以降で動作する [AtCoder](https://atcoder.jp/) からサンプル入力をダウンロードしたりする際に便利なツールです。
このツールには次のような機能があります。
- AtCoderへのログイン,入出力例データなどの抽出
- 枝刈り探索による高精度・高速な入力フォーマット解析 (ARC、ABC、AGCについては約9割ほど)
- 問題文中に含まれるMOD値、YES/NO文字列、誤差ジャッジのための誤差値等の定数値抽出
- サンプルのローカルテスト機能
- 誤差ジャッジに対応 by [@chaemon](https://github.com/chaemon/)
- コード提出機能
- 入力フォーマット解析結果や抽出した定数値を用いたテンプレートからのコード自動生成(以下の表に記載されている言語をサポートしています)
- カスタムテンプレートに対応
- 他言語対応のためのコントリビューション(≒中間形式からコードに変換する部分のPR)を募集中です!
|対応言語 |Contributor 1|Contributor 2|
|---:|:---:|:---:|
|C++|[@kyuridenamida](https://github.com/kyuridenamida/) (generator, template)|[@asi1024](https://github.com/asi1024/) (template)|
|Java|[@kyuridenamida](https://github.com/kyuridenamida/) (generator, template)||
|Rust|[@fukatani](https://github.com/fukatani/) (generator, template)|[@koba-e964](https://github.com/koba-e964/) (template, CR)|
|Python3|[@kmyk](https://github.com/kmyk/) (generator, template)|[@penpenpng](https://github.com/penpenpng/) (generator)|
|D|[@penpenpng](https://github.com/penpenpng/) (generator, template)||
|Nim|[@chaemon](https://github.com/chaemon/) (generator, template)||
|C#|[@chaemon](https://github.com/chaemon/) (generator, template)||
|Swift|[@firewood](https://github.com/firewood/) (generator, template)||
|Go|[@nu50218](https://github.com/nu50218/) (generator, template)|[@chaemon](https://github.com/chaemon/) (generator, template)|
|Julia|[@yatra9](https://github.com/yatra9/) (generator, template)|[@chaemon](https://github.com/chaemon/) (generator, template)|
## Demo
<img src="https://user-images.githubusercontent.com/233559/52807100-f6e2d300-30cd-11e9-8906-82b9f9b2dff7.gif" width=70%>
## How to install
`pip3 install atcoder-tools`
ただの`pip`だとPython 2系を使ってインストールされる可能性があるためうまくいかないかもしれません。
## Userscript by [@kmyk](https://github.com/kmyk/) (NEW! 2019/03/06)
Tampermonkey(各種ブラウザで動作)でインストールすることが可能なUserscriptです。公開されている過去問を対象として、atcoder-toolsで自動生成されたコードをそのままAtCoderのスニペット上で利用できます。
1. Tampermonkey をインストールする ([Chrome](https://chrome.google.com/webstore/detail/tampermonkey/dhdgffkkebhmkfjojejmpbldmpobfkfo), [FireFox](https://addons.mozilla.org/en-US/firefox/addon/tampermonkey/))
2. https://kyuridenamida.github.io/atcoder-tools/index.user.js にアクセスしてUserscriptをインストール
3. ログインした状態で適当な問題ページに行く(e.g. https://atcoder.jp/contests/abc120/tasks/abc120_d)
4. 正しくインストールされている場合、ページ下部のコードスニペットにコードが標準で埋め込まれている (atcoder-toolsの対応言語のみ)
<img src="https://user-images.githubusercontent.com/233559/53821542-56d3e780-3fb1-11e9-89d9-24a3d0e9af5c.png" width=50%>
## Analysis
https://kyuridenamida.github.io/atcoder-tools/
各問題ごとの解析結果などが載っています。
## Usage
*重要: かつてパスワード入力なしでログインを実現するために`AccountInformation.py`にログイン情報を書き込むことを要求していましたが、セキュリティリスクが高すぎるため、セッション情報のみを保持する方針に切り替えました。
今後はできるだけ保持されているセッション情報を利用してAtCoderにアクセスし、必要に応じて再入力を要求します。
過去のユーザーの皆様には`AccountInformation.py`を削除して頂くようお願い申し上げます。*
- `atcoder-tools gen {contest_id}` コンテスト環境を用意します。
- `atcoder-tools test` カレント・ディレクトリ上に実行ファイルと入出力(in_\*.txt, out_\*.txt)がある状態で実行するとローカルテストを行います。
- `atcoder-tools submit` カレント・ディレクトリ上で実行すると対応する問題がサンプルに通る場合ソースコードを提出します。既にAtCoder上にその問題に対する提出がある場合、`-u`を指定しないと提出できないようになっています。
- `atcoder-tools version` 現在の atcoder-tools のバージョンを出力します。
`atcoder-tools gen --help`で`atcoder-tools gen`の引数の詳細について確認することができます。
例:
```console
atcoder-tools gen agc001
cd ~/atcoder-workspace/agc001/A
g++ main.cpp
atcoder-tools test
```
`--without-login` 引数を指定するとログインなしでデータをダウンロードできます(一般公開されているコンテストのみ)。
```console
$ atcoder-tools gen [contest_id] --without-login
```
### gen の詳細
```
usage: atcoder-tools gen
[-h] [--without-login] [--workspace WORKSPACE] [--lang LANG]
[--template TEMPLATE] [--parallel] [--save-no-session-cache]
[--skip-existing-problems] [--config CONFIG]
contest_id
positional arguments:
contest_id Contest ID (e.g. arc001)
optional arguments:
-h, --help show this help message and exit
--without-login Download data without login
--workspace WORKSPACE
Path to workspace's root directory. This script will create files in {WORKSPACE}/{contest_name}/{alphabet}/ e.g. ./your-workspace/arc001/A/
[Default] /home/kyuridenamida/atcoder-workspace
--lang LANG Programming language of your template code, cpp or java or rust or python or nim or d or cs or julia.
[Default] cpp
--template TEMPLATE File path to your template code
[Default (C++)] /atcodertools/tools/templates/default_template.cpp
[Default (Java)] /atcodertools/tools/templates/default_template.java
[Default (Rust)] /atcodertools/tools/templates/default_template.rs
[Default (Python3)] /atcodertools/tools/templates/default_template.py
[Default (NIM)] /atcodertools/tools/templates/default_template.nim
[Default (D)] /atcodertools/tools/templates/default_template.d
[Default (C#)] /atcodertools/tools/templates/default_template.cs
--parallel Prepare problem directories asynchronously using multi processors.
--save-no-session-cache
Save no session cache to avoid security risk
--skip-existing-problems
Skip processing every problem for which a directory already exists
--config CONFIG File path to your config file
[Default (Primary)] /home/kyuridenamida/.atcodertools.toml
[Default (Secondary)] /atcoder-tools/atcodertools/tools/atcodertools-default.toml
```
### test の詳細
```
usage: atcoder-tools test [-h] [--exec EXEC] [--num NUM]
[--dir DIR] [--timeout TIMEOUT]
[--knock-out]
[--skip-almost-ac-feedback]
[--judge-type JUDGE_TYPE]
[--error-value ERROR_VALUE]
optional arguments:
-h, --help show this help message and exit
--exec EXEC, -e EXEC File path to the execution target. [Default] Automatically detected exec file
--num NUM, -n NUM The case number to test (1-origin). All cases are tested if not specified.
--dir DIR, -d DIR Target directory to test. [Default] Current directory
--timeout TIMEOUT, -t TIMEOUT
Timeout for each test cases (sec) [Default] 1
--knock-out, -k Stop execution immediately after any example's failure [Default] False
--skip-almost-ac-feedback, -s
Hide inputs and expected/actual outputs if result is correct and there are error outputs [Default] False,
--judge-type JUDGE_TYPE, -j JUDGE_TYPE
error type must be one of [normal, absolute, relative, absolute_or_relative]
--error-value ERROR_VALUE, -v ERROR_VALUE
error value for decimal number judge: [Default] 0.000000001
```
### submit の詳細
```
usage: atcoder-tools submit [-h] [--exec EXEC] [--dir DIR]
[--timeout TIMEOUT] [--code CODE]
[--force] [--save-no-session-cache]
[--unlock-safety]
[--judge-type JUDGE_TYPE]
[--error-value ERROR_VALUE]
optional arguments:
-h, --help show this help message and exit
--exec EXEC, -e EXEC File path to the execution target. [Default] Automatically detected exec file
--dir DIR, -d DIR Target directory to test. [Default] Current directory
--timeout TIMEOUT, -t TIMEOUT
Timeout for each test cases (sec) [Default] 1
--code CODE, -c CODE Path to the source code to submit [Default] Code path written in metadata.json
--force, -f Submit the code regardless of the local test result [Default] False
--save-no-session-cache
Save no session cache to avoid security risk
--unlock-safety, -u By default, this script only submits the first code per problem. However, you can remove the safety by this option in order to submit codes twice or more.
--judge-type JUDGE_TYPE, -j JUDGE_TYPE
error type must be one of [normal, absolute, relative, absolute_or_relative]
--error-value ERROR_VALUE, -v ERROR_VALUE
error value for decimal number judge: [Default] 1e-09
```
### codegen の詳細
```
usage: ./atcoder-tools codegen [-h] [--without-login] [--lang LANG]
[--template TEMPLATE] [--save-no-session-cache]
[--config CONFIG]
url
positional arguments:
url URL (e.g. https://atcoder.jp/contests/abc012/tasks/abc012_3)
optional arguments:
-h, --help show this help message and exit
--without-login Download data without login
--lang LANG Programming language of your template code, cpp or java or rust.
[Default] cpp
--template TEMPLATE File path to your template code
[Default (C++)] /home/user/GitHub/atcoder-tools/atcodertools/tools/templates/default_template.cpp
[Default (Java)] /home/user/GitHub/atcoder-tools/atcodertools/tools/templates/default_template.java
[Default (Rust)] /home/user/GitHub/atcoder-tools/atcodertools/tools/templates/default_template.rs
--save-no-session-cache
Save no session cache to avoid security risk
--config CONFIG File path to your config file
[Default (Primary)] /home/user/.atcodertools.toml
[Default (Secondary)] /home/user/GitHub/atcoder-tools/atcodertools/tools/atcodertools-default.toml
```
## 設定ファイルの例
`~/.atcodertools.toml`に以下の設定を保存すると、コードスタイルや、コード生成後に実行するコマンドを指定できます。
設定ファイルはcodestyle, postprocess, tester, submit, etcのテーブルに分かれていて、codestyle.nimというようにテーブル名の後に.[言語名]で指定するとその言語のみに適用されます。
以下は、次の挙動を期待する場合の`~/.atcodertools.toml`の例です。
- `indent_type='space'` スペースがインデントに使われる(`'tab'`を指定した場合はタブが使われる)
- `indent_width=4` インデント幅は4である (`indent_width`が無指定の場合`4`(nim言語以外), `2`(nim言語)が規定値として使われます。)
- `template_file='~/my_template.cpp'` コード生成テンプレートとして`~/my_template.cpp`を使う
- `workspace_dir='~/atcoder-workspace/'` ワークスペースのルートは `~/atcoder-workspace/`
- `lang='cpp'` 言語設定は `cpp` (提出時もしくはデフォルトのコードジェネレーター生成時に使われます)
- `code_generator_file="~/custom_code_generator.py"` カスタムコードジェネレーター `~/custom_code_generator.py`を指定する
- `code_generator_toml="~/universal_code_generator.toml"` ユニバーサルコードジェネレーター `~/universal_code_generator.toml`を指定する
- `exec_on_each_problem_dir='clang-format -i ./*.cpp'` `exec_on_contest_dir='touch CMakeLists.txt'`
- 問題用ディレクトリ内で毎回`clang-format`を実行して、最後に`CMakeLists.txt`(空)をコンテスト用ディレクトリに生成する
- `compile_before_testing` テスト前にコンパイルを実行するか否かをTrue/Falseで指定。何も指定しないとFalseとなります。
- `compile_only_when_diff_detected` テスト前のコンパイルの際、元のソースに変更があった場合のみ実行するかをTrue/Falseで指定。何も指定しないとFalseとなります。
- `timeout_adjustment=1.2` 問題文に記載された実行時間制限にこの値をかけた秒数がローカルテストの際の実行時間制限になります。例えばatcoderで制限時間2秒の問題は2x1.2=2.4秒となります。atcoderとローカルの実行環境が異なる場合の調整に使用してください。
なお、`compile_before_testing`, `compile_only_when_diff_detected`はいずれもtesterの引数で指定することも可能で、指定した場合はそちらが優先されます。
- `download_without_login=false` AtCoderにログインせずにダウンロードを行う機能を使わない (公開コンテストに対してのみ可能)
- `parallel_download=false` データの並列ダウンロードを無効にする
- `save_no_session_cache=false` ログイン情報のクッキーを保存する
- `skip_existing_problems=false` ディレクトリが既に存在する問題の処理をスキップする
- `in_example_format="in_{}.txt"` テストケース(input)のフォーマットを`in_1.txt, in_2.txt, ...`とする
- `out_example_format="out_{}.txt"` テストケース(output)のフォーマットを`out_1.txt, out_2.txt, ...`とする
```toml
[codestyle]
indent_type='space' # 'tab' or 'space'
indent_width=4
template_file='~/my_template.cpp'
workspace_dir='~/atcoder-workspace/'
lang='cpp' # Check README.md for the supported languages.
code_generator_file="~/custom_code_generator.py"
[postprocess]
exec_on_each_problem_dir='clang-format -i ./*.cpp'
exec_on_contest_dir='touch CMakeLists.txt'
[compiler]
compile_command='g++ main.cpp -o main -std=c++17'
compile_only_when_diff_detected=true
[tester]
compile_before_testing=true
compile_only_when_diff_detected=true
timeout_adjustment=1.2
[etc]
download_without_login=false
parallel_download=false
save_no_session_cache=false
skip_existing_problems=false
in_example_format="in_{}.txt"
out_example_format="out_{}.txt"
```
また、以下のように提出時にコマンドを実行してその結果を提出することが可能です。C++以外のAC-libraryを自動に展開するような用途で用いることができます。下記の例はNim言語でACLのexpanderを実行しその出力ファイルを提出し、その後ローカルの出力ファイルを削除するという設定です。
なお、C++に関してはAC-libraryがatcoderのジャッジにも搭載されているためこのような設定は不要です。
- `exec_before_submit` 提出前に実行するコマンド
- `submit_filename` exec_before_submitを実行した結果提出するファイルが変わる場合に指定
- `exec_after_submit` 提出後に行う処理
```toml
[submit.nim]
exec_before_submit='rm ./combined.nim | python3 ~/git/Nim-ACL/expander.py main.nim --lib /home/chaemon/git/Nim-ACL/ -s'
exec_after_submit='rm ./combined.nim'
submit_filename='./combined.nim'
```
### カスタムコードジェネレーター
[標準のC++コードジェネレーター](https://github.com/kyuridenamida/atcoder-tools/blob/master/atcodertools/codegen/code_generators/cpp.py)に倣って、
`(CogeGenArgs) -> str(ソースコード)`が型であるような`main`関数を定義した.pyファイルを`code_generator_file`で指定すると、コード生成時にカスタムコードジェネレーターを利用できます。
### ユニバーサルコードジェネレーター
ユニバーサルコードジェネレーターはループ・配列アクセス方法等のいくつかの言語仕様を記述するだけでカスタムコードジェネレーターよりも簡単にコード生成することを意図して作成したジェネレーターです。設定ファイルの`code_generator_toml`で指定します。書き方は以下です。
- *base_indent* 入力部分のインデント数
- *insert_space_around_operators* 入力部分の変数や演算子の間にスペースを入れるかどうかをtrue/falseで指定
- *newline_after_input* 入力部分で入力ごとに空行を入れるかどうかをtrue/falseで指定
- *global_prefix* グローバル変数の宣言時に入れる接頭辞(Javaなどでstaticを指定したりできます)
以下のようにテーブルを定義します。各項目はダブルコーテーションあるいはシングルコーテーションを用いた文字列で指定します。Pythonのformatメソッドに渡されるため、波括弧等の文字を直に書きたい場合はエスケープする必要があります。
テーブルのキーは整数(int), 浮動小数(float), 文字列(str), およびこれら3つを使った1次元配列(seq), 2次元配列(2d_seq)となっています。
- *[index]* ループインデックスの名称を指定します。1重目を`i`, 2重目を`j`で指定してください。省略可能で省略した場合はi, jが指定されます。Perl, PHPなどの言語で$i, $jなどとi, j以外の名前を指定しなければならないとき用のつもりです。
- *[loop]* ループに関することを記述します
- **header** ループの最初に記述する内容。ループを回すための変数は`{loop_var}`, 回す回数は`{length}`を用いてください。
- **footer** ループの最後に記述する内容。C++, Javaでは閉じカッコになります。波括弧の場合は`}}`とエスケープする必要があることに注意してください。
- *[type]* タイプ(int, float, str)のタイプについて記述します。例を参照してください。
- *[default]* デフォルトの値について記述します。例を参照してください。注意: TOMLの表記に癖があるようで、ダブルコーテーション2つ(空の文字列)を表記する際にはstr='""'とするとよいようです。"\"\""だとエラーになるようです。
- *[input_func]* int, float, strについて入力時に呼び出す関数を記述します。
- *[arg]* solve関数の引数の記述方法について指定します。`int`, `float`, `str`, `seq`, `2d_seq`について記述してください。`{name}`が変数名, `{type}`が`seq`, `2d_seq`についてベースとなる型です。
- *[actual_arg]* `seq`, `2d_seq`についてsolve関数を呼び出す際の引数の渡し方について記述します。C++などでmoveをつかってメモリを節約したいときなどに指定できます。省略可能で、省略した場合はそのまま渡されます。
- *[access]* 配列のアクセス方法について記述します。`seq`, `2d_seq`について指定してください。`{name}`で変数名, `{index_i}`, `{index_j}`でインデックス名を指定します。
以下は宣言・確保・入力を行うためのコードを記述します。いくつかを同時に行う方法も指定できます。いずれも一行または複数行に渡る指定が可能でセミコロン等の終端子も(必要な言語では)記述してください。
キーワードとして`{name}`, `{type}`はそれぞれ対象となる変数名、タイプ名で、上記で指定した`{default}`が使えます。また、指定していれば`{input_func}`も使えます。`seq`, `2d_seq`の場合は`{type}`はベースとなる型名になります(`vector<int>`における`int`)のでご注意ください。また、`seq`の長さは`{length}`, `2d_seq`の長さは`{length_i}`, `{length_j}`となっています。
- *[declare]* int, float, str, seq, 2d_seqの宣言方法について記述します。
- *[allocate]* `seq`, `2d_seq`の確保の方法を記述します。
- *[declare_and_allocate]* `seq`, `2d_seq`について宣言と確保を同時に行う方法について記述します。
- *[input]* int, float, strの入力方法について記述します。
以下は入力コードの冗長性を下げる目的で指定するテーブルで省略可能なものです。指定方法についてはPythonの設定を参照してください。
- *[allocate_and_input]* `seq`, `2d_seq`について確保と入力をまとめて行うことができる場合に記述します。省略した場合、上記で指定した確保と入力の方式を複合したものが挿入されます
- *[declare_and_allocate_and_input]* `seq`, `2d_seq`について宣言・確保・入力をまとめて行うことができる場合に記述します。省略した場合、上記で指定した宣言と確保と入力の方式を複合したものが挿入されます
例えばC++での設定方法は以下です。
```toml
base_indent = 1
insert_space_around_operators = false
# global変数宣言時の接頭辞
global_prefix = ""
# ループ
[loop]
header = "for(int {loop_var} = 0 ; {loop_var} < {length} ; {loop_var}++){{"
footer = "}}"
# タイプ
[type]
int = "long long"
float = "long double"
str = "std::string"
# デフォルト値
[default]
int = "0"
float = "0.0"
str = '""'
# 引数
[arg]
int = "long long {name}"
float = "long double {name}"
str = "std::string {name}"
seq = "std::vector<{type}> {name}"
2d_seq = "std::vector<std::vector<{type}>> {name}"
# 引数への渡し方
[actual_arg]
seq = "std::move({name})"
2d_seq = "std::move({name})"
# 配列アクセス
[access]
seq = "{name}[{index}]"
2d_seq = "{name}[{index_i}][{index_j}]"
# 宣言
[declare]
int = "long long {name};"
float = "long double {name};"
str = "std::string {name};"
seq = "std::vector<{type}> {name};"
2d_seq = "std::vector<std::vector<{type}>> {name};"
# 確保
[allocate]
seq = "{name}.assign({length}, {default});"
2d_seq = "{name}.assign({length_i}, std::vector<{type}>({length_j}));"
# 宣言と確保
[declare_and_allocate]
seq = "std::vector<{type}> {name}({length});"
2d_seq = "std::vector<std::vector<{type}>> {name}({length_i}, std::vector<{type}>({length_j}));"
# 入力
[input]
#int = "std::cin >> {name};"
int = "std::scanf(\"%lld\", &{name});"
#float = "std::cin >> {name};"
float = "std::scanf(\"%Lf\", &{name});"
str = "std::cin >> {name};"
```
例えばPythonでの設定方法は以下です。
```toml
base_indent = 1
insert_space_around_operators = true
# global変数宣言時の接頭辞
global_prefix = ""
# インデックス
[index]
i = "i"
j = "j"
# ループ
[loop]
header = "for {loop_var} in range({length}):"
footer = ""
# タイプ
[type]
int = "int"
float = "float"
str = "str"
# デフォルト値
[default]
int = "int()"
float = "float()"
str = "str()"
# 宣言
[declare]
int = ""
float = ""
str = ""
seq = ""
2d_seq = ""
# 確保
[allocate]
seq = "{name} = [{default}] * ({length})"
2d_seq = "{name} = [[{default}] * ({length_j}) for _ in {length_i}]"
# 宣言と確保
[declare_and_allocate]
seq = "{name} = [{default}] * ({length}) # type: \"List[{type}]\""
self.declare_and_allocate_2d_seq = "{name} = [[{default}] * ({length_j}) for _ in {length_i}] # type: \"List[List[{type}]]\""
# 入力関数
[input_func]
int = "int(next(tokens))"
float = "float(next(tokens))"
str = "next(tokens)"
# 入力
[input]
int = "{name} = {input_func}"
float = "{name} = {input_func}"
str = "{name} = {input_func}"
# 宣言と入力
[declare_and_input]
int = "{name} = {input_func} # type: int"
float = "{name} = {input_func} # type: float"
str = "{name} = {input_func} # type: str"
# 確保と入力
[allocate_and_input]
seq = "{name} = [{input_func} for _ in range({length})]"
2d_seq = "{name} = [[{input_func} for _ in range({length_j})] for _ in range({length_i})]"
# 宣言と確保と入力
[declare_and_allocate_and_input]
seq = "{name} = [{input_func} for _ in range({length})] # type: \"List[{type}]\""
2d_seq = "{name} = [[{input_func} for _ in range({length_j})] for _ in range({length_i})] # type: \"List[List[{type}]]\""
# 引数
[arg]
int = "{name}: int"
float = "{name}: float"
str = "{name}: str"
seq = "{name}: \"List[{type}]\""
2d_seq = "{name}: \"List[List[{type}]]\""
# 配列アクセス
[access]
seq = "{name}[{index}]"
2d_seq = "{name}[{index_i}][{index_j}]"
```
## テンプレートの例
`atcoder-tools gen`コマンドに対し`--template`でテンプレートソースコードを指定できます。
テンプレートエンジンの仕様については[jinja2](http://jinja.pocoo.org/docs/2.10/) の公式ドキュメントを参照してください。
テンプレートに渡される変数は以下の通りです。
- **prediction_success** 入力形式の推論に成功したとき `True`、 失敗したとき `False`が格納されている。この値が`True`のとき次の3種類の変数も存在することが保証される。
- **input_part** input用のコード
- **formal_arguments** 型つき引数列
- **actual_arguments** 型なし引数列
- **mod** 問題文中に存在するmodの整数値
- **yes_str** 問題文中に存在する yes や possible などの真を表しそうな文字列値
- **no_str** 問題文中に存在する no や impossible などの偽を表しそうな文字列値
```c++
#include <bits/stdc++.h>
using namespace std;
{% if mod %}
const long long MOD = {{ mod }};
{% endif %}
{% if yes_str %}
const string YES = "{{ yes_str }}";
{% endif %}
{% if no_str %}
const string NO = "{{ no_str }}";
{% endif %}
{% if prediction_success %}
void solve({{ formal_arguments }}){
}
{% endif %}
int main(){
{% if prediction_success %}
{{input_part}}
solve({{ actual_arguments }});
{% else %}
// Failed to predict input format
{% endif %}
return 0;
}
```
## Contribution
[CONTRIBUTING.md](CONTRIBUTING.md) を参照してください。
## Licence
[MIT](https://github.com/kyuridenamida/ToolsForAtCoder/blob/master/LICENCE)
## Author
[kyuridenamida](https://github.com/kyuridenamida) ([@kyuridenamida](https://twitter.com/kyuridenamida))
%package -n python3-atcoder-tools
Summary: Convenient modules & tools for AtCoder users, written in Python 3.6
Provides: python-atcoder-tools
BuildRequires: python3-devel
BuildRequires: python3-setuptools
BuildRequires: python3-pip
%description -n python3-atcoder-tools
Python 3.6 以降で動作する [AtCoder](https://atcoder.jp/) からサンプル入力をダウンロードしたりする際に便利なツールです。
このツールには次のような機能があります。
- AtCoderへのログイン,入出力例データなどの抽出
- 枝刈り探索による高精度・高速な入力フォーマット解析 (ARC、ABC、AGCについては約9割ほど)
- 問題文中に含まれるMOD値、YES/NO文字列、誤差ジャッジのための誤差値等の定数値抽出
- サンプルのローカルテスト機能
- 誤差ジャッジに対応 by [@chaemon](https://github.com/chaemon/)
- コード提出機能
- 入力フォーマット解析結果や抽出した定数値を用いたテンプレートからのコード自動生成(以下の表に記載されている言語をサポートしています)
- カスタムテンプレートに対応
- 他言語対応のためのコントリビューション(≒中間形式からコードに変換する部分のPR)を募集中です!
|対応言語 |Contributor 1|Contributor 2|
|---:|:---:|:---:|
|C++|[@kyuridenamida](https://github.com/kyuridenamida/) (generator, template)|[@asi1024](https://github.com/asi1024/) (template)|
|Java|[@kyuridenamida](https://github.com/kyuridenamida/) (generator, template)||
|Rust|[@fukatani](https://github.com/fukatani/) (generator, template)|[@koba-e964](https://github.com/koba-e964/) (template, CR)|
|Python3|[@kmyk](https://github.com/kmyk/) (generator, template)|[@penpenpng](https://github.com/penpenpng/) (generator)|
|D|[@penpenpng](https://github.com/penpenpng/) (generator, template)||
|Nim|[@chaemon](https://github.com/chaemon/) (generator, template)||
|C#|[@chaemon](https://github.com/chaemon/) (generator, template)||
|Swift|[@firewood](https://github.com/firewood/) (generator, template)||
|Go|[@nu50218](https://github.com/nu50218/) (generator, template)|[@chaemon](https://github.com/chaemon/) (generator, template)|
|Julia|[@yatra9](https://github.com/yatra9/) (generator, template)|[@chaemon](https://github.com/chaemon/) (generator, template)|
## Demo
<img src="https://user-images.githubusercontent.com/233559/52807100-f6e2d300-30cd-11e9-8906-82b9f9b2dff7.gif" width=70%>
## How to install
`pip3 install atcoder-tools`
ただの`pip`だとPython 2系を使ってインストールされる可能性があるためうまくいかないかもしれません。
## Userscript by [@kmyk](https://github.com/kmyk/) (NEW! 2019/03/06)
Tampermonkey(各種ブラウザで動作)でインストールすることが可能なUserscriptです。公開されている過去問を対象として、atcoder-toolsで自動生成されたコードをそのままAtCoderのスニペット上で利用できます。
1. Tampermonkey をインストールする ([Chrome](https://chrome.google.com/webstore/detail/tampermonkey/dhdgffkkebhmkfjojejmpbldmpobfkfo), [FireFox](https://addons.mozilla.org/en-US/firefox/addon/tampermonkey/))
2. https://kyuridenamida.github.io/atcoder-tools/index.user.js にアクセスしてUserscriptをインストール
3. ログインした状態で適当な問題ページに行く(e.g. https://atcoder.jp/contests/abc120/tasks/abc120_d)
4. 正しくインストールされている場合、ページ下部のコードスニペットにコードが標準で埋め込まれている (atcoder-toolsの対応言語のみ)
<img src="https://user-images.githubusercontent.com/233559/53821542-56d3e780-3fb1-11e9-89d9-24a3d0e9af5c.png" width=50%>
## Analysis
https://kyuridenamida.github.io/atcoder-tools/
各問題ごとの解析結果などが載っています。
## Usage
*重要: かつてパスワード入力なしでログインを実現するために`AccountInformation.py`にログイン情報を書き込むことを要求していましたが、セキュリティリスクが高すぎるため、セッション情報のみを保持する方針に切り替えました。
今後はできるだけ保持されているセッション情報を利用してAtCoderにアクセスし、必要に応じて再入力を要求します。
過去のユーザーの皆様には`AccountInformation.py`を削除して頂くようお願い申し上げます。*
- `atcoder-tools gen {contest_id}` コンテスト環境を用意します。
- `atcoder-tools test` カレント・ディレクトリ上に実行ファイルと入出力(in_\*.txt, out_\*.txt)がある状態で実行するとローカルテストを行います。
- `atcoder-tools submit` カレント・ディレクトリ上で実行すると対応する問題がサンプルに通る場合ソースコードを提出します。既にAtCoder上にその問題に対する提出がある場合、`-u`を指定しないと提出できないようになっています。
- `atcoder-tools version` 現在の atcoder-tools のバージョンを出力します。
`atcoder-tools gen --help`で`atcoder-tools gen`の引数の詳細について確認することができます。
例:
```console
atcoder-tools gen agc001
cd ~/atcoder-workspace/agc001/A
g++ main.cpp
atcoder-tools test
```
`--without-login` 引数を指定するとログインなしでデータをダウンロードできます(一般公開されているコンテストのみ)。
```console
$ atcoder-tools gen [contest_id] --without-login
```
### gen の詳細
```
usage: atcoder-tools gen
[-h] [--without-login] [--workspace WORKSPACE] [--lang LANG]
[--template TEMPLATE] [--parallel] [--save-no-session-cache]
[--skip-existing-problems] [--config CONFIG]
contest_id
positional arguments:
contest_id Contest ID (e.g. arc001)
optional arguments:
-h, --help show this help message and exit
--without-login Download data without login
--workspace WORKSPACE
Path to workspace's root directory. This script will create files in {WORKSPACE}/{contest_name}/{alphabet}/ e.g. ./your-workspace/arc001/A/
[Default] /home/kyuridenamida/atcoder-workspace
--lang LANG Programming language of your template code, cpp or java or rust or python or nim or d or cs or julia.
[Default] cpp
--template TEMPLATE File path to your template code
[Default (C++)] /atcodertools/tools/templates/default_template.cpp
[Default (Java)] /atcodertools/tools/templates/default_template.java
[Default (Rust)] /atcodertools/tools/templates/default_template.rs
[Default (Python3)] /atcodertools/tools/templates/default_template.py
[Default (NIM)] /atcodertools/tools/templates/default_template.nim
[Default (D)] /atcodertools/tools/templates/default_template.d
[Default (C#)] /atcodertools/tools/templates/default_template.cs
--parallel Prepare problem directories asynchronously using multi processors.
--save-no-session-cache
Save no session cache to avoid security risk
--skip-existing-problems
Skip processing every problem for which a directory already exists
--config CONFIG File path to your config file
[Default (Primary)] /home/kyuridenamida/.atcodertools.toml
[Default (Secondary)] /atcoder-tools/atcodertools/tools/atcodertools-default.toml
```
### test の詳細
```
usage: atcoder-tools test [-h] [--exec EXEC] [--num NUM]
[--dir DIR] [--timeout TIMEOUT]
[--knock-out]
[--skip-almost-ac-feedback]
[--judge-type JUDGE_TYPE]
[--error-value ERROR_VALUE]
optional arguments:
-h, --help show this help message and exit
--exec EXEC, -e EXEC File path to the execution target. [Default] Automatically detected exec file
--num NUM, -n NUM The case number to test (1-origin). All cases are tested if not specified.
--dir DIR, -d DIR Target directory to test. [Default] Current directory
--timeout TIMEOUT, -t TIMEOUT
Timeout for each test cases (sec) [Default] 1
--knock-out, -k Stop execution immediately after any example's failure [Default] False
--skip-almost-ac-feedback, -s
Hide inputs and expected/actual outputs if result is correct and there are error outputs [Default] False,
--judge-type JUDGE_TYPE, -j JUDGE_TYPE
error type must be one of [normal, absolute, relative, absolute_or_relative]
--error-value ERROR_VALUE, -v ERROR_VALUE
error value for decimal number judge: [Default] 0.000000001
```
### submit の詳細
```
usage: atcoder-tools submit [-h] [--exec EXEC] [--dir DIR]
[--timeout TIMEOUT] [--code CODE]
[--force] [--save-no-session-cache]
[--unlock-safety]
[--judge-type JUDGE_TYPE]
[--error-value ERROR_VALUE]
optional arguments:
-h, --help show this help message and exit
--exec EXEC, -e EXEC File path to the execution target. [Default] Automatically detected exec file
--dir DIR, -d DIR Target directory to test. [Default] Current directory
--timeout TIMEOUT, -t TIMEOUT
Timeout for each test cases (sec) [Default] 1
--code CODE, -c CODE Path to the source code to submit [Default] Code path written in metadata.json
--force, -f Submit the code regardless of the local test result [Default] False
--save-no-session-cache
Save no session cache to avoid security risk
--unlock-safety, -u By default, this script only submits the first code per problem. However, you can remove the safety by this option in order to submit codes twice or more.
--judge-type JUDGE_TYPE, -j JUDGE_TYPE
error type must be one of [normal, absolute, relative, absolute_or_relative]
--error-value ERROR_VALUE, -v ERROR_VALUE
error value for decimal number judge: [Default] 1e-09
```
### codegen の詳細
```
usage: ./atcoder-tools codegen [-h] [--without-login] [--lang LANG]
[--template TEMPLATE] [--save-no-session-cache]
[--config CONFIG]
url
positional arguments:
url URL (e.g. https://atcoder.jp/contests/abc012/tasks/abc012_3)
optional arguments:
-h, --help show this help message and exit
--without-login Download data without login
--lang LANG Programming language of your template code, cpp or java or rust.
[Default] cpp
--template TEMPLATE File path to your template code
[Default (C++)] /home/user/GitHub/atcoder-tools/atcodertools/tools/templates/default_template.cpp
[Default (Java)] /home/user/GitHub/atcoder-tools/atcodertools/tools/templates/default_template.java
[Default (Rust)] /home/user/GitHub/atcoder-tools/atcodertools/tools/templates/default_template.rs
--save-no-session-cache
Save no session cache to avoid security risk
--config CONFIG File path to your config file
[Default (Primary)] /home/user/.atcodertools.toml
[Default (Secondary)] /home/user/GitHub/atcoder-tools/atcodertools/tools/atcodertools-default.toml
```
## 設定ファイルの例
`~/.atcodertools.toml`に以下の設定を保存すると、コードスタイルや、コード生成後に実行するコマンドを指定できます。
設定ファイルはcodestyle, postprocess, tester, submit, etcのテーブルに分かれていて、codestyle.nimというようにテーブル名の後に.[言語名]で指定するとその言語のみに適用されます。
以下は、次の挙動を期待する場合の`~/.atcodertools.toml`の例です。
- `indent_type='space'` スペースがインデントに使われる(`'tab'`を指定した場合はタブが使われる)
- `indent_width=4` インデント幅は4である (`indent_width`が無指定の場合`4`(nim言語以外), `2`(nim言語)が規定値として使われます。)
- `template_file='~/my_template.cpp'` コード生成テンプレートとして`~/my_template.cpp`を使う
- `workspace_dir='~/atcoder-workspace/'` ワークスペースのルートは `~/atcoder-workspace/`
- `lang='cpp'` 言語設定は `cpp` (提出時もしくはデフォルトのコードジェネレーター生成時に使われます)
- `code_generator_file="~/custom_code_generator.py"` カスタムコードジェネレーター `~/custom_code_generator.py`を指定する
- `code_generator_toml="~/universal_code_generator.toml"` ユニバーサルコードジェネレーター `~/universal_code_generator.toml`を指定する
- `exec_on_each_problem_dir='clang-format -i ./*.cpp'` `exec_on_contest_dir='touch CMakeLists.txt'`
- 問題用ディレクトリ内で毎回`clang-format`を実行して、最後に`CMakeLists.txt`(空)をコンテスト用ディレクトリに生成する
- `compile_before_testing` テスト前にコンパイルを実行するか否かをTrue/Falseで指定。何も指定しないとFalseとなります。
- `compile_only_when_diff_detected` テスト前のコンパイルの際、元のソースに変更があった場合のみ実行するかをTrue/Falseで指定。何も指定しないとFalseとなります。
- `timeout_adjustment=1.2` 問題文に記載された実行時間制限にこの値をかけた秒数がローカルテストの際の実行時間制限になります。例えばatcoderで制限時間2秒の問題は2x1.2=2.4秒となります。atcoderとローカルの実行環境が異なる場合の調整に使用してください。
なお、`compile_before_testing`, `compile_only_when_diff_detected`はいずれもtesterの引数で指定することも可能で、指定した場合はそちらが優先されます。
- `download_without_login=false` AtCoderにログインせずにダウンロードを行う機能を使わない (公開コンテストに対してのみ可能)
- `parallel_download=false` データの並列ダウンロードを無効にする
- `save_no_session_cache=false` ログイン情報のクッキーを保存する
- `skip_existing_problems=false` ディレクトリが既に存在する問題の処理をスキップする
- `in_example_format="in_{}.txt"` テストケース(input)のフォーマットを`in_1.txt, in_2.txt, ...`とする
- `out_example_format="out_{}.txt"` テストケース(output)のフォーマットを`out_1.txt, out_2.txt, ...`とする
```toml
[codestyle]
indent_type='space' # 'tab' or 'space'
indent_width=4
template_file='~/my_template.cpp'
workspace_dir='~/atcoder-workspace/'
lang='cpp' # Check README.md for the supported languages.
code_generator_file="~/custom_code_generator.py"
[postprocess]
exec_on_each_problem_dir='clang-format -i ./*.cpp'
exec_on_contest_dir='touch CMakeLists.txt'
[compiler]
compile_command='g++ main.cpp -o main -std=c++17'
compile_only_when_diff_detected=true
[tester]
compile_before_testing=true
compile_only_when_diff_detected=true
timeout_adjustment=1.2
[etc]
download_without_login=false
parallel_download=false
save_no_session_cache=false
skip_existing_problems=false
in_example_format="in_{}.txt"
out_example_format="out_{}.txt"
```
また、以下のように提出時にコマンドを実行してその結果を提出することが可能です。C++以外のAC-libraryを自動に展開するような用途で用いることができます。下記の例はNim言語でACLのexpanderを実行しその出力ファイルを提出し、その後ローカルの出力ファイルを削除するという設定です。
なお、C++に関してはAC-libraryがatcoderのジャッジにも搭載されているためこのような設定は不要です。
- `exec_before_submit` 提出前に実行するコマンド
- `submit_filename` exec_before_submitを実行した結果提出するファイルが変わる場合に指定
- `exec_after_submit` 提出後に行う処理
```toml
[submit.nim]
exec_before_submit='rm ./combined.nim | python3 ~/git/Nim-ACL/expander.py main.nim --lib /home/chaemon/git/Nim-ACL/ -s'
exec_after_submit='rm ./combined.nim'
submit_filename='./combined.nim'
```
### カスタムコードジェネレーター
[標準のC++コードジェネレーター](https://github.com/kyuridenamida/atcoder-tools/blob/master/atcodertools/codegen/code_generators/cpp.py)に倣って、
`(CogeGenArgs) -> str(ソースコード)`が型であるような`main`関数を定義した.pyファイルを`code_generator_file`で指定すると、コード生成時にカスタムコードジェネレーターを利用できます。
### ユニバーサルコードジェネレーター
ユニバーサルコードジェネレーターはループ・配列アクセス方法等のいくつかの言語仕様を記述するだけでカスタムコードジェネレーターよりも簡単にコード生成することを意図して作成したジェネレーターです。設定ファイルの`code_generator_toml`で指定します。書き方は以下です。
- *base_indent* 入力部分のインデント数
- *insert_space_around_operators* 入力部分の変数や演算子の間にスペースを入れるかどうかをtrue/falseで指定
- *newline_after_input* 入力部分で入力ごとに空行を入れるかどうかをtrue/falseで指定
- *global_prefix* グローバル変数の宣言時に入れる接頭辞(Javaなどでstaticを指定したりできます)
以下のようにテーブルを定義します。各項目はダブルコーテーションあるいはシングルコーテーションを用いた文字列で指定します。Pythonのformatメソッドに渡されるため、波括弧等の文字を直に書きたい場合はエスケープする必要があります。
テーブルのキーは整数(int), 浮動小数(float), 文字列(str), およびこれら3つを使った1次元配列(seq), 2次元配列(2d_seq)となっています。
- *[index]* ループインデックスの名称を指定します。1重目を`i`, 2重目を`j`で指定してください。省略可能で省略した場合はi, jが指定されます。Perl, PHPなどの言語で$i, $jなどとi, j以外の名前を指定しなければならないとき用のつもりです。
- *[loop]* ループに関することを記述します
- **header** ループの最初に記述する内容。ループを回すための変数は`{loop_var}`, 回す回数は`{length}`を用いてください。
- **footer** ループの最後に記述する内容。C++, Javaでは閉じカッコになります。波括弧の場合は`}}`とエスケープする必要があることに注意してください。
- *[type]* タイプ(int, float, str)のタイプについて記述します。例を参照してください。
- *[default]* デフォルトの値について記述します。例を参照してください。注意: TOMLの表記に癖があるようで、ダブルコーテーション2つ(空の文字列)を表記する際にはstr='""'とするとよいようです。"\"\""だとエラーになるようです。
- *[input_func]* int, float, strについて入力時に呼び出す関数を記述します。
- *[arg]* solve関数の引数の記述方法について指定します。`int`, `float`, `str`, `seq`, `2d_seq`について記述してください。`{name}`が変数名, `{type}`が`seq`, `2d_seq`についてベースとなる型です。
- *[actual_arg]* `seq`, `2d_seq`についてsolve関数を呼び出す際の引数の渡し方について記述します。C++などでmoveをつかってメモリを節約したいときなどに指定できます。省略可能で、省略した場合はそのまま渡されます。
- *[access]* 配列のアクセス方法について記述します。`seq`, `2d_seq`について指定してください。`{name}`で変数名, `{index_i}`, `{index_j}`でインデックス名を指定します。
以下は宣言・確保・入力を行うためのコードを記述します。いくつかを同時に行う方法も指定できます。いずれも一行または複数行に渡る指定が可能でセミコロン等の終端子も(必要な言語では)記述してください。
キーワードとして`{name}`, `{type}`はそれぞれ対象となる変数名、タイプ名で、上記で指定した`{default}`が使えます。また、指定していれば`{input_func}`も使えます。`seq`, `2d_seq`の場合は`{type}`はベースとなる型名になります(`vector<int>`における`int`)のでご注意ください。また、`seq`の長さは`{length}`, `2d_seq`の長さは`{length_i}`, `{length_j}`となっています。
- *[declare]* int, float, str, seq, 2d_seqの宣言方法について記述します。
- *[allocate]* `seq`, `2d_seq`の確保の方法を記述します。
- *[declare_and_allocate]* `seq`, `2d_seq`について宣言と確保を同時に行う方法について記述します。
- *[input]* int, float, strの入力方法について記述します。
以下は入力コードの冗長性を下げる目的で指定するテーブルで省略可能なものです。指定方法についてはPythonの設定を参照してください。
- *[allocate_and_input]* `seq`, `2d_seq`について確保と入力をまとめて行うことができる場合に記述します。省略した場合、上記で指定した確保と入力の方式を複合したものが挿入されます
- *[declare_and_allocate_and_input]* `seq`, `2d_seq`について宣言・確保・入力をまとめて行うことができる場合に記述します。省略した場合、上記で指定した宣言と確保と入力の方式を複合したものが挿入されます
例えばC++での設定方法は以下です。
```toml
base_indent = 1
insert_space_around_operators = false
# global変数宣言時の接頭辞
global_prefix = ""
# ループ
[loop]
header = "for(int {loop_var} = 0 ; {loop_var} < {length} ; {loop_var}++){{"
footer = "}}"
# タイプ
[type]
int = "long long"
float = "long double"
str = "std::string"
# デフォルト値
[default]
int = "0"
float = "0.0"
str = '""'
# 引数
[arg]
int = "long long {name}"
float = "long double {name}"
str = "std::string {name}"
seq = "std::vector<{type}> {name}"
2d_seq = "std::vector<std::vector<{type}>> {name}"
# 引数への渡し方
[actual_arg]
seq = "std::move({name})"
2d_seq = "std::move({name})"
# 配列アクセス
[access]
seq = "{name}[{index}]"
2d_seq = "{name}[{index_i}][{index_j}]"
# 宣言
[declare]
int = "long long {name};"
float = "long double {name};"
str = "std::string {name};"
seq = "std::vector<{type}> {name};"
2d_seq = "std::vector<std::vector<{type}>> {name};"
# 確保
[allocate]
seq = "{name}.assign({length}, {default});"
2d_seq = "{name}.assign({length_i}, std::vector<{type}>({length_j}));"
# 宣言と確保
[declare_and_allocate]
seq = "std::vector<{type}> {name}({length});"
2d_seq = "std::vector<std::vector<{type}>> {name}({length_i}, std::vector<{type}>({length_j}));"
# 入力
[input]
#int = "std::cin >> {name};"
int = "std::scanf(\"%lld\", &{name});"
#float = "std::cin >> {name};"
float = "std::scanf(\"%Lf\", &{name});"
str = "std::cin >> {name};"
```
例えばPythonでの設定方法は以下です。
```toml
base_indent = 1
insert_space_around_operators = true
# global変数宣言時の接頭辞
global_prefix = ""
# インデックス
[index]
i = "i"
j = "j"
# ループ
[loop]
header = "for {loop_var} in range({length}):"
footer = ""
# タイプ
[type]
int = "int"
float = "float"
str = "str"
# デフォルト値
[default]
int = "int()"
float = "float()"
str = "str()"
# 宣言
[declare]
int = ""
float = ""
str = ""
seq = ""
2d_seq = ""
# 確保
[allocate]
seq = "{name} = [{default}] * ({length})"
2d_seq = "{name} = [[{default}] * ({length_j}) for _ in {length_i}]"
# 宣言と確保
[declare_and_allocate]
seq = "{name} = [{default}] * ({length}) # type: \"List[{type}]\""
self.declare_and_allocate_2d_seq = "{name} = [[{default}] * ({length_j}) for _ in {length_i}] # type: \"List[List[{type}]]\""
# 入力関数
[input_func]
int = "int(next(tokens))"
float = "float(next(tokens))"
str = "next(tokens)"
# 入力
[input]
int = "{name} = {input_func}"
float = "{name} = {input_func}"
str = "{name} = {input_func}"
# 宣言と入力
[declare_and_input]
int = "{name} = {input_func} # type: int"
float = "{name} = {input_func} # type: float"
str = "{name} = {input_func} # type: str"
# 確保と入力
[allocate_and_input]
seq = "{name} = [{input_func} for _ in range({length})]"
2d_seq = "{name} = [[{input_func} for _ in range({length_j})] for _ in range({length_i})]"
# 宣言と確保と入力
[declare_and_allocate_and_input]
seq = "{name} = [{input_func} for _ in range({length})] # type: \"List[{type}]\""
2d_seq = "{name} = [[{input_func} for _ in range({length_j})] for _ in range({length_i})] # type: \"List[List[{type}]]\""
# 引数
[arg]
int = "{name}: int"
float = "{name}: float"
str = "{name}: str"
seq = "{name}: \"List[{type}]\""
2d_seq = "{name}: \"List[List[{type}]]\""
# 配列アクセス
[access]
seq = "{name}[{index}]"
2d_seq = "{name}[{index_i}][{index_j}]"
```
## テンプレートの例
`atcoder-tools gen`コマンドに対し`--template`でテンプレートソースコードを指定できます。
テンプレートエンジンの仕様については[jinja2](http://jinja.pocoo.org/docs/2.10/) の公式ドキュメントを参照してください。
テンプレートに渡される変数は以下の通りです。
- **prediction_success** 入力形式の推論に成功したとき `True`、 失敗したとき `False`が格納されている。この値が`True`のとき次の3種類の変数も存在することが保証される。
- **input_part** input用のコード
- **formal_arguments** 型つき引数列
- **actual_arguments** 型なし引数列
- **mod** 問題文中に存在するmodの整数値
- **yes_str** 問題文中に存在する yes や possible などの真を表しそうな文字列値
- **no_str** 問題文中に存在する no や impossible などの偽を表しそうな文字列値
```c++
#include <bits/stdc++.h>
using namespace std;
{% if mod %}
const long long MOD = {{ mod }};
{% endif %}
{% if yes_str %}
const string YES = "{{ yes_str }}";
{% endif %}
{% if no_str %}
const string NO = "{{ no_str }}";
{% endif %}
{% if prediction_success %}
void solve({{ formal_arguments }}){
}
{% endif %}
int main(){
{% if prediction_success %}
{{input_part}}
solve({{ actual_arguments }});
{% else %}
// Failed to predict input format
{% endif %}
return 0;
}
```
## Contribution
[CONTRIBUTING.md](CONTRIBUTING.md) を参照してください。
## Licence
[MIT](https://github.com/kyuridenamida/ToolsForAtCoder/blob/master/LICENCE)
## Author
[kyuridenamida](https://github.com/kyuridenamida) ([@kyuridenamida](https://twitter.com/kyuridenamida))
%package help
Summary: Development documents and examples for atcoder-tools
Provides: python3-atcoder-tools-doc
%description help
Python 3.6 以降で動作する [AtCoder](https://atcoder.jp/) からサンプル入力をダウンロードしたりする際に便利なツールです。
このツールには次のような機能があります。
- AtCoderへのログイン,入出力例データなどの抽出
- 枝刈り探索による高精度・高速な入力フォーマット解析 (ARC、ABC、AGCについては約9割ほど)
- 問題文中に含まれるMOD値、YES/NO文字列、誤差ジャッジのための誤差値等の定数値抽出
- サンプルのローカルテスト機能
- 誤差ジャッジに対応 by [@chaemon](https://github.com/chaemon/)
- コード提出機能
- 入力フォーマット解析結果や抽出した定数値を用いたテンプレートからのコード自動生成(以下の表に記載されている言語をサポートしています)
- カスタムテンプレートに対応
- 他言語対応のためのコントリビューション(≒中間形式からコードに変換する部分のPR)を募集中です!
|対応言語 |Contributor 1|Contributor 2|
|---:|:---:|:---:|
|C++|[@kyuridenamida](https://github.com/kyuridenamida/) (generator, template)|[@asi1024](https://github.com/asi1024/) (template)|
|Java|[@kyuridenamida](https://github.com/kyuridenamida/) (generator, template)||
|Rust|[@fukatani](https://github.com/fukatani/) (generator, template)|[@koba-e964](https://github.com/koba-e964/) (template, CR)|
|Python3|[@kmyk](https://github.com/kmyk/) (generator, template)|[@penpenpng](https://github.com/penpenpng/) (generator)|
|D|[@penpenpng](https://github.com/penpenpng/) (generator, template)||
|Nim|[@chaemon](https://github.com/chaemon/) (generator, template)||
|C#|[@chaemon](https://github.com/chaemon/) (generator, template)||
|Swift|[@firewood](https://github.com/firewood/) (generator, template)||
|Go|[@nu50218](https://github.com/nu50218/) (generator, template)|[@chaemon](https://github.com/chaemon/) (generator, template)|
|Julia|[@yatra9](https://github.com/yatra9/) (generator, template)|[@chaemon](https://github.com/chaemon/) (generator, template)|
## Demo
<img src="https://user-images.githubusercontent.com/233559/52807100-f6e2d300-30cd-11e9-8906-82b9f9b2dff7.gif" width=70%>
## How to install
`pip3 install atcoder-tools`
ただの`pip`だとPython 2系を使ってインストールされる可能性があるためうまくいかないかもしれません。
## Userscript by [@kmyk](https://github.com/kmyk/) (NEW! 2019/03/06)
Tampermonkey(各種ブラウザで動作)でインストールすることが可能なUserscriptです。公開されている過去問を対象として、atcoder-toolsで自動生成されたコードをそのままAtCoderのスニペット上で利用できます。
1. Tampermonkey をインストールする ([Chrome](https://chrome.google.com/webstore/detail/tampermonkey/dhdgffkkebhmkfjojejmpbldmpobfkfo), [FireFox](https://addons.mozilla.org/en-US/firefox/addon/tampermonkey/))
2. https://kyuridenamida.github.io/atcoder-tools/index.user.js にアクセスしてUserscriptをインストール
3. ログインした状態で適当な問題ページに行く(e.g. https://atcoder.jp/contests/abc120/tasks/abc120_d)
4. 正しくインストールされている場合、ページ下部のコードスニペットにコードが標準で埋め込まれている (atcoder-toolsの対応言語のみ)
<img src="https://user-images.githubusercontent.com/233559/53821542-56d3e780-3fb1-11e9-89d9-24a3d0e9af5c.png" width=50%>
## Analysis
https://kyuridenamida.github.io/atcoder-tools/
各問題ごとの解析結果などが載っています。
## Usage
*重要: かつてパスワード入力なしでログインを実現するために`AccountInformation.py`にログイン情報を書き込むことを要求していましたが、セキュリティリスクが高すぎるため、セッション情報のみを保持する方針に切り替えました。
今後はできるだけ保持されているセッション情報を利用してAtCoderにアクセスし、必要に応じて再入力を要求します。
過去のユーザーの皆様には`AccountInformation.py`を削除して頂くようお願い申し上げます。*
- `atcoder-tools gen {contest_id}` コンテスト環境を用意します。
- `atcoder-tools test` カレント・ディレクトリ上に実行ファイルと入出力(in_\*.txt, out_\*.txt)がある状態で実行するとローカルテストを行います。
- `atcoder-tools submit` カレント・ディレクトリ上で実行すると対応する問題がサンプルに通る場合ソースコードを提出します。既にAtCoder上にその問題に対する提出がある場合、`-u`を指定しないと提出できないようになっています。
- `atcoder-tools version` 現在の atcoder-tools のバージョンを出力します。
`atcoder-tools gen --help`で`atcoder-tools gen`の引数の詳細について確認することができます。
例:
```console
atcoder-tools gen agc001
cd ~/atcoder-workspace/agc001/A
g++ main.cpp
atcoder-tools test
```
`--without-login` 引数を指定するとログインなしでデータをダウンロードできます(一般公開されているコンテストのみ)。
```console
$ atcoder-tools gen [contest_id] --without-login
```
### gen の詳細
```
usage: atcoder-tools gen
[-h] [--without-login] [--workspace WORKSPACE] [--lang LANG]
[--template TEMPLATE] [--parallel] [--save-no-session-cache]
[--skip-existing-problems] [--config CONFIG]
contest_id
positional arguments:
contest_id Contest ID (e.g. arc001)
optional arguments:
-h, --help show this help message and exit
--without-login Download data without login
--workspace WORKSPACE
Path to workspace's root directory. This script will create files in {WORKSPACE}/{contest_name}/{alphabet}/ e.g. ./your-workspace/arc001/A/
[Default] /home/kyuridenamida/atcoder-workspace
--lang LANG Programming language of your template code, cpp or java or rust or python or nim or d or cs or julia.
[Default] cpp
--template TEMPLATE File path to your template code
[Default (C++)] /atcodertools/tools/templates/default_template.cpp
[Default (Java)] /atcodertools/tools/templates/default_template.java
[Default (Rust)] /atcodertools/tools/templates/default_template.rs
[Default (Python3)] /atcodertools/tools/templates/default_template.py
[Default (NIM)] /atcodertools/tools/templates/default_template.nim
[Default (D)] /atcodertools/tools/templates/default_template.d
[Default (C#)] /atcodertools/tools/templates/default_template.cs
--parallel Prepare problem directories asynchronously using multi processors.
--save-no-session-cache
Save no session cache to avoid security risk
--skip-existing-problems
Skip processing every problem for which a directory already exists
--config CONFIG File path to your config file
[Default (Primary)] /home/kyuridenamida/.atcodertools.toml
[Default (Secondary)] /atcoder-tools/atcodertools/tools/atcodertools-default.toml
```
### test の詳細
```
usage: atcoder-tools test [-h] [--exec EXEC] [--num NUM]
[--dir DIR] [--timeout TIMEOUT]
[--knock-out]
[--skip-almost-ac-feedback]
[--judge-type JUDGE_TYPE]
[--error-value ERROR_VALUE]
optional arguments:
-h, --help show this help message and exit
--exec EXEC, -e EXEC File path to the execution target. [Default] Automatically detected exec file
--num NUM, -n NUM The case number to test (1-origin). All cases are tested if not specified.
--dir DIR, -d DIR Target directory to test. [Default] Current directory
--timeout TIMEOUT, -t TIMEOUT
Timeout for each test cases (sec) [Default] 1
--knock-out, -k Stop execution immediately after any example's failure [Default] False
--skip-almost-ac-feedback, -s
Hide inputs and expected/actual outputs if result is correct and there are error outputs [Default] False,
--judge-type JUDGE_TYPE, -j JUDGE_TYPE
error type must be one of [normal, absolute, relative, absolute_or_relative]
--error-value ERROR_VALUE, -v ERROR_VALUE
error value for decimal number judge: [Default] 0.000000001
```
### submit の詳細
```
usage: atcoder-tools submit [-h] [--exec EXEC] [--dir DIR]
[--timeout TIMEOUT] [--code CODE]
[--force] [--save-no-session-cache]
[--unlock-safety]
[--judge-type JUDGE_TYPE]
[--error-value ERROR_VALUE]
optional arguments:
-h, --help show this help message and exit
--exec EXEC, -e EXEC File path to the execution target. [Default] Automatically detected exec file
--dir DIR, -d DIR Target directory to test. [Default] Current directory
--timeout TIMEOUT, -t TIMEOUT
Timeout for each test cases (sec) [Default] 1
--code CODE, -c CODE Path to the source code to submit [Default] Code path written in metadata.json
--force, -f Submit the code regardless of the local test result [Default] False
--save-no-session-cache
Save no session cache to avoid security risk
--unlock-safety, -u By default, this script only submits the first code per problem. However, you can remove the safety by this option in order to submit codes twice or more.
--judge-type JUDGE_TYPE, -j JUDGE_TYPE
error type must be one of [normal, absolute, relative, absolute_or_relative]
--error-value ERROR_VALUE, -v ERROR_VALUE
error value for decimal number judge: [Default] 1e-09
```
### codegen の詳細
```
usage: ./atcoder-tools codegen [-h] [--without-login] [--lang LANG]
[--template TEMPLATE] [--save-no-session-cache]
[--config CONFIG]
url
positional arguments:
url URL (e.g. https://atcoder.jp/contests/abc012/tasks/abc012_3)
optional arguments:
-h, --help show this help message and exit
--without-login Download data without login
--lang LANG Programming language of your template code, cpp or java or rust.
[Default] cpp
--template TEMPLATE File path to your template code
[Default (C++)] /home/user/GitHub/atcoder-tools/atcodertools/tools/templates/default_template.cpp
[Default (Java)] /home/user/GitHub/atcoder-tools/atcodertools/tools/templates/default_template.java
[Default (Rust)] /home/user/GitHub/atcoder-tools/atcodertools/tools/templates/default_template.rs
--save-no-session-cache
Save no session cache to avoid security risk
--config CONFIG File path to your config file
[Default (Primary)] /home/user/.atcodertools.toml
[Default (Secondary)] /home/user/GitHub/atcoder-tools/atcodertools/tools/atcodertools-default.toml
```
## 設定ファイルの例
`~/.atcodertools.toml`に以下の設定を保存すると、コードスタイルや、コード生成後に実行するコマンドを指定できます。
設定ファイルはcodestyle, postprocess, tester, submit, etcのテーブルに分かれていて、codestyle.nimというようにテーブル名の後に.[言語名]で指定するとその言語のみに適用されます。
以下は、次の挙動を期待する場合の`~/.atcodertools.toml`の例です。
- `indent_type='space'` スペースがインデントに使われる(`'tab'`を指定した場合はタブが使われる)
- `indent_width=4` インデント幅は4である (`indent_width`が無指定の場合`4`(nim言語以外), `2`(nim言語)が規定値として使われます。)
- `template_file='~/my_template.cpp'` コード生成テンプレートとして`~/my_template.cpp`を使う
- `workspace_dir='~/atcoder-workspace/'` ワークスペースのルートは `~/atcoder-workspace/`
- `lang='cpp'` 言語設定は `cpp` (提出時もしくはデフォルトのコードジェネレーター生成時に使われます)
- `code_generator_file="~/custom_code_generator.py"` カスタムコードジェネレーター `~/custom_code_generator.py`を指定する
- `code_generator_toml="~/universal_code_generator.toml"` ユニバーサルコードジェネレーター `~/universal_code_generator.toml`を指定する
- `exec_on_each_problem_dir='clang-format -i ./*.cpp'` `exec_on_contest_dir='touch CMakeLists.txt'`
- 問題用ディレクトリ内で毎回`clang-format`を実行して、最後に`CMakeLists.txt`(空)をコンテスト用ディレクトリに生成する
- `compile_before_testing` テスト前にコンパイルを実行するか否かをTrue/Falseで指定。何も指定しないとFalseとなります。
- `compile_only_when_diff_detected` テスト前のコンパイルの際、元のソースに変更があった場合のみ実行するかをTrue/Falseで指定。何も指定しないとFalseとなります。
- `timeout_adjustment=1.2` 問題文に記載された実行時間制限にこの値をかけた秒数がローカルテストの際の実行時間制限になります。例えばatcoderで制限時間2秒の問題は2x1.2=2.4秒となります。atcoderとローカルの実行環境が異なる場合の調整に使用してください。
なお、`compile_before_testing`, `compile_only_when_diff_detected`はいずれもtesterの引数で指定することも可能で、指定した場合はそちらが優先されます。
- `download_without_login=false` AtCoderにログインせずにダウンロードを行う機能を使わない (公開コンテストに対してのみ可能)
- `parallel_download=false` データの並列ダウンロードを無効にする
- `save_no_session_cache=false` ログイン情報のクッキーを保存する
- `skip_existing_problems=false` ディレクトリが既に存在する問題の処理をスキップする
- `in_example_format="in_{}.txt"` テストケース(input)のフォーマットを`in_1.txt, in_2.txt, ...`とする
- `out_example_format="out_{}.txt"` テストケース(output)のフォーマットを`out_1.txt, out_2.txt, ...`とする
```toml
[codestyle]
indent_type='space' # 'tab' or 'space'
indent_width=4
template_file='~/my_template.cpp'
workspace_dir='~/atcoder-workspace/'
lang='cpp' # Check README.md for the supported languages.
code_generator_file="~/custom_code_generator.py"
[postprocess]
exec_on_each_problem_dir='clang-format -i ./*.cpp'
exec_on_contest_dir='touch CMakeLists.txt'
[compiler]
compile_command='g++ main.cpp -o main -std=c++17'
compile_only_when_diff_detected=true
[tester]
compile_before_testing=true
compile_only_when_diff_detected=true
timeout_adjustment=1.2
[etc]
download_without_login=false
parallel_download=false
save_no_session_cache=false
skip_existing_problems=false
in_example_format="in_{}.txt"
out_example_format="out_{}.txt"
```
また、以下のように提出時にコマンドを実行してその結果を提出することが可能です。C++以外のAC-libraryを自動に展開するような用途で用いることができます。下記の例はNim言語でACLのexpanderを実行しその出力ファイルを提出し、その後ローカルの出力ファイルを削除するという設定です。
なお、C++に関してはAC-libraryがatcoderのジャッジにも搭載されているためこのような設定は不要です。
- `exec_before_submit` 提出前に実行するコマンド
- `submit_filename` exec_before_submitを実行した結果提出するファイルが変わる場合に指定
- `exec_after_submit` 提出後に行う処理
```toml
[submit.nim]
exec_before_submit='rm ./combined.nim | python3 ~/git/Nim-ACL/expander.py main.nim --lib /home/chaemon/git/Nim-ACL/ -s'
exec_after_submit='rm ./combined.nim'
submit_filename='./combined.nim'
```
### カスタムコードジェネレーター
[標準のC++コードジェネレーター](https://github.com/kyuridenamida/atcoder-tools/blob/master/atcodertools/codegen/code_generators/cpp.py)に倣って、
`(CogeGenArgs) -> str(ソースコード)`が型であるような`main`関数を定義した.pyファイルを`code_generator_file`で指定すると、コード生成時にカスタムコードジェネレーターを利用できます。
### ユニバーサルコードジェネレーター
ユニバーサルコードジェネレーターはループ・配列アクセス方法等のいくつかの言語仕様を記述するだけでカスタムコードジェネレーターよりも簡単にコード生成することを意図して作成したジェネレーターです。設定ファイルの`code_generator_toml`で指定します。書き方は以下です。
- *base_indent* 入力部分のインデント数
- *insert_space_around_operators* 入力部分の変数や演算子の間にスペースを入れるかどうかをtrue/falseで指定
- *newline_after_input* 入力部分で入力ごとに空行を入れるかどうかをtrue/falseで指定
- *global_prefix* グローバル変数の宣言時に入れる接頭辞(Javaなどでstaticを指定したりできます)
以下のようにテーブルを定義します。各項目はダブルコーテーションあるいはシングルコーテーションを用いた文字列で指定します。Pythonのformatメソッドに渡されるため、波括弧等の文字を直に書きたい場合はエスケープする必要があります。
テーブルのキーは整数(int), 浮動小数(float), 文字列(str), およびこれら3つを使った1次元配列(seq), 2次元配列(2d_seq)となっています。
- *[index]* ループインデックスの名称を指定します。1重目を`i`, 2重目を`j`で指定してください。省略可能で省略した場合はi, jが指定されます。Perl, PHPなどの言語で$i, $jなどとi, j以外の名前を指定しなければならないとき用のつもりです。
- *[loop]* ループに関することを記述します
- **header** ループの最初に記述する内容。ループを回すための変数は`{loop_var}`, 回す回数は`{length}`を用いてください。
- **footer** ループの最後に記述する内容。C++, Javaでは閉じカッコになります。波括弧の場合は`}}`とエスケープする必要があることに注意してください。
- *[type]* タイプ(int, float, str)のタイプについて記述します。例を参照してください。
- *[default]* デフォルトの値について記述します。例を参照してください。注意: TOMLの表記に癖があるようで、ダブルコーテーション2つ(空の文字列)を表記する際にはstr='""'とするとよいようです。"\"\""だとエラーになるようです。
- *[input_func]* int, float, strについて入力時に呼び出す関数を記述します。
- *[arg]* solve関数の引数の記述方法について指定します。`int`, `float`, `str`, `seq`, `2d_seq`について記述してください。`{name}`が変数名, `{type}`が`seq`, `2d_seq`についてベースとなる型です。
- *[actual_arg]* `seq`, `2d_seq`についてsolve関数を呼び出す際の引数の渡し方について記述します。C++などでmoveをつかってメモリを節約したいときなどに指定できます。省略可能で、省略した場合はそのまま渡されます。
- *[access]* 配列のアクセス方法について記述します。`seq`, `2d_seq`について指定してください。`{name}`で変数名, `{index_i}`, `{index_j}`でインデックス名を指定します。
以下は宣言・確保・入力を行うためのコードを記述します。いくつかを同時に行う方法も指定できます。いずれも一行または複数行に渡る指定が可能でセミコロン等の終端子も(必要な言語では)記述してください。
キーワードとして`{name}`, `{type}`はそれぞれ対象となる変数名、タイプ名で、上記で指定した`{default}`が使えます。また、指定していれば`{input_func}`も使えます。`seq`, `2d_seq`の場合は`{type}`はベースとなる型名になります(`vector<int>`における`int`)のでご注意ください。また、`seq`の長さは`{length}`, `2d_seq`の長さは`{length_i}`, `{length_j}`となっています。
- *[declare]* int, float, str, seq, 2d_seqの宣言方法について記述します。
- *[allocate]* `seq`, `2d_seq`の確保の方法を記述します。
- *[declare_and_allocate]* `seq`, `2d_seq`について宣言と確保を同時に行う方法について記述します。
- *[input]* int, float, strの入力方法について記述します。
以下は入力コードの冗長性を下げる目的で指定するテーブルで省略可能なものです。指定方法についてはPythonの設定を参照してください。
- *[allocate_and_input]* `seq`, `2d_seq`について確保と入力をまとめて行うことができる場合に記述します。省略した場合、上記で指定した確保と入力の方式を複合したものが挿入されます
- *[declare_and_allocate_and_input]* `seq`, `2d_seq`について宣言・確保・入力をまとめて行うことができる場合に記述します。省略した場合、上記で指定した宣言と確保と入力の方式を複合したものが挿入されます
例えばC++での設定方法は以下です。
```toml
base_indent = 1
insert_space_around_operators = false
# global変数宣言時の接頭辞
global_prefix = ""
# ループ
[loop]
header = "for(int {loop_var} = 0 ; {loop_var} < {length} ; {loop_var}++){{"
footer = "}}"
# タイプ
[type]
int = "long long"
float = "long double"
str = "std::string"
# デフォルト値
[default]
int = "0"
float = "0.0"
str = '""'
# 引数
[arg]
int = "long long {name}"
float = "long double {name}"
str = "std::string {name}"
seq = "std::vector<{type}> {name}"
2d_seq = "std::vector<std::vector<{type}>> {name}"
# 引数への渡し方
[actual_arg]
seq = "std::move({name})"
2d_seq = "std::move({name})"
# 配列アクセス
[access]
seq = "{name}[{index}]"
2d_seq = "{name}[{index_i}][{index_j}]"
# 宣言
[declare]
int = "long long {name};"
float = "long double {name};"
str = "std::string {name};"
seq = "std::vector<{type}> {name};"
2d_seq = "std::vector<std::vector<{type}>> {name};"
# 確保
[allocate]
seq = "{name}.assign({length}, {default});"
2d_seq = "{name}.assign({length_i}, std::vector<{type}>({length_j}));"
# 宣言と確保
[declare_and_allocate]
seq = "std::vector<{type}> {name}({length});"
2d_seq = "std::vector<std::vector<{type}>> {name}({length_i}, std::vector<{type}>({length_j}));"
# 入力
[input]
#int = "std::cin >> {name};"
int = "std::scanf(\"%lld\", &{name});"
#float = "std::cin >> {name};"
float = "std::scanf(\"%Lf\", &{name});"
str = "std::cin >> {name};"
```
例えばPythonでの設定方法は以下です。
```toml
base_indent = 1
insert_space_around_operators = true
# global変数宣言時の接頭辞
global_prefix = ""
# インデックス
[index]
i = "i"
j = "j"
# ループ
[loop]
header = "for {loop_var} in range({length}):"
footer = ""
# タイプ
[type]
int = "int"
float = "float"
str = "str"
# デフォルト値
[default]
int = "int()"
float = "float()"
str = "str()"
# 宣言
[declare]
int = ""
float = ""
str = ""
seq = ""
2d_seq = ""
# 確保
[allocate]
seq = "{name} = [{default}] * ({length})"
2d_seq = "{name} = [[{default}] * ({length_j}) for _ in {length_i}]"
# 宣言と確保
[declare_and_allocate]
seq = "{name} = [{default}] * ({length}) # type: \"List[{type}]\""
self.declare_and_allocate_2d_seq = "{name} = [[{default}] * ({length_j}) for _ in {length_i}] # type: \"List[List[{type}]]\""
# 入力関数
[input_func]
int = "int(next(tokens))"
float = "float(next(tokens))"
str = "next(tokens)"
# 入力
[input]
int = "{name} = {input_func}"
float = "{name} = {input_func}"
str = "{name} = {input_func}"
# 宣言と入力
[declare_and_input]
int = "{name} = {input_func} # type: int"
float = "{name} = {input_func} # type: float"
str = "{name} = {input_func} # type: str"
# 確保と入力
[allocate_and_input]
seq = "{name} = [{input_func} for _ in range({length})]"
2d_seq = "{name} = [[{input_func} for _ in range({length_j})] for _ in range({length_i})]"
# 宣言と確保と入力
[declare_and_allocate_and_input]
seq = "{name} = [{input_func} for _ in range({length})] # type: \"List[{type}]\""
2d_seq = "{name} = [[{input_func} for _ in range({length_j})] for _ in range({length_i})] # type: \"List[List[{type}]]\""
# 引数
[arg]
int = "{name}: int"
float = "{name}: float"
str = "{name}: str"
seq = "{name}: \"List[{type}]\""
2d_seq = "{name}: \"List[List[{type}]]\""
# 配列アクセス
[access]
seq = "{name}[{index}]"
2d_seq = "{name}[{index_i}][{index_j}]"
```
## テンプレートの例
`atcoder-tools gen`コマンドに対し`--template`でテンプレートソースコードを指定できます。
テンプレートエンジンの仕様については[jinja2](http://jinja.pocoo.org/docs/2.10/) の公式ドキュメントを参照してください。
テンプレートに渡される変数は以下の通りです。
- **prediction_success** 入力形式の推論に成功したとき `True`、 失敗したとき `False`が格納されている。この値が`True`のとき次の3種類の変数も存在することが保証される。
- **input_part** input用のコード
- **formal_arguments** 型つき引数列
- **actual_arguments** 型なし引数列
- **mod** 問題文中に存在するmodの整数値
- **yes_str** 問題文中に存在する yes や possible などの真を表しそうな文字列値
- **no_str** 問題文中に存在する no や impossible などの偽を表しそうな文字列値
```c++
#include <bits/stdc++.h>
using namespace std;
{% if mod %}
const long long MOD = {{ mod }};
{% endif %}
{% if yes_str %}
const string YES = "{{ yes_str }}";
{% endif %}
{% if no_str %}
const string NO = "{{ no_str }}";
{% endif %}
{% if prediction_success %}
void solve({{ formal_arguments }}){
}
{% endif %}
int main(){
{% if prediction_success %}
{{input_part}}
solve({{ actual_arguments }});
{% else %}
// Failed to predict input format
{% endif %}
return 0;
}
```
## Contribution
[CONTRIBUTING.md](CONTRIBUTING.md) を参照してください。
## Licence
[MIT](https://github.com/kyuridenamida/ToolsForAtCoder/blob/master/LICENCE)
## Author
[kyuridenamida](https://github.com/kyuridenamida) ([@kyuridenamida](https://twitter.com/kyuridenamida))
%prep
%autosetup -n atcoder-tools-2.12.0
%build
%py3_build
%install
%py3_install
install -d -m755 %{buildroot}/%{_pkgdocdir}
if [ -d doc ]; then cp -arf doc %{buildroot}/%{_pkgdocdir}; fi
if [ -d docs ]; then cp -arf docs %{buildroot}/%{_pkgdocdir}; fi
if [ -d example ]; then cp -arf example %{buildroot}/%{_pkgdocdir}; fi
if [ -d examples ]; then cp -arf examples %{buildroot}/%{_pkgdocdir}; fi
pushd %{buildroot}
if [ -d usr/lib ]; then
find usr/lib -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/lib64 ]; then
find usr/lib64 -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/bin ]; then
find usr/bin -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/sbin ]; then
find usr/sbin -type f -printf "/%h/%f\n" >> filelist.lst
fi
touch doclist.lst
if [ -d usr/share/man ]; then
find usr/share/man -type f -printf "/%h/%f.gz\n" >> doclist.lst
fi
popd
mv %{buildroot}/filelist.lst .
mv %{buildroot}/doclist.lst .
%files -n python3-atcoder-tools -f filelist.lst
%dir %{python3_sitelib}/*
%files help -f doclist.lst
%{_docdir}/*
%changelog
* Thu May 18 2023 Python_Bot <Python_Bot@openeuler.org> - 2.12.0-1
- Package Spec generated
|