leetcode_helper.py 26.9 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1
import os
每日一练社区's avatar
每日一练社区 已提交
2
import re
每日一练社区's avatar
每日一练社区 已提交
3
import shutil
4 5
import json
import uuid
每日一练社区's avatar
每日一练社区 已提交
6 7 8 9 10 11 12
import argparse
import collections
parser = argparse.ArgumentParser()
parser.add_argument("--run", type=str, help="Decide to run which function")
args = parser.parse_args()
helper_function = args.run

13

14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
def get_files_path(file_dir, filetype='.txt'):
    """得到文件夹下的所有.txt文件的路径
    Args:
        file_dir: 文件夹路径
        filetype: 文件后缀
    Returns:
        所有filetype类型文件的绝对路径
    """
    files_path = []
    for root, dirs, files in os.walk(file_dir):
        for file in files:
            if filetype is None or (os.path.splitext(file)[1] == filetype):
                files_path.append(os.path.join(root, file))
    return files_path

每日一练社区's avatar
每日一练社区 已提交
29 30 31
def leetcode_helper():
    data_dir = 'data/3.算法高阶/1.leetcode'
    dailycode_exercises_dir = '/Users/zhangzc/Desktop/workplace/daily-code-data/data/input/dailycode/leetcode/exercises'
每日一练社区's avatar
每日一练社区 已提交
32
    crawer_leetcode_dir = '/Users/zhangzc/Desktop/workplace/LeetCodeCN-Problem-Crawler/leetcode_html'
每日一练社区's avatar
每日一练社区 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
    dirs_ = os.listdir(data_dir)
    dirs = []
    for dir in dirs_:
        dir = os.path.join(data_dir, dir)
        if os.path.isdir(dir):
            dirs.append(dir)
    for dir in dirs:
        assert os.path.isdir(dir)
        exercises_id = dir.split('/')[-1].split('_')[0]
        if 0 <= int(exercises_id) and int(exercises_id) < 100:
            desc_src_path = os.path.join(os.path.join(dailycode_exercises_dir, exercises_id), '{}_desc.html'.format(exercises_id))
            cpp_code_src_path = os.path.join(os.path.join(dailycode_exercises_dir, exercises_id),'{}.cpp'.format(exercises_id))
            
            
            desc_dst_path = os.path.join(dir, 'desc.html')
            cpp_code_dst_path = os.path.join(dir, 'solution.cpp')

每日一练社区's avatar
每日一练社区 已提交
50 51
            # print(cpp_code_src_path)
            # print(cpp_code_dst_path)
每日一练社区's avatar
每日一练社区 已提交
52 53 54
            shutil.copy(desc_src_path, desc_dst_path)
            shutil.copy(cpp_code_src_path, cpp_code_dst_path)
        else:
每日一练社区's avatar
每日一练社区 已提交
55
            cpp_code_dst_path = os.path.join(dir, 'solution.cpp')
每日一练社区's avatar
每日一练社区 已提交
56 57 58
            shell_code_dst_path = os.path.join(dir, 'solution.sh')
            sql_code_dst_path = os.path.join(dir, 'solution.sql')

每日一练社区's avatar
每日一练社区 已提交
59 60
            if not os.path.exists(cpp_code_dst_path):
                open(cpp_code_dst_path, 'w', encoding='utf-8')
每日一练社区's avatar
每日一练社区 已提交
61 62 63 64 65 66
            
            if 100 <= int(exercises_id) and int(exercises_id) < 203:
                with open(cpp_code_dst_path, 'r', encoding='utf-8') as f:
                        cpp_code = f.read()
                if cpp_code == '' and not os.path.exists(shell_code_dst_path) and not os.path.exists(sql_code_dst_path):
                    print(cpp_code_dst_path)
每日一练社区's avatar
每日一练社区 已提交
67 68 69 70 71 72 73 74
            desc_src_path = os.path.join(crawer_leetcode_dir, str(int(exercises_id) + 1) + '.html')
            desc_dst_path = os.path.join(dir, 'desc.html')
            # print(desc_src_path)
            # print(desc_dst_path)

            if os.path.exists(desc_src_path):
                shutil.copy(desc_src_path, desc_dst_path)

每日一练社区's avatar
每日一练社区 已提交
75 76 77 78
                
            else:
                pass
                # print("该路径不存在,请检查: {}".format(desc_src_path))
