11import os
2+
23import requests
34
45pbars = []
5-
6+ # ['0x11', '그리디', 'https://www.acmicpc.net/workbook/view/7320']
67def parse_links ():
7- attrs = []
8- with open ('links.txt' , encoding = "UTF-8" ) as f :
9- for line in f :
10- attrs .append (line .strip ().split (',' ))
11- return attrs
8+ attrs = []
9+ with open ('links.txt' , encoding = "UTF-8" ) as f :
10+ for line in f :
11+ attrs .append (line .strip ().split (',' ))
12+ return attrs
1213
1314def parse_category ():
14- category = []
15- with open ('problems.txt' , encoding = "UTF-8" ) as f :
16- for line in f :
17- category .append (line .strip ().split (',' ))
18- return category
15+ category = []
16+ with open ('problems.txt' , encoding = "UTF-8" ) as f :
17+ for line in f :
18+ category .append (line .strip ().split (',' ))
19+ return category
1920
2021def get_problem_info (workbook_url ):
21- headers = {
22- 'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36'
23- }
24- txt = requests .get (workbook_url , headers = headers ).text
25- pattern = '/problem/'
26- ret = []
27- while True :
28- x = txt .find (pattern )
29- if x == - 1 :
30- break
31- txt = txt [x + 9 :]
32- prob_id , prob_name = '' , ''
33- i = 0
34- while txt [i ] in '0123456789' :
35- prob_id += txt [i ]
36- i += 1
37- if not prob_id :
38- continue
39- i += 2
40- while txt [i ] != '<' :
41- prob_name += txt [i ]
42- i += 1
43- ret .append ((prob_id , prob_name ))
44- return ret
22+ headers = {'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36' }
23+ txt = requests .get (workbook_url , headers = headers ).text
24+ pattern = '/problem/'
25+ ret = []
26+ while True :
27+ x = txt .find (pattern )
28+ if x == - 1 : break
29+ txt = txt [x + 9 :]
30+ prob_id , prob_name = '' , ''
31+ i = 0
32+ while txt [i ] in '0123456789' :
33+ prob_id += txt [i ]
34+ i += 1
35+ if not prob_id : continue
36+ i += 2
37+ while txt [i ] != '<' :
38+ prob_name += txt [i ]
39+ i += 1
40+ ret .append ((prob_id , prob_name ))
41+ return ret
4542
4643CATEGORY = ["연습 문제" , "기본 문제✔" , "기본 문제" , "응용 문제✔" , "응용 문제" ]
4744
45+ # gen 0x00.md to 0x??.md, proper prob_id.cpp for each solution directory
4846def gen_ind_workbook (attrs , category ):
49- java_template = '''// Authored by : BaaaaaaaaaaarkingDog
47+ txt = '''// Authored by : BaaaaaaaaaaarkingDog
5048// Co-authored by : -
5149// http://boj.kr/****************
5250'''
53-
54- chapter_idx = 0
55- for attr in attrs :
56- if len (attr ) < 3 :
57- pbars .append ("" )
58- continue
59- solution_num = 0
60- solution_path = f'../{ attr [0 ]} /solutions/'
61- os .makedirs (solution_path , exist_ok = True )
62-
63- category_idx = 0
64- problem_infos = get_problem_info (attr [2 ])
65- print (f'[{ attr [0 ]} ] { attr [1 ]} : { len (problem_infos )} problems' )
66- print (problem_infos )
67- prob_table = '| 문제 분류 | 문제 | 문제 제목 | 정답 코드 |\n | :--: | :--: | :--: | :--: |\n '
68-
69- for prob_id , prob_name in problem_infos :
70- if prob_id in category [chapter_idx ]:
71- category_idx = category [chapter_idx ].index (prob_id )
72- file_path = os .path .join (solution_path , prob_id )
73- java_file = file_path + '.java'
74-
75- if not os .path .exists (java_file ):
76- with open (java_file , 'w' , encoding = "UTF-8" ) as f :
77- f .write (java_template )
78-
79- try :
80- codes = open (java_file , 'r' , encoding = "UTF-8" ).read ()
81- except :
82- codes = open (java_file , 'r' , encoding = "EUC-KR" ).read ()
83- with open (java_file , 'w' , encoding = "UTF-8" ) as fw :
84- fw .write (codes )
85-
86- if codes [:100 ] == java_template [:100 ]:
87- prob_table += f'| { CATEGORY [category_idx ]} | { prob_id } | [{ prob_name } ](https://www.acmicpc.net/problem/{ prob_id } ) | - |\n '
88- else :
89- solution_num += 1
90- code_attr = f'[정답 코드]({ java_file .replace (" " , "%20" )} )'
91- for i in range (1 , 10 ):
92- alt_path = f'{ file_path } _{ i } .java'
93- if os .path .exists (alt_path ):
94- code_attr += f", [별해 { i } ]({ alt_path .replace (' ' , '%20' )} )"
95- prob_table += f'| { CATEGORY [category_idx ]} | { prob_id } | [{ prob_name } ](https://www.acmicpc.net/problem/{ prob_id } ) | { code_attr } |\n '
96-
97- with open (attr [0 ] + '.md' , 'w' , encoding = "UTF-8" ) as f :
98- f .write (f'# { attr [1 ]} \n \n ' )
99- pbar = f'} &title=progress&width=500&color=babaca&suffix=/{ len (problem_infos )} )'
100- pbars .append (pbar )
101- f .write (pbar + '\n \n ' )
102- f .write (f'[문제집 링크]({ attr [2 ]} )\n \n ' )
103- f .write (prob_table )
104-
105- chapter_idx += 1
51+ chapter_idx = 0
52+ for attr in attrs :
53+ if len (attr ) < 3 : # No workbook
54+ pbars .append ("" )
55+ continue
56+ solution_num = 0
57+ solution_path = f'../{ attr [0 ]} /solutions/'
58+ category_idx = 0
59+ problem_infos = get_problem_info (attr [2 ])
60+ prob_table = '| 문제 분류 | 문제 | 문제 제목 | 정답 코드 |\n | :--: | :--: | :--: | :--: |\n '
61+ for prob_id , prob_name in problem_infos :
62+ if prob_id in category [chapter_idx ]:
63+ category_idx = category [chapter_idx ].index (prob_id )
64+ file_path = solution_path + prob_id
65+ if not os .path .exists (file_path + '.java' ):
66+ with open (file_path + '.java' , 'w' , encoding = "UTF-8" ) as f :
67+ f .write (txt )
68+ try :
69+ codes = open (file_path + '.java' , 'r' , encoding = "UTF-8" ).read ()
70+ except : # EUC-KR -> UTF-8
71+ codes = open (file_path + '.java' , 'r' , encoding = "EUC-KR" ).read ()
72+ with open (file_path + '.java' , 'w' , encoding = "UTF-8" ) as fw :
73+ fw .write (codes )
74+ if codes [:100 ] == txt [:100 ]:
75+ prob_table += f'| { CATEGORY [category_idx ]} | { prob_id } | [{ prob_name } ](https://www.acmicpc.net/problem/{ prob_id } ) | - |\n '
76+ else :
77+ solution_num += 1
78+ code_attr = f'[정답 코드]({ file_path .replace (" " , "%20" )} .java)'
79+ MAX_DIFFERENT_SOLUTION = 9
80+ for i in range (1 , MAX_DIFFERENT_SOLUTION + 1 ):
81+ if os .path .exists (file_path + '_' + str (i )+ '.java' ):
82+ code_attr += f", [별해 { i } ]({ file_path + '_' + str (i )+ '.java' } )"
83+ prob_table += f'| { CATEGORY [category_idx ]} | { prob_id } | [{ prob_name } ](https://www.acmicpc.net/problem/{ prob_id } ) | { code_attr } |\n '
84+ with open (attr [0 ]+ '.md' , 'w' , encoding = "UTF-8" ) as f :
85+ # progress bar
86+ f .write (f'# { attr [1 ]} \n \n ' )
87+ pbar = f'} &title=progress&width=500&color=babaca&suffix=/{ len (problem_infos )} )'
88+ pbars .append (pbar )
89+ f .write (pbar + '\n \n ' )
90+ f .write (f'[문제집 링크]({ attr [2 ]} )\n \n ' )
91+ f .write (prob_table )
92+ chapter_idx += 1
10693
10794# ['0x11', '그리디', 'https://www.acmicpc.net/workbook/view/7320']
10895def gen_total_workbook (attrs ):
@@ -135,13 +122,12 @@ def gen_total_workbook(attrs):
135122| 번호 | 주제 | 진행도 |
136123| :--: | :--: | :--: |\n ''' )
137124 for attr , pbar in zip (attrs , pbars ):
138- if len (attr ) < 3 :
139- f .write (f'| { attr [0 ]} | { attr [1 ]} | |\n ' )
140- else :
141- f .write (f'| { attr [0 ]} | [{ attr [1 ]} ](workbook/{ attr [0 ].replace (" " , "%20" )} .md) | { pbar } |\n ' )
142-
143- if __name__ == "__main__" :
144- attrs = parse_links ()
145- category = parse_category ()
146- gen_ind_workbook (attrs , category )
147- gen_total_workbook (attrs )
125+ if len (attr ) < 3 : # No workbook
126+ f .write (f'| { attr [0 ]} | { attr [1 ]} | |\n ' )
127+ else :
128+ f .write (f'| { attr [0 ]} | [{ attr [1 ]} ](workbook/{ attr [0 ].replace (" " , "%20" )} .md) | { pbar } |\n ' )
129+
130+ attrs = parse_links ()
131+ category = parse_category ()
132+ gen_ind_workbook (attrs , category )
133+ gen_total_workbook (attrs )
0 commit comments