11import os
2-
32import requests
43
54pbars = []
6- # ['0x11', '그리디', 'https://www.acmicpc.net/workbook/view/7320']
5+
76def parse_links ():
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
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
1312
1413def parse_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
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
2019
2120def get_problem_info (workbook_url ):
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
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
4245
4346CATEGORY = ["연습 문제" , "기본 문제✔" , "기본 문제" , "응용 문제✔" , "응용 문제" ]
4447
45- # gen 0x00.md to 0x??.md, proper prob_id.cpp for each solution directory
4648def gen_ind_workbook (attrs , category ):
47- txt = '''// Authored by : BaaaaaaaaaaarkingDog
49+ java_template = '''// Authored by : BaaaaaaaaaaarkingDog
4850// Co-authored by : -
4951// http://boj.kr/****************
50- #include <bits/stdc++.h>
51- using namespace std;
52-
53- int main(void){
54- ios::sync_with_stdio(0);
55- cin.tie(0);
56-
57- }'''
58- chapter_idx = 0
59- for attr in attrs :
60- if len (attr ) < 3 : # No workbook
61- pbars .append ("" )
62- continue
63- solution_num = 0
64- solution_path = f'../{ attr [0 ]} /solutions/'
65- category_idx = 0
66- problem_infos = get_problem_info (attr [2 ])
67- prob_table = '| 문제 분류 | 문제 | 문제 제목 | 정답 코드 |\n | :--: | :--: | :--: | :--: |\n '
68- for prob_id , prob_name in problem_infos :
69- if prob_id in category [chapter_idx ]:
70- category_idx = category [chapter_idx ].index (prob_id )
71- file_path = solution_path + prob_id
72- if not os .path .exists (file_path + '.cpp' ):
73- with open (file_path + '.cpp' , 'w' , encoding = "UTF-8" ) as f :
74- f .write (txt )
75- try :
76- codes = open (file_path + '.cpp' , 'r' , encoding = "UTF-8" ).read ()
77- except : # EUC-KR -> UTF-8
78- codes = open (file_path + '.cpp' , 'r' , encoding = "EUC-KR" ).read ()
79- with open (file_path + '.cpp' , 'w' , encoding = "UTF-8" ) as fw :
80- fw .write (codes )
81- if codes [:100 ] == txt [:100 ]:
82- prob_table += f'| { CATEGORY [category_idx ]} | { prob_id } | [{ prob_name } ](https://www.acmicpc.net/problem/{ prob_id } ) | - |\n '
83- else :
84- solution_num += 1
85- code_attr = f'[정답 코드]({ file_path .replace (" " , "%20" )} .cpp)'
86- MAX_DIFFERENT_SOLUTION = 9
87- for i in range (1 , MAX_DIFFERENT_SOLUTION + 1 ):
88- if os .path .exists (file_path + '_' + str (i )+ '.cpp' ):
89- code_attr += f", [별해 { i } ]({ file_path + '_' + str (i )+ '.cpp' } )"
90- prob_table += f'| { CATEGORY [category_idx ]} | { prob_id } | [{ prob_name } ](https://www.acmicpc.net/problem/{ prob_id } ) | { code_attr } |\n '
91- with open (attr [0 ]+ '.md' , 'w' , encoding = "UTF-8" ) as f :
92- # progress bar
93- f .write (f'# { attr [1 ]} \n \n ' )
94- pbar = f'} &title=progress&width=500&color=babaca&suffix=/{ len (problem_infos )} )'
95- pbars .append (pbar )
96- f .write (pbar + '\n \n ' )
97- f .write (f'[문제집 링크]({ attr [2 ]} )\n \n ' )
98- f .write (prob_table )
99- chapter_idx += 1
100-
52+ '''
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+ prob_table = '| 문제 분류 | 문제 | 문제 제목 | 정답 코드 |\n | :--: | :--: | :--: | :--: |\n '
66+
67+ for prob_id , prob_name in problem_infos :
68+ if prob_id in category [chapter_idx ]:
69+ category_idx = category [chapter_idx ].index (prob_id )
70+ file_path = os .path .join (solution_path , prob_id )
71+ java_file = file_path + '.java'
72+
73+ if not os .path .exists (java_file ):
74+ with open (java_file , 'w' , encoding = "UTF-8" ) as f :
75+ f .write (java_template )
76+
77+ try :
78+ codes = open (java_file , 'r' , encoding = "UTF-8" ).read ()
79+ except :
80+ codes = open (java_file , 'r' , encoding = "EUC-KR" ).read ()
81+ with open (java_file , 'w' , encoding = "UTF-8" ) as fw :
82+ fw .write (codes )
83+
84+ if codes [:100 ] == java_template [:100 ]:
85+ prob_table += f'| { CATEGORY [category_idx ]} | { prob_id } | [{ prob_name } ](https://www.acmicpc.net/problem/{ prob_id } ) | - |\n '
86+ else :
87+ solution_num += 1
88+ code_attr = f'[정답 코드]({ java_file .replace (" " , "%20" )} )'
89+ for i in range (1 , 10 ):
90+ alt_path = f'{ file_path } _{ i } .java'
91+ if os .path .exists (alt_path ):
92+ code_attr += f", [별해 { i } ]({ alt_path .replace (' ' , '%20' )} )"
93+ prob_table += f'| { CATEGORY [category_idx ]} | { prob_id } | [{ prob_name } ](https://www.acmicpc.net/problem/{ prob_id } ) | { code_attr } |\n '
94+
95+ with open (attr [0 ] + '.md' , 'w' , encoding = "UTF-8" ) as f :
96+ f .write (f'# { attr [1 ]} \n \n ' )
97+ pbar = f'} &title=progress&width=500&color=babaca&suffix=/{ len (problem_infos )} )'
98+ pbars .append (pbar )
99+ f .write (pbar + '\n \n ' )
100+ f .write (f'[문제집 링크]({ attr [2 ]} )\n \n ' )
101+ f .write (prob_table )
102+
103+ chapter_idx += 1
104+
101105# ['0x11', '그리디', 'https://www.acmicpc.net/workbook/view/7320']
102106def gen_total_workbook (attrs ):
103107 with open ('../workbook.md' , 'w' , encoding = "UTF-8" ) as f :
@@ -128,13 +132,14 @@ def gen_total_workbook(attrs):
128132# 문제집
129133| 번호 | 주제 | 진행도 |
130134| :--: | :--: | :--: |\n ''' )
131- for attr , pbar in zip (attrs , pbars ):
132- if len (attr ) < 3 : # No workbook
133- f .write (f'| { attr [0 ]} | { attr [1 ]} | |\n ' )
134- else :
135- f .write (f'| { attr [0 ]} | [{ attr [1 ]} ](workbook/{ attr [0 ].replace (" " , "%20" )} .md) | { pbar } |\n ' )
136-
137- attrs = parse_links ()
138- category = parse_category ()
139- gen_ind_workbook (attrs , category )
140- gen_total_workbook (attrs )
135+ for attr , pbar in zip (attrs , pbars ):
136+ if len (attr ) < 3 :
137+ f .write (f'| { attr [0 ]} | { attr [1 ]} | |\n ' )
138+ else :
139+ f .write (f'| { attr [0 ]} | [{ attr [1 ]} ](workbook/{ attr [0 ].replace (" " , "%20" )} .md) | { pbar } |\n ' )
140+
141+ if __name__ == "__main__" :
142+ attrs = parse_links ()
143+ category = parse_category ()
144+ gen_ind_workbook (attrs , category )
145+ gen_total_workbook (attrs )
0 commit comments