每日一练社区's avatar
每日一练社区 已提交
79
            
每日一练社区's avatar
每日一练社区 已提交
80

每日一练社区's avatar
每日一练社区 已提交
81 82 83 84 85 86 87 88 89 90 91 92
def leetcode_helper_delete_md():
    data_dir = 'data/3.算法高阶/1.leetcode'
    dirs_ = os.listdir(data_dir)
    dirs = []
    for dir in dirs_:
        dir = os.path.join(data_dir, dir)
        if os.path.isdir(dir):
            dirs.append(dir)
    for dir in dirs:
        assert os.path.isdir(dir)
        exercises_id = dir.split('/')[-1].split('_')[0]
        title = dir.split('/')[-1].split('_')[1]
每日一练社区's avatar
每日一练社区 已提交
93
        if 0 <= int(exercises_id) and int(exercises_id) < 100:
每日一练社区's avatar
每日一练社区 已提交
94 95 96 97 98 99 100 101 102 103
            solution_md_path = os.path.join(dir, 'solution.md')
            # print(solution_md_path) 
            with open('leetcode_template.md', 'r', encoding='utf-8') as f:
                template = f.read()
            template = template.replace('# 两数之和', '# {}'.format(title))
            with open(solution_md_path, 'r', encoding='utf-8') as f:
                leetcode_solution_md_data = f.read()
            if leetcode_solution_md_data == template:
                os.remove(solution_md_path)

每日一练社区's avatar
每日一练社区 已提交
104 105 106 107 108 109 110 111 112 113 114 115
def leetcode_helper_update_md():
    data_dir = 'data/3.算法高阶/1.leetcode'
    dirs_ = os.listdir(data_dir)
    dirs = []
    for dir in dirs_:
        dir = os.path.join(data_dir, dir)
        if os.path.isdir(dir):
            dirs.append(dir)
    for dir in dirs:
        assert os.path.isdir(dir)
        exercises_id = dir.split('/')[-1].split('_')[0]
        title = dir.split('/')[-1].split('_')[1]
116
        if 0 <= int(exercises_id) and int(exercises_id) < 500:
每日一练社区's avatar
每日一练社区 已提交
117 118
            solution_md_path = os.path.join(dir, 'solution.md')
            desc_html_path = os.path.join(dir, 'desc.html')
119 120
            if not os.path.exists(desc_html_path):
                continue
每日一练社区's avatar
每日一练社区 已提交
121 122 123 124 125
            with open(solution_md_path, 'r', encoding='utf-8') as f:
                solution_md_data = f.read()
            with open(desc_html_path, 'r', encoding='utf-8') as f:
                desc_html_data = f.read()
            content = re.findall('# .*?\n(.*?)\n## aop'.format(title), solution_md_data, re.DOTALL)[0]
126
            new_content = desc_html_data + "\n<p>{}</p>".format("以下错误的选项是?")
每日一练社区's avatar
每日一练社区 已提交
127 128 129 130 131 132
            # print(solution_md_path)
            solution_md_data = solution_md_data.replace(content, new_content)
            # print(solution_md_data)
            with open(solution_md_path, 'w', encoding='utf-8') as f:
                f.write(solution_md_data)

133 134 135 136 137 138 139 140 141 142 143 144
def leetcode_helper_update_config():
    data_dir = 'data/3.算法高阶/1.leetcode'
    dirs_ = os.listdir(data_dir)
    dirs = []
    for dir in dirs_:
        dir = os.path.join(data_dir, dir)
        if os.path.isdir(dir):
            dirs.append(dir)
    for dir in dirs:
        assert os.path.isdir(dir)
        exercises_id = dir.split('/')[-1].split('_')[0]
        title = dir.split('/')[-1].split('_')[1]
