-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
1636 lines (1343 loc) · 55.4 KB
/
app.py
File metadata and controls
1636 lines (1343 loc) · 55.4 KB
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
from flask import Flask, render_template, request
from flask_paginate import Pagination, get_page_parameter
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import desc,or_
from collections import defaultdict
from config import *
import numpy
import os
import pickle
import json
import time
import requests
import csv
from urllib.request import urlopen
from bs4 import BeautifulSoup
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
headers = {'Accept-Encoding': 'gzip'}
class Tag(db.Model):
id = db.Column(db.Integer, primary_key=True)
problem_id = db.Column(db.String(64))
tag = db.Column(db.String(64))
tag_second =db.Column(db.String(64))
class problem_tag(db.Model):
id = db.Column(db.Integer, primary_key=True)
problem_official_name = db.Column(db.String(64))
# first_tag:最も表の多いTag
first_tag = db.Column(db.String(64))
second_tag=db.Column(db.String(64))
second_second_tag=db.Column(db.String(64))
second_third_tag=db.Column(db.String(64))
class User_(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(64), index=True, unique=True)
user_image_url = db.Column(db.String(120), index=True, unique=True)
twitter_id = db.Column(db.String(64), nullable=False, unique=True)
vote_count=db.Column(db.Integer)
atcoder_user_id=db.Column(db.String(64))
name_dict={"Brute-Force":"全探索","Binary-Search":"二分探索","Ternary-Search":"三分探索","DFS":"深さ優先探索",
"BFS":"幅優先探索","Bit-Brute-Force":"bit全探索","Heuristic":"ヒューリスティック","Other":"その他","String-Operation":"文字列処理",
"Rolling-Hash":"ローリングハッシュ","Manacher":"Manacher","Suffix-Array":"Suffix-Array","Z-Algorithm":"Z-Algorithm",
"Trie":"Trie","Cumulative-Sum":"累積和","imos":"imos法","Two-Pointers":"尺取り法","Split-And-List":"半分全列挙",
"Square-Division":"平方分割","Divide-And-Conquer":"分割統治法","Doubling":"ダブリング","Shortest-Path":"最短経路",
"Minimum-Spanning-Tree":"最小全域木","LCA":"最小共通祖先","Strongly-Connected-Components":"強連結成分分解","Topological-Sort":"トポロジカルソート",
"Euler-Tour":"オイラーツアー","HL-Decomposition":"HL分解","Centroid-Decomposition":"重心分解","Check-Tree":"木の同型判定",
"Two-Edge-Connected-Components":"二重辺連結成分分解","Bi-Connected-Components":"二重頂点連結成分分解","Cycle-Basis":"サイクル基底",
"dfs-tree":"dfs木","Erdesh":"エルデシュガライの定理","Simple-DP":"基礎DP","String-DP":"文字列DP","Section-DP":"区間DP","Digit-DP":"桁DP",
"Tree-DP":"木DP","Every-Direction-DP":"全方位木DP","Bit-DP":"bitDP","Probability-DP":"確率DP","Expected-Value-DP":"期待値DP",
"Insert-DP":"挿入DP","Link-DP":"連結DP","Inline-DP":"インラインDP","Matrix-Power":"行列累乗","CHT":"Convex-Hull-Trick","Monge-DP":"Monge-DP",
"Alien-DP":"Alien-DP","Kitamasa":"きたまさ法","stack":"stack","queue":"queue","set":"set","map":"map","deque":"deque",
"multiset":"multiset","priority_queue":"priority_queue","Union-Find-Tree":"Union-Find-Tree","BIT":"Binary-Indexed-Tree","Segment-Tree":"Segment-Tree",
"Lazy-Segment-Tree":"Lazy-Segment-Tree","Sparse-Table":"Sparse-Table","WaveletMatrix":"WaveletMatrix","Persistent-Data-Structures":"永続データ構造",
"Balanced-Tree":"平衡二分探索木","Nim":"Nim","Grundy":"Grundy数","Backtrack":"後退解析","Mini-Max":"ミニマックス法","unique":"特殊な性質",
"Max-Flow":"最大流問題","Min-Cost-Flow":"最小費用流問題","Bipartite-Matching":"二部マッチング","Min-Cut":"最小カット","Burn":"燃やす埋める",
"Convex-Hull":"凸包","Declination-Sorting":"偏角ソート","Three-D":"三次元","Number":"整数","Combinatorics":"組み合わせ","Probability":"確率","Expected-Value":"期待値",
"Matrix":"行列","Parsing":"構文解析","Easy":"Easy","Ad-Hoc":"Ad-Hoc","Greedy-Methods":"Greedy-Methods","Construct":"Construct",None:"None",'null':'None',"Enumerate":"数え上げ",
"Compress":"座標圧縮","Sort":"ソート","XOR":"XOR","Euler-Path-and-Hamilton-Path":"オイラーパス・ハミルトンパス","Randomized-Algorithm":"乱択アルゴリズム",
"Voronoi-Diagram":"ボロノイ図","Recursion":"再帰関数","Kirchhoff":"行列木定理","Restore-DP":"戻すDP","Marathon":"Marathon","Interactive":"インタラクティブ","Simulation":"シミュレーション",
"April-Fool":"April-Fool"}
@app.route("/")
def index():
category_list = [
"Easy",
"Ad-Hoc",
"Searching",
"Greedy-Methods",
"String",
"Mathematics",
"Technique",
"Construct",
"Graph",
"Dynamic-Programming",
"Data-Structure",
"Game",
"Flow-Algorithms",
"Geometry",
"Interactive",
"April-Fool",
"Marathon",
"Other",
]
# 問題の総数を求める。
get_problem = requests.get(
"https://kenkoooo.com/atcoder/resources/merged-problems.json",headers=headers
)
get_problem = get_problem.json()
ALL_PROBLEM_NUM = len(get_problem)
# 正式な問題名かをチェックするための辞書
add = defaultdict(int)
for problem in get_problem:
add[problem["id"]] += 1
# 投票済みの問題の総数を求める。
list = db.session.query(problem_tag).all()
all_tag=db.session.query(Tag).all()
ALL_VOTED_NUM=len(all_tag)
VOTED_PROBLEM_NUM = 0
for tag in list:
# 正しいカテゴリーかチェック
for category in category_list:
if tag.first_tag == category:
# 問題名が正式かつタグのカテゴリーも正しいものならば総数に1加算される
VOTED_PROBLEM_NUM += add[tag.problem_official_name]
break
# 投票済みパーセンテージ
PERCENTAGE = round((VOTED_PROBLEM_NUM / ALL_PROBLEM_NUM) * 100, 3)
return render_template("index.html", percentage=PERCENTAGE,voted_num=ALL_VOTED_NUM)
@app.route("/explain")
def explain():
return render_template("tag_explain.html")
@app.route("/tag_search/<tag_name>")
def tag_search(tag_name):
# コンテスト名取得のため、AtCoderProblemsAPIを利用する。
get_problem = requests.get(
"https://kenkoooo.com/atcoder/resources/merged-problems.json",headers=headers
)
get_problem = get_problem.json()
get_difficulty=requests.get("https://kenkoooo.com/atcoder/resources/problem-models.json",headers=headers)
get_difficulty=get_difficulty.json()
tagName = tag_name
if tagName!="Other":
problems = db.session.query(problem_tag).filter(or_(problem_tag.first_tag==tagName,problem_tag.second_tag==tagName,problem_tag.second_second_tag==tagName,problem_tag.second_third_tag==tagName))
else:
problems=db.session.query(problem_tag).filter_by(first_tag="Other").all()
dict = {}
difficulty_dict={}
# 最新のコンテストの場合、API反映までに時間がかかるため、バグらせないように以下の処理をする必要がある。
for problem in problems:
dict[str(problem.problem_official_name)] = {
"contest_id": problem.problem_official_name,
"title": "Error",
"solver_count": -1
}
difficulty_dict[str(problem.problem_official_name)]=99999
for problem_name in get_difficulty:
if get_difficulty[problem_name] is not None and 'difficulty' in get_difficulty[problem_name]:
difficulty_value = get_difficulty[problem_name]['difficulty']
if difficulty_value is not None:
difficulty_dict[problem_name] = difficulty_value
else:
difficulty_dict[problem_name] = 99999
else:
difficulty_dict[problem_name]=99999
# official_nameからコンテスト名を得るために辞書を作成する。
for problem in get_problem:
dict[str(problem["id"])] = problem
if dict[str(problem["id"])]["solver_count"] == None:
dict[str(problem["id"])]["solver_count"] = -1
# 問題を解かれた人数で並び替える。predictで並び替えるとnullがあるので死ぬ。
problems = sorted(
problems,
key=lambda x: (
difficulty_dict[str(x.problem_official_name)],
-dict[str(x.problem_official_name)]["solver_count"]
),
)
return render_template(
"tag_search.html", tagName=tagName, problems=problems, dict=dict,difficulty_dict=difficulty_dict
)
@app.route("/tag_search/<tag_name>/<user_id>")
def user_tag_search(tag_name, user_id):
# コンテスト名およびuser情報取得のため、AtCoderProblemsAPIを利用する。
get_problem = requests.get(
"https://kenkoooo.com/atcoder/resources/merged-problems.json",headers=headers
)
get_user_info = requests.get(
str("https://kenkoooo.com/atcoder/atcoder-api/results?user=" + user_id),headers=headers
)
if get_user_info.status_code!=200:
return render_template('error.html',message='ユーザーが存在しません')
get_difficulty=requests.get("https://kenkoooo.com/atcoder/resources/problem-models.json",headers=headers)
get_problem = get_problem.json()
get_user_info = get_user_info.json()
get_difficulty=get_difficulty.json()
tagName = tag_name
if tagName!="Other":
problems = db.session.query(problem_tag).filter(or_(problem_tag.first_tag==tagName,problem_tag.second_tag==tagName,problem_tag.second_second_tag==tagName,problem_tag.second_third_tag==tagName))
else:
problems=db.session.query(problem_tag).filter_by(first_tag="Other").all()
dict = {}
difficulty_dict={}
# 最新のコンテストの場合、API反映までに時間がかかるため、バグらせないように以下の処理をする必要がある。
for problem in problems:
dict[str(problem.problem_official_name)] = {
"contest_id": problem.problem_official_name,
"title": "Error",
"solver_count": -1
}
difficulty_dict[str(problem.problem_official_name)]=99999
for problem_name in get_difficulty:
if get_difficulty[problem_name] is not None and 'difficulty' in get_difficulty[problem_name]:
difficulty_value = get_difficulty[problem_name]['difficulty']
if difficulty_value is not None:
difficulty_dict[problem_name] = difficulty_value
else:
difficulty_dict[problem_name] = 99999
else:
difficulty_dict[problem_name]=99999
# official_nameからコンテスト名を得るために辞書を作成する。
for problem in get_problem:
dict[str(problem["id"])] = problem
if dict[str(problem["id"])]["solver_count"] == None:
dict[str(problem["id"])]["solver_count"] = -1
# 問題を解かれた人数で並び替える。predictで並び替えるとnullがあるので死ぬ。
problems = sorted(
problems,
key=lambda x: (
difficulty_dict[str(x.problem_official_name)],
-dict[str(x.problem_official_name)]["solver_count"]
),
)
for problem in problems:
print(difficulty_dict[str(problem.problem_official_name)])
############################################################################################################
# 以下user情報取得
user_dict = {}
# はじめに全ての問題をWAとする。
for problem in problems:
user_dict[str(problem.problem_official_name)] = "WA"
# その後、ACの問題が見つかり次第、書き換える。
for info in get_user_info:
if info["result"] == "AC":
user_dict[str(info["problem_id"])] = "AC"
return render_template(
"user_tag_search.html",
tagName=tagName,
problems=problems,
dict=dict,
user_id=user_id,
user_dict=user_dict,
difficulty_dict=difficulty_dict,
)
@app.route("/vote")
def vote():
return render_template("vote.html")
@app.route("/vote_result")
def vote_result():
#####################################################################################
#タグを投票する処理
problem_id = request.args.get("problem_id")
tag = request.args.get("tag")
tag2= request.args.get("tag2")
# 白紙投票がある場合
if problem_id == "" or tag == None:
return render_template("error.html",message='空欄が存在します')
##############################################################################################
try:
#現在開催中のコンテストの場合エラーを出す
# URLの指定
check_set=set()
html = urlopen("https://atcoder.jp/home")
bsObj = BeautifulSoup(html, "html.parser")
# テーブルを指定
recent_table = bsObj.find(id="contest-table-active")
if recent_table !=None:
table=recent_table.findAll('td')
for i in range(0,len(table)):
#time and date は飛ばす
if i%2==0:
continue
add_url=table[i].find("a").attrs["href"]
#problem_idを抜き出す
html2 = urlopen(str("https://atcoder.jp"+add_url+"/tasks"))
bsObj2 = BeautifulSoup(html2, "html.parser")
table2=bsObj2.findAll('tr')
for row in table2:
if len(row.findAll("a"))>0:
check_set.add(row.findAll("a")[0].attrs["href"].split('/')[-1])
print(row.findAll("a")[0].attrs["href"].split('/')[-1])
if problem_id in check_set:
return render_template('error.html',message='コンテスト終了までお待ち下さい。終了している場合は、もうしばらくお待ち下さい。')
except Exception as e:
return render_template('error.html',message=e)
##############################################################################################
#もし下位分類が存在しないカテゴリーだった場合、下位分類は上位分類と同じにする。
if tag in ["Easy","Ad-Hoc","Greedy-Methods","Construct","Marathon","Interactive","Other","April-Fool"]:
tag2=tag
if not current_user.is_anonymous:
user=db.session.query(User_).filter_by(id=current_user.id).first()
user.vote_count+=1
db.session.commit()
newTag = Tag(problem_id=problem_id, tag=tag,tag_second=tag2)
db.session.add(newTag)
db.session.commit()
search_tag = (
db.session.query(problem_tag)
.filter_by(problem_official_name=problem_id)
.first()
)
# Tagが存在しない場合、投票されたTagがその問題のジャンルになる。
if search_tag == None:
tag_params = {"problem_official_name": problem_id, "first_tag": tag,"second_tag":tag2}
newProblemTag = problem_tag(**tag_params)
db.session.add(newProblemTag)
db.session.commit()
# Tagが存在する場合、その問題に投票された全てのTagを集計し直し、ジャンルを決定する。
else:
####################################################################################################
#first_tag
tags = db.session.query(Tag).filter(Tag.problem_id == problem_id)
vote_num = defaultdict(int)
for t in tags:
vote_num[t.tag] += 1
vote_num = sorted(vote_num.items(), key=lambda x: x[1], reverse=True)
tag_ = None
if len(vote_num) != 0:
tag_ = vote_num[0][0]
if tag_ != None:
search_tag.first_tag = tag_
db.session.commit()
###########################################################################################################
#second_tag
vote_num2 = defaultdict(int)
second_tags=db.session.query(Tag).filter(Tag.problem_id==problem_id)
for t in second_tags:
vote_num2[t.tag_second] += 1
vote_num2 = sorted(vote_num2.items(), key=lambda x: x[1], reverse=True)
#下位分類の上位3位まで
tag_ = None
tag2_= None
tag3_= None
if len(vote_num2) != 0:
for i in range(0,len(vote_num2)) :
if vote_num2[i][0]!=None and vote_num2[i][0] != 'null':
tag_=vote_num2[i][0]
break
for i in range(0,len(vote_num2)):
if vote_num2[i][0]!=None and vote_num2[i][0] != 'null' and vote_num2[i][0]!=tag_:
tag2_=vote_num2[i][0]
break
for i in range(0,len(vote_num2)):
if vote_num2[i][0]!=None and vote_num2[i][0] != 'null' and vote_num2[i][0]!=tag_ and vote_num2[i][0]!=tag2_:
tag3_=vote_num2[i][0]
break
if tag_ != None:
search_tag.second_tag = tag_
db.session.commit()
if tag2_ != None:
search_tag.second_second_tag = tag2_
db.session.commit()
if tag3_ != None:
search_tag.second_third_tag = tag3_
db.session.commit()
#####################################################################################
#グラフを表示する処理
tag_name = tag
# 各ジャンルタグ数
sum_dict = {
"Easy":0,
"Ad-Hoc":0,
"Searching": 0,
"Greedy-Methods": 0,
"String": 0,
"Mathematics": 0,
"Technique": 0,
"Construct": 0,
"Graph": 0,
"Dynamic-Programming": 0,
"Data-Structure": 0,
"Game": 0,
"Flow-Algorithms": 0,
"Geometry": 0,
"Interactive":0,
"April-Fool":0,
"Marathon":0,
"Other":0,
}
second_sum_dict=defaultdict(int)
name_list=set()
tags = db.session.query(Tag).filter_by(problem_id=problem_id).all()
for i in tags:
if i.tag!=None:
sum_dict[i.tag] += 1
if i.tag_second !=None and i.tag_second !='null':
second_sum_dict[name_dict[i.tag_second]]+=1
name_list.add(name_dict[i.tag_second])
name_list=list(name_list)
return render_template("success.html", tag_name=tag_name, dict=sum_dict,list=name_list,second_dict=second_sum_dict)
@app.route("/check")
def check():
return render_template("check_problem.html")
@app.route("/check/<problem_id>")
def check_problem(problem_id):
tag = (
db.session.query(problem_tag)
.filter_by(problem_official_name=problem_id)
.first()
)
if tag == None:
return render_template("check_error.html")
else:
tag_name = tag.first_tag
second_tag=None
if tag.second_tag!=None and tag.second_tag!='null':
second_tag = name_dict[tag.second_tag]
tag0=tag_name
tag1=name_dict[tag.second_tag]
tag2=name_dict[tag.second_second_tag]
tag3=name_dict[tag.second_third_tag]
# 各ジャンルタグ数
sum_dict = {
"Easy":0,
"Ad-Hoc":0,
"Searching": 0,
"Greedy-Methods": 0,
"String": 0,
"Mathematics": 0,
"Technique": 0,
"Construct": 0,
"Graph": 0,
"Dynamic-Programming": 0,
"Data-Structure": 0,
"Game": 0,
"Flow-Algorithms": 0,
"Geometry": 0,
"Interactive":0,
"April-Fool":0,
"Marathon":0,
"Other":0,
}
second_sum_dict=defaultdict(int)
name_list=set()
tags = db.session.query(Tag).filter_by(problem_id=problem_id).all()
for i in tags:
if i.tag!=None:
sum_dict[i.tag] += 1
if i.tag_second !=None and i.tag_second !='null':
second_sum_dict[name_dict[i.tag_second]]+=1
name_list.add(name_dict[i.tag_second])
name_list=list(name_list)
return render_template(
"check_problem_result.html", tag_name=tag_name, dict=sum_dict,second_tag=second_tag,list=name_list,second_dict=second_sum_dict,tag0=tag0,tag1=tag1,tag2=tag2,tag3=tag3
)
@app.route("/graph")
def graph():
# ジャンル
category_list = [
"Easy",
"Ad-Hoc",
"Searching",
"Greedy-Methods",
"String",
"Mathematics",
"Technique",
"Construct",
"Graph",
"Dynamic-Programming",
"Data-Structure",
"Game",
"Flow-Algorithms",
"Geometry",
"Interactive",
]
# 各ジャンルの問題総数
sum_dict = {
"Easy":0,
"Ad-Hoc":0,
"Searching": 0,
"Greedy-Methods": 0,
"String": 0,
"Mathematics": 0,
"Technique": 0,
"Construct": 0,
"Graph": 0,
"Dynamic-Programming": 0,
"Data-Structure": 0,
"Game": 0,
"Flow-Algorithms": 0,
"Geometry": 0,
"Interactive":0,
}
for category in category_list:
problem_list = db.session.query(problem_tag).filter_by(first_tag=category).all()
sum_dict[category] = len(problem_list)
return render_template("graph.html", sum_dict=sum_dict)
@app.route("/graph/<user_id>")
def user_graph(user_id):
# AtCoderAPIからUser情報を取得する
get_user_info = requests.get(
str("https://kenkoooo.com/atcoder/atcoder-api/results?user=" + user_id),headers=headers
)
if get_user_info.status_code!=200:
return render_template('error.html',message='ユーザーが存在しません')
get_user_info = get_user_info.json()
# ジャンルリスト
category_list = [
"Easy",
"Ad-Hoc",
"Searching",
"Greedy-Methods",
"String",
"Mathematics",
"Technique",
"Construct",
"Graph",
"Dynamic-Programming",
"Data-Structure",
"Game",
"Flow-Algorithms",
"Geometry",
"Interactive",
]
# ジャンル別の問題総数
sum_dict = {
"Easy":0,
"Ad-Hoc":0,
"Searching": 0,
"Greedy-Methods": 0,
"String": 0,
"Mathematics": 0,
"Technique": 0,
"Construct": 0,
"Graph": 0,
"Dynamic-Programming": 0,
"Data-Structure": 0,
"Game": 0,
"Flow-Algorithms": 0,
"Geometry": 0,
"Interactive":0,
}
# ユーザーが各ジャンルの問題を何問解いたか
user_sum_dict = {
"Easy":0,
"Ad-Hoc":0,
"Searching": 0,
"Greedy-Methods": 0,
"String": 0,
"Mathematics": 0,
"Technique": 0,
"Construct": 0,
"Graph": 0,
"Dynamic-Programming": 0,
"Data-Structure": 0,
"Game": 0,
"Flow-Algorithms": 0,
"Geometry": 0,
"Interactive":0,
}
# ジャンル毎にUserが何%ACしているか
percent_dict = {
"Easy":0,
"Ad-Hoc":0,
"Searching": 0,
"Greedy-Methods": 0,
"String": 0,
"Mathematics": 0,
"Technique": 0,
"Construct": 0,
"Graph": 0,
"Dynamic-Programming": 0,
"Data-Structure": 0,
"Game": 0,
"Flow-Algorithms": 0,
"Geometry": 0,
"Interactive":0,
}
###########################################################################################
# ACリスト作成
# userがその問題をACしているかどうかのリスト
user_dict = {}
# タグ付けされている全ての問題
all_problems = db.session.query(problem_tag).all()
# 一旦、全てをWAとする。
for problem in all_problems:
user_dict[str(problem.problem_official_name)] = "WA"
# その後、ACの問題が見つかり次第、書き換える。
for info in get_user_info:
if info["result"] == "AC":
user_dict[str(info["problem_id"])] = "AC"
############################################################################################
for category in category_list:
problem_list = db.session.query(problem_tag).filter_by(first_tag=category).all()
sum_dict[category] = len(problem_list)
for problem in problem_list:
if user_dict[problem.problem_official_name] == "AC":
user_sum_dict[category] = user_sum_dict[category] + 1
if sum_dict[category] == 0:
percent_dict[category] = 0
else:
percent_dict[category] = int(
(user_sum_dict[category] / sum_dict[category]) * 100
)
return render_template(
"user_graph.html", dict=percent_dict, user_id=user_id, sum_dict=sum_dict
)
@app.route("/graph/<user_id>/<rival_id>")
def user_and_rival_graph(user_id, rival_id):
# AtCoderAPIからUser情報を取得する
get_user_info = requests.get(
str("https://kenkoooo.com/atcoder/atcoder-api/results?user=" + user_id),headers=headers
)
if get_user_info.status_code!=200:
return render_template('error.html','ユーザーが存在しません')
get_user_info = get_user_info.json()
get_rival_info = requests.get(
str("https://kenkoooo.com/atcoder/atcoder-api/results?user=" + rival_id),headers=headers
)
if get_rival_info.status_code!=200:
return render_template('error.html','ライバルが存在しません')
get_rival_info = get_rival_info.json()
# ジャンルリスト
category_list = [
"Easy",
"Ad-Hoc",
"Searching",
"Greedy-Methods",
"String",
"Mathematics",
"Technique",
"Construct",
"Graph",
"Dynamic-Programming",
"Data-Structure",
"Game",
"Flow-Algorithms",
"Geometry",
"Interactive",
]
# ジャンル別の問題総数
sum_dict = {
"Easy":0,
"Ad-Hoc":0,
"Searching": 0,
"Greedy-Methods": 0,
"String": 0,
"Mathematics": 0,
"Technique": 0,
"Construct": 0,
"Graph": 0,
"Dynamic-Programming": 0,
"Data-Structure": 0,
"Game": 0,
"Flow-Algorithms": 0,
"Geometry": 0,
"Interactive":0,
}
# ユーザーが各ジャンルの問題を何問解いたか
user_sum_dict = {
"Easy":0,
"Ad-Hoc":0,
"Searching": 0,
"Greedy-Methods": 0,
"String": 0,
"Mathematics": 0,
"Technique": 0,
"Construct": 0,
"Graph": 0,
"Dynamic-Programming": 0,
"Data-Structure": 0,
"Game": 0,
"Flow-Algorithms": 0,
"Geometry": 0,
"Interactive":0,
}
rival_sum_dict = {
"Easy":0,
"Ad-Hoc":0,
"Searching": 0,
"Greedy-Methods": 0,
"String": 0,
"Mathematics": 0,
"Technique": 0,
"Construct": 0,
"Graph": 0,
"Dynamic-Programming": 0,
"Data-Structure": 0,
"Game": 0,
"Flow-Algorithms": 0,
"Geometry": 0,
"Interactive":0,
}
# ジャンル毎にUserが何%ACしているか
percent_dict = {
"Easy":0,
"Ad-Hoc":0,
"Searching": 0,
"Greedy-Methods": 0,
"String": 0,
"Mathematics": 0,
"Technique": 0,
"Construct": 0,
"Graph": 0,
"Dynamic-Programming": 0,
"Data-Structure": 0,
"Game": 0,
"Flow-Algorithms": 0,
"Geometry": 0,
"Interactive":0,
}
rival_percent_dict = {
"Easy":0,
"Ad-Hoc":0,
"Searching": 0,
"Greedy-Methods": 0,
"String": 0,
"Mathematics": 0,
"Technique": 0,
"Construct": 0,
"Graph": 0,
"Dynamic-Programming": 0,
"Data-Structure": 0,
"Game": 0,
"Flow-Algorithms": 0,
"Geometry": 0,
"Interactive":0,
}
###########################################################################################
# ACリスト作成
# userがその問題をACしているかどうかのリスト
user_dict = {}
rival_dict = {}
# タグ付けされている全ての問題
all_problems = db.session.query(problem_tag).all()
# 一旦、全てをWAとする。
for problem in all_problems:
user_dict[str(problem.problem_official_name)] = "WA"
rival_dict[str(problem.problem_official_name)] = "WA"
# その後、ACの問題が見つかり次第、書き換える。
for info in get_user_info:
if info["result"] == "AC":
user_dict[str(info["problem_id"])] = "AC"
for info in get_rival_info:
if info["result"] == "AC":
rival_dict[str(info["problem_id"])] = "AC"
############################################################################################
for category in category_list:
problem_list = db.session.query(problem_tag).filter_by(first_tag=category).all()
sum_dict[category] = len(problem_list)
for problem in problem_list:
if user_dict[problem.problem_official_name] == "AC":
user_sum_dict[category] = user_sum_dict[category] + 1
if rival_dict[problem.problem_official_name] == "AC":
rival_sum_dict[category] = rival_sum_dict[category] + 1
if sum_dict[category] == 0:
percent_dict[category] = 0
rival_percent_dict[category] = 0
else:
percent_dict[category] = int(
(user_sum_dict[category] / sum_dict[category]) * 100
)
rival_percent_dict[category] = int(
(rival_sum_dict[category] / sum_dict[category]) * 100
)
return render_template(
"user_and_rival_graph.html",
user_dict=percent_dict,
rival_dict=rival_percent_dict,
user_id=user_id,
rival_id=rival_id,
sum_dict=sum_dict,
)
@app.route("/collect")
def collect():
return render_template("collect.html")
@app.route("/collect/<user_id>")
def user_collect(user_id):
# コンテスト名およびuser情報取得のため、AtCoderProblemsAPIを利用する。
get_problem = requests.get(
"https://kenkoooo.com/atcoder/resources/merged-problems.json",headers=headers
)
get_user_info = requests.get(
str("https://kenkoooo.com/atcoder/atcoder-api/results?user=" + user_id),headers=headers
)
get_difficulty=requests.get("https://kenkoooo.com/atcoder/resources/problem-models.json",headers=headers)
if get_user_info.status_code!=200:
return render_template('error.html',message='ユーザーが存在しません')
get_problem = get_problem.json()
get_user_info = get_user_info.json()
get_difficulty=get_difficulty.json()
category_list = [
"Ad-Hoc",
"Searching",
"Greedy-Methods",
"String",
"Mathematics",
"Technique",
"Construct",
"Graph",
"Dynamic-Programming",
"Data-Structure",
"Game",
"Flow-Algorithms",
"Geometry",
"Interactive",
]
# 各カテゴリーの出題確率
probability = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0,0,0]
problem_sum = 0
SIZE = len(category_list)
for i in range(0, SIZE):
num = db.session.query(problem_tag).filter_by(first_tag=category_list[i]).all()
probability[i] = len(num)
problem_sum += len(num)
probability_sum = 0
for i in range(0, SIZE - 1):
probability[i] = probability[i] / problem_sum
probability_sum += probability[i]
probability[SIZE - 1] = 1 - probability_sum
return_list = []
problem_set = set()
difficulty_dict={}
timer = 0
for problem in get_problem:
difficulty_dict[problem["id"]]=99999
for problem_id in get_difficulty:
difficulty_dict[problem_id]=get_difficulty[problem_id].get("difficulty",99999)
while True:
if len(return_list) > 3:
break
timer += 1
if timer > 100:
break
# ランダムにカテゴリーを選ぶ
tagName = numpy.random.choice(category_list, p=probability)
problems = db.session.query(problem_tag).filter_by(first_tag=tagName)
dict = {}
# 最新のコンテストの場合、API反映までに時間がかかるため、バグらせないように以下の処理をする必要がある。
for problem in problems:
dict[str(problem.problem_official_name)] = {
"contest_id": problem.problem_official_name,
"title": "Error",
"solver_count": -1,
"predict": -1,
}
# official_nameからコンテスト名を得るために辞書を作成する。
for problem in get_problem:
dict[str(problem["id"])] = problem
if dict[str(problem["id"])]["predict"] == None:
dict[str(problem["id"])]["predict"] = -1
if dict[str(problem["id"])]["solver_count"] == None:
dict[str(problem["id"])]["solver_count"] = -1