145
        if 0 <= int(exercises_id) and int(exercises_id) < 500:
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
            solution_md_path = os.path.join(dir, 'solution.md')
            config_json_path = os.path.join(dir, 'config.json')
            solution_json_path = os.path.join(dir, 'solution.json')
            if os.path.exists(solution_md_path):
                with open(config_json_path, 'r', encoding='utf-8') as f:
                    config_data = json.load(f)
                config_data['export'] = ['solution.json']
                config_data['title'] = title

                config_data['keywords'] = ['leetcode', title]
                config_data_json = json.dumps(config_data, ensure_ascii=False, indent=4)
                with open(config_json_path, 'w', encoding='utf-8') as f:
                    f.write(config_data_json)
                exercise_id = uuid.uuid4().hex
                solution_json_data = {
                    "type": "code_options",
                    "author": "CSDN.net",
                    "source": "solution.md",
                    "exercise_id":exercise_id,
                }
                solution_json = json.dumps(solution_json_data, ensure_ascii=False, indent=3)
                with open(solution_json_path, 'w', encoding='utf-8') as f:
                    f.write(solution_json)


171 172 173 174 175 176 177 178 179 180 181 182 183 184
def count_tag_class():
    data_dir = '/Users/zhangzc/Desktop/workplace/skill_tree_pipeline/data/input/dailycode/leetcode/exercises'
    files = get_files_path(data_dir, '.json')
    tags_lists = []
    for file in files:
        with open(file, 'r') as f:
            data = json.load(f)
        tags = data['tags']
        tags_list = tags.split(',')
        tags_lists.extend(tags_list)
    tags_set = set(tags_lists)
    print(tags_set)


每日一练社区's avatar
每日一练社区 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
def count_exercises():
    dirs = ['data/2.算法中阶', 'data/3.算法高阶']
    exercises_ids = []
    for dir in dirs:
        dirs_ = os.listdir(dir)
        algo_floor_dirs = []
        for algo_floor_dir in dirs_:
            leetcode_class_dir = os.path.join(dir, algo_floor_dir)
            if os.path.isdir(leetcode_class_dir):
                algo_floor_dirs.append(leetcode_class_dir)
        exercises_dirs = []
        for algo_floor_dir in algo_floor_dirs:
            exercises_dirs_ = os.listdir(algo_floor_dir)
            for exercises_dir_ in exercises_dirs_:
                exercises_dir = os.path.join(algo_floor_dir, exercises_dir_)
                if os.path.isdir(exercises_dir):
                    exercises_dirs.append(exercises_dir)
        for exercises_dir in exercises_dirs:
每日一练社区's avatar
每日一练社区 已提交
203
            # print(exercises_dir)
每日一练社区's avatar
每日一练社区 已提交
204 205 206 207 208 209 210 211 212 213 214 215 216
            exercises_id = int(exercises_dir.split('/')[-1].split('_')[0])
            exercises_ids.append(exercises_id)
    try:
        assert len(set(exercises_ids)) == len(exercises_ids)
    except:
        print(collections.Counter(exercises_ids))
        print('------分割线-------')
    dst_exercises_ids = [i for i in range(200)]
    lacked_id = set(dst_exercises_ids) - set(exercises_ids)
    print(lacked_id)
        


每日一练社区's avatar
每日一练社区 已提交
217
def modify_config_and_dir_name():
每日一练社区's avatar
每日一练社区 已提交
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
    # dirs = ['data/2.算法中阶', 'data/3.算法高阶']
    # exercises_ids = []
    # for dir in dirs:
    dir = 'data_backup/1.leetcode'
    dirs_ = os.listdir(dir)
    algo_floor_dirs = []
    for algo_floor_dir in dirs_:
        leetcode_class_dir = os.path.join(dir, algo_floor_dir)
        if os.path.isdir(leetcode_class_dir):
            algo_floor_dirs.append(leetcode_class_dir)
    exercises_dirs = []
    for algo_floor_dir in algo_floor_dirs:
        print(algo_floor_dir)
        root_dir = '/'.join(algo_floor_dir.split('/')[:-1])
        exercises_id = algo_floor_dir.split('/')[-1].split('.')[0]
        title = algo_floor_dir.split('/')[-1].split('.')[-1]

        config_path = os.path.join(algo_floor_dir, 'config.json')
        config_data = {}
        # with open(config_path, 'r', encoding='utf-8') as f:
        #     config_data = json.load(f)

        config_data['node_id'] = "algorithm-" +  uuid.uuid4().hex
        config_data['keywords'] = ["leetcode", title]
        config_data['children'] = []
        config_data['export'] = ['solution.json']
        config_data['title'] = title
        print(title)
        config_data_json = json.dumps(config_data, ensure_ascii=False, indent=2)
每日一练社区's avatar
每日一练社区 已提交
247

每日一练社区's avatar
每日一练社区 已提交
248 249
        with open(config_path, 'w', encoding='utf-8') as f:
            f.write(config_data_json)
每日一练社区's avatar
每日一练社区 已提交
250 251


每日一练社区's avatar
每日一练社区 已提交
252 253


254 255
def modify_config_and_dir_name_new():

每日一练社区's avatar
每日一练社区 已提交
256
    dirs = ['data/2.算法中阶', 'data/3.算法高阶', 'data/1.算法初阶']
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
    exercises_ids = []
    for dir in dirs:
        dirs_ = os.listdir(dir)
        algo_floor_dirs = []
        for algo_floor_dir in dirs_:
            leetcode_class_dir = os.path.join(dir, algo_floor_dir)
            if os.path.isdir(leetcode_class_dir):
                algo_floor_dirs.append(leetcode_class_dir)
        
        for algo_floor_dir in algo_floor_dirs:
            exercises_dirs_ = os.listdir(algo_floor_dir)
            exercises_dirs = []
            for exercises_dir_ in exercises_dirs_:
                exercises_dir = os.path.join(algo_floor_dir, exercises_dir_)
                if os.path.isdir(exercises_dir):
                    exercises_dirs.append(exercises_dir)
            for idx, tem_dir in enumerate(exercises_dirs):
每日一练社区's avatar
每日一练社区 已提交
274
                config_path = os.path.join(tem_dir, 'config.json')
每日一练社区's avatar
每日一练社区 已提交
275
                solution_md_path = os.path.join(tem_dir, 'solution.md')
276 277 278 279 280
                
                if dir == "data/1.算法初阶":
                    title = tem_dir.split('/')[-1].split('.')[-1]
                else:
                    title = tem_dir.split('/')[-1].split('-')[-1]
每日一练社区's avatar
每日一练社区 已提交
281 282
                with open(solution_md_path, 'r', encoding='utf-8') as f:
                    solution_md_data = f.read()
283 284
                if solution_md_data.find('# {}\n\n'.format(title)) == -1:
                    solution_md_data = solution_md_data.replace('# {}'.format(title), '# {}\n'.format(title))
285
                    
286 287
                if solution_md_data.find('## aop\n\n') == -1:
                    solution_md_data = solution_md_data.replace('## aop', '## aop\n')
每日一练社区's avatar
每日一练社区 已提交
288

289 290 291 292 293 294 295 296 297 298 299 300 301 302
                if solution_md_data.find('## 答案\n\n') == -1:
                    solution_md_data = solution_md_data.replace('## 答案', '## 答案\n')
                
                if solution_md_data.find('## 选项\n\n') == -1:
                    solution_md_data = solution_md_data.replace('## 选项', '## 选项\n')
                
                if solution_md_data.find('### before\n\n') == -1:
                    solution_md_data = solution_md_data.replace('### before', '### before\n')

                if solution_md_data.find('### after\n\n') == -1:
                    solution_md_data = solution_md_data.replace('### after', '### after\n')

                if solution_md_data.find('\n\n```cpp') == -1:
                    solution_md_data = solution_md_data.replace('```cpp', '\n```cpp')
303 304 305 306
                
                if solution_md_data.find('\n\n### ') == -1:
                    solution_md_data = solution_md_data.replace('### ', '\n### ')
                    print(tem_dir)
307 308 309 310 311 312 313 314 315 316 317 318 319 320

                with open(solution_md_path, 'w', encoding='utf-8') as f:
                    f.write(solution_md_data)
                


                # with open(config_path, 'r', encoding='utf-8') as f:
                #     config_data = json.load(f)
                # del config_data['title']
                # config_data_json = json.dumps(config_data, ensure_ascii=False, indent=2)
                # print(config_data_json)

                # with open(config_path, 'w', encoding='utf-8') as f:
                #     f.write(config_data_json)
每日一练社区's avatar
每日一练社区 已提交
321 322


323 324


每日一练社区's avatar
每日一练社区 已提交
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346

def modify_back_up_dir_name():
    # dirs = ['data/2.算法中阶', 'data/3.算法高阶']
    # exercises_ids = []
    # for dir in dirs:
    dir = 'data_backup/1.leetcode'
    dirs_ = os.listdir(dir)
    algo_floor_dirs = []
    for algo_floor_dir in dirs_:
        leetcode_class_dir = os.path.join(dir, algo_floor_dir)
        if os.path.isdir(leetcode_class_dir):
            algo_floor_dirs.append(leetcode_class_dir)
    exercises_dirs = []
    for algo_floor_dir in algo_floor_dirs:
        print(algo_floor_dir)
        root_dir = '/'.join(algo_floor_dir.split('/')[:-1])
        exercises_id = algo_floor_dir.split('/')[-1].split('.')[0]
        title = algo_floor_dir.split('/')[-1].split('.')[-1]
        new_dir_name = '{}-{}'.format(int(exercises_id) + 1, title)
        new_dir_name = os.path.join(root_dir, new_dir_name)
        os.rename(algo_floor_dir, new_dir_name)
        print(new_dir_name)
每日一练社区's avatar
每日一练社区 已提交
347
    
348

每日一练社区's avatar
每日一练社区 已提交
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
def leetcode_helper_add_sloutionjson():
    data_dir = 'data_backup/1.leetcode'
    dirs_ = os.listdir(data_dir)
    dirs = []
    for dir in dirs_:
        dir = os.path.join(data_dir, dir)
        if os.path.isdir(dir):
            dirs.append(dir)
    for dir in dirs:
        assert os.path.isdir(dir)
        # exercises_id = dir.split('/')[-1].split('_')[0]
        # title = dir.split('/')[-1].split('_')[1]

        # solution_md_path = os.path.join(dir, 'solution.md')
        # config_json_path = os.path.join(dir, 'config.json')
        solution_json_path = os.path.join(dir, 'solution.json')
        print(solution_json_path)
        if not os.path.exists(solution_json_path):
            exercise_id = uuid.uuid4().hex
            solution_json_data = {
                "type": "code_options",
                "author": "CSDN.net",
                "source": "solution.md",
                "exercise_id":exercise_id,
            }
            solution_json = json.dumps(solution_json_data, ensure_ascii=False, indent=2)
            with open(solution_json_path, 'w', encoding='utf-8') as f:
                f.write(solution_json)



380
def fix_bug():
每日一练社区's avatar
每日一练社区 已提交
381
    data_dir = 'data_backup/1.leetcode'
382 383 384 385 386 387 388 389 390
    dirs_ = os.listdir(data_dir)
    dirs = []
    for dir in dirs_:
        dir = os.path.join(data_dir, dir)
        if os.path.isdir(dir):
            dirs.append(dir)
    for dir in dirs:
        assert os.path.isdir(dir)
        
每日一练社区's avatar
每日一练社区 已提交
391 392 393 394 395
        solution_md_path = os.path.join(dir, 'solution.md')
        if not os.path.exists(solution_md_path):
            continue
        with open(solution_md_path, 'r', encoding='utf-8') as f:
            solution_md_data = f.read()
396
        # print(dir)
每日一练社区's avatar
每日一练社区 已提交
397
        title = dir.split('/')[-1].split('-')[-1]
398
        # print(title)
每日一练社区's avatar
每日一练社区 已提交
399 400 401 402 403 404 405 406 407
        solution_md_data = solution_md_data.replace('# 两数之和', '# {}'.format(title))
        if solution_md_data.find('# {}\n\n'.format(title)) == -1:
                solution_md_data = solution_md_data.replace('# {}'.format(title), '# {}\n'.format(title)) 
        if solution_md_data.find('## aop\n\n') == -1:
            solution_md_data = solution_md_data.replace('## aop', '## aop\n')

        if solution_md_data.find('## 答案\n\n') == -1:
            solution_md_data = solution_md_data.replace('## 答案', '## 答案\n')
        
408 409
        if solution_md_data.find('\n\n## 选项\n\n') == -1:
            solution_md_data = solution_md_data.replace('## 选项', '\n## 选项\n')
每日一练社区's avatar
每日一练社区 已提交
410 411 412 413 414 415 416
        
        if solution_md_data.find('### before\n\n') == -1:
            solution_md_data = solution_md_data.replace('### before', '### before\n')

        if solution_md_data.find('### after\n\n') == -1:
            solution_md_data = solution_md_data.replace('### after', '### after\n')

417 418 419 420 421 422 423 424
        if solution_md_data.find('A\n\n```cpp') == -1:
            solution_md_data = solution_md_data.replace('A\n```cpp', 'A\n\n```cpp')
        
        if solution_md_data.find('B\n\n```cpp') == -1:
            solution_md_data = solution_md_data.replace('B\n```cpp', 'B\n\n```cpp')
        if solution_md_data.find('C\n\n```cpp') == -1:
            solution_md_data = solution_md_data.replace('C\n```cpp', 'C\n\n```cpp')
            print(dir)
每日一练社区's avatar
每日一练社区 已提交
425 426 427 428 429 430 431 432
        
        if solution_md_data.find('\n\n### ') == -1:
            solution_md_data = solution_md_data.replace('### ', '\n### ')
        with open(solution_md_path, 'w', encoding='utf-8') as f:
            f.write(solution_md_data)



每日一练社区's avatar
每日一练社区 已提交
433
def add_color_for_special_exercises():
每日一练社区's avatar
每日一练社区 已提交
434
    count = 0
每日一练社区's avatar
每日一练社区 已提交
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455

    dirs = ['data/2.算法中阶', 'data/3.算法高阶', 'data/1.算法初阶']
    exercises_ids = []
    for dir in dirs:
        dirs_ = os.listdir(dir)
        algo_floor_dirs = []
        for algo_floor_dir in dirs_:
            leetcode_class_dir = os.path.join(dir, algo_floor_dir)
            if os.path.isdir(leetcode_class_dir):
                algo_floor_dirs.append(leetcode_class_dir)
        
        for algo_floor_dir in algo_floor_dirs:
            exercises_dirs_ = os.listdir(algo_floor_dir)
            exercises_dirs = []
            for exercises_dir_ in exercises_dirs_:
                exercises_dir = os.path.join(algo_floor_dir, exercises_dir_)
                if os.path.isdir(exercises_dir):
                    exercises_dirs.append(exercises_dir)
            for idx, tem_dir in enumerate(exercises_dirs):
                config_path = os.path.join(tem_dir, 'config.json')
                solution_md_path = os.path.join(tem_dir, 'solution.md')
每日一练社区's avatar
每日一练社区 已提交
456
                count+=1
每日一练社区's avatar
每日一练社区 已提交
457
                print(solution_md_path)
每日一练社区's avatar
每日一练社区 已提交
458 459
                with open(solution_md_path, 'r', encoding='utf-8') as f:
                    solution_md_data = f.read()
每日一练社区's avatar
每日一练社区 已提交
460 461
                if solution_md_data.find('<span style="color:red">错误</span>') == -1:
                    solution_md_data = solution_md_data.replace('错误', '<span style="color:red">错误</span>')
每日一练社区's avatar
每日一练社区 已提交
462 463 464 465
                    with open(solution_md_path, 'w', encoding='utf-8') as f:
                        f.write(solution_md_data)
    
    print('当前已有 {} 道习题'.format(count))
每日一练社区's avatar
每日一练社区 已提交
466

467 468 469



每日一练社区's avatar
每日一练社区 已提交
470 471 472 473 474 475 476 477 478 479 480 481
def add_solution_md_template_for_lanqiao():
    data_dir = 'data_backup/3.蓝桥杯'
    dirs_ = os.listdir(data_dir)
    dirs = []
    for dir in dirs_:
        dir = os.path.join(data_dir, dir)
        if os.path.isdir(dir):
            dirs.append(dir)
    for dir in dirs:
        assert os.path.isdir(dir)
        
        solution_md_path = os.path.join(dir, 'solution.md')
每日一练社区's avatar
每日一练社区 已提交
482 483
        solution_json_path = os.path.join(dir, 'solution.json')

每日一练社区's avatar
每日一练社区 已提交
484 485 486 487 488 489 490 491 492 493 494 495
        if not os.path.exists(solution_md_path):
            template_path = 'lanqiao_template.md'
            title = dir.split('/')[-1].split('.')[-1]
            desc_path = os.path.join(dir, 'desc.md')
            with open(desc_path, 'r', encoding='utf-8') as f:
                desc_data = f.read()
            with open(template_path, 'r', encoding='utf-8') as f:
                template_data = f.read()
            template_data = template_data.replace('# 两数之和\n', '# {}\n\n{}'.format(title, desc_data))
            print(template_data)
            with open(solution_md_path, 'w', encoding='utf-8') as f:
                f.write(template_data)
每日一练社区's avatar
每日一练社区 已提交
496 497 498
        else:
            with open(solution_md_path, 'r', encoding='utf-8') as f:
                solution_md_data = f.read()
499 500 501
            solution_md_data = re.sub(r'#### (.*)\n', r'\n**\1**\n\n', solution_md_data)
            with open(solution_md_path, 'w', encoding='utf-8') as f:
                f.write(solution_md_data)
每日一练社区's avatar
每日一练社区 已提交
502 503 504 505 506 507 508 509 510 511 512 513 514 515
        if not os.path.exists(solution_json_path):
            exercise_id = uuid.uuid4().hex
            solution_json_data = {
                    "type": "code_options",
                    "author": "CSDN.net",
                    "source": "solution.md",
                    "exercise_id": exercise_id
                }
            solution_json_data_json = json.dumps(solution_json_data, ensure_ascii=False, indent=2)
            with open(solution_json_path, 'w', encoding='utf-8') as f:
                f.write(solution_json_data_json)
        else:
            pass

每日一练社区's avatar
每日一练社区 已提交
516

每日一练社区's avatar
每日一练社区 已提交
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
def check_exercises_is_duplicate():
    count = 0

    dirs = ['data/2.算法中阶', 'data/3.算法高阶', 'data/1.算法初阶']
    exercises_ids = []
    for dir in dirs:
        exercises_check_list = []
        dirs_ = os.listdir(dir)
        algo_floor_dirs = []
        for algo_floor_dir in dirs_:
            leetcode_class_dir = os.path.join(dir, algo_floor_dir)
            if os.path.isdir(leetcode_class_dir):
                algo_floor_dirs.append(leetcode_class_dir)
        
        for algo_floor_dir in algo_floor_dirs:
            exercises_dirs_ = os.listdir(algo_floor_dir)
            exercises_dirs = []
            for exercises_dir_ in exercises_dirs_:
                exercises_dir = os.path.join(algo_floor_dir, exercises_dir_)
                if os.path.isdir(exercises_dir):
                    exercises_dirs.append(exercises_dir)
            for idx, tem_dir in enumerate(exercises_dirs):
                # print(tem_dir)
                title = tem_dir.split('/')[-1].split('.')[-1]
                exercises_check_list.append(title)
        print(collections.Counter(exercises_check_list))

544

每日一练社区's avatar
fix bug  
每日一练社区 已提交
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
def add_config_export():
    count = 0

    dirs = ['data/2.算法中阶', 'data/3.算法高阶', 'data/1.算法初阶']
    exercises_ids = []
    for dir in dirs:
        dirs_ = os.listdir(dir)
        algo_floor_dirs = []
        for algo_floor_dir in dirs_:
            leetcode_class_dir = os.path.join(dir, algo_floor_dir)
            if os.path.isdir(leetcode_class_dir):
                algo_floor_dirs.append(leetcode_class_dir)
        
        for algo_floor_dir in algo_floor_dirs:
            exercises_dirs_ = os.listdir(algo_floor_dir)
            exercises_dirs = []
            for exercises_dir_ in exercises_dirs_:
                exercises_dir = os.path.join(algo_floor_dir, exercises_dir_)
                if os.path.isdir(exercises_dir):
                    exercises_dirs.append(exercises_dir)
            for idx, tem_dir in enumerate(exercises_dirs):
                print(tem_dir)
                config_path = os.path.join(tem_dir, 'config.json')
                with open(config_path, 'r', encoding='utf-8') as f:
                    config_data = json.load(f)
                config_data['export'] = ["solution.json"]
                config_data_json = json.dumps(config_data, ensure_ascii=False, indent=2)
                with open(config_path, 'w', encoding='utf-8') as f:
                    f.write(config_data_json)

575

每日一练社区's avatar
fix bug  
每日一练社区 已提交
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
def add_config_export_for_lanqiao_backup():
    data_dir = 'data_backup/3.蓝桥杯'
    dirs_ = os.listdir(data_dir)
    dirs = []
    for dir in dirs_:
        dir = os.path.join(data_dir, dir)
        if os.path.isdir(dir):
            dirs.append(dir)
    for dir in dirs:
        assert os.path.isdir(dir)
        
        config_path = os.path.join(dir, 'config.json')
        print(config_path)
        with open(config_path, 'r', encoding='utf-8') as f:
            config_data = json.load(f)
        config_data['export'] = ["solution.json"]
        config_data_json = json.dumps(config_data, ensure_ascii=False, indent=2)
        with open(config_path, 'w', encoding='utf-8') as f:
            f.write(config_data_json)
每日一练社区's avatar
每日一练社区 已提交
595

596

每日一练社区's avatar
每日一练社区 已提交
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
def fixbug_for_md():
    count = 0

    dirs = ['data/2.算法中阶', 'data/3.算法高阶', 'data/1.算法初阶']
    exercises_ids = []
    for dir in dirs:
        dirs_ = os.listdir(dir)
        algo_floor_dirs = []
        for algo_floor_dir in dirs_:
            leetcode_class_dir = os.path.join(dir, algo_floor_dir)
            if os.path.isdir(leetcode_class_dir):
                algo_floor_dirs.append(leetcode_class_dir)
        
        for algo_floor_dir in algo_floor_dirs:
            exercises_dirs_ = os.listdir(algo_floor_dir)
            exercises_dirs = []
            for exercises_dir_ in exercises_dirs_:
                exercises_dir = os.path.join(algo_floor_dir, exercises_dir_)
                if os.path.isdir(exercises_dir):
                    exercises_dirs.append(exercises_dir)
            for idx, tem_dir in enumerate(exercises_dirs):
                print(tem_dir)
                solution_md_path = os.path.join(tem_dir, 'solution.md')
                with open(solution_md_path, 'r') as f:
                    solution_md_data = f.read()
每日一练社区's avatar
每日一练社区 已提交
622
                solution_md_data = solution_md_data.replace('```cpp', '```c')
每日一练社区's avatar
每日一练社区 已提交
623 624 625 626 627 628
                with open(solution_md_path, 'w') as f:
                    f.write(solution_md_data)




每日一练社区's avatar
每日一练社区 已提交
629 630 631 632
if helper_function == 'count_tag_class':
    count_tag_class()
if helper_function == 'count_exercises':
    count_exercises()
每日一练社区's avatar
每日一练社区 已提交
633 634 635 636
if helper_function == 'modify_back_up_dir_name':
    modify_back_up_dir_name()


每日一练社区's avatar
每日一练社区 已提交
637

每日一练社区's avatar
每日一练社区 已提交
638 639 640 641
# add_solution_md_template_for_lanqiao()

# add_color_for_special_exercises()

每日一练社区's avatar
fix bug  
每日一练社区 已提交
642 643


每日一练社区's avatar
每日一练社区 已提交
644

每日一练社区's avatar
每日一练社区 已提交
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
def remove_unse_md_file():
    count = 0

    dirs = ['data/2.算法中阶', 'data/3.算法高阶', 'data/1.算法初阶']
    exercises_ids = []
    for dir in dirs:
        dirs_ = os.listdir(dir)
        algo_floor_dirs = []
        for algo_floor_dir in dirs_:
            leetcode_class_dir = os.path.join(dir, algo_floor_dir)
            if os.path.isdir(leetcode_class_dir):
                algo_floor_dirs.append(leetcode_class_dir)
        
        for algo_floor_dir in algo_floor_dirs:
            exercises_dirs_ = os.listdir(algo_floor_dir)
            exercises_dirs = []
            for exercises_dir_ in exercises_dirs_:
                exercises_dir = os.path.join(algo_floor_dir, exercises_dir_)
                if os.path.isdir(exercises_dir):
                    exercises_dirs.append(exercises_dir)
            for idx, tem_dir in enumerate(exercises_dirs):
                
                desc_md_path = os.path.join(tem_dir, 'desc.md')
                print(desc_md_path)
                if os.path.exists(desc_md_path):
                    os.remove(desc_md_path)

    
remove_unse_md_file()


每日一练社区's avatar
每日一练社区 已提交
676

每日一练社区's avatar
每日一练社区 已提交
677 678
# check_exercises_is_duplicate()

每日一练社区's avatar
每日一练社区 已提交
679
# leetcode_helper_update_md()
680 681 682
# leetcode_helper_update_config()