leetcode_helper.py 4.6 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1
import os
每日一练社区's avatar
每日一练社区 已提交
2
import re
每日一练社区's avatar
每日一练社区 已提交
3 4 5 6
import shutil
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
每日一练社区 已提交
7
    crawer_leetcode_dir = '/Users/zhangzc/Desktop/workplace/LeetCodeCN-Problem-Crawler/leetcode_html'
每日一练社区's avatar
每日一练社区 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
    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
每日一练社区 已提交
25 26
            # print(cpp_code_src_path)
            # print(cpp_code_dst_path)
每日一练社区's avatar
每日一练社区 已提交
27 28 29
            shutil.copy(desc_src_path, desc_dst_path)
            shutil.copy(cpp_code_src_path, cpp_code_dst_path)
        else:
每日一练社区's avatar
每日一练社区 已提交
30
            cpp_code_dst_path = os.path.join(dir, 'solution.cpp')
每日一练社区's avatar
每日一练社区 已提交
31 32 33
            shell_code_dst_path = os.path.join(dir, 'solution.sh')
            sql_code_dst_path = os.path.join(dir, 'solution.sql')

每日一练社区's avatar
每日一练社区 已提交
34 35
            if not os.path.exists(cpp_code_dst_path):
                open(cpp_code_dst_path, 'w', encoding='utf-8')
每日一练社区's avatar
每日一练社区 已提交
36 37 38 39 40 41
            
            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
每日一练社区 已提交
42 43 44 45 46 47 48 49
            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
每日一练社区 已提交
50 51 52 53
                
            else:
                pass
                # print("该路径不存在,请检查: {}".format(desc_src_path))
每日一练社区's avatar
每日一练社区 已提交
54
            
每日一练社区's avatar
每日一练社区 已提交
55

每日一练社区's avatar
每日一练社区 已提交
56 57 58 59 60 61 62 63 64 65 66 67
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
每日一练社区 已提交
68
        if 0 <= int(exercises_id) and int(exercises_id) < 100:
每日一练社区's avatar
每日一练社区 已提交
69 70 71 72 73 74 75 76 77 78
            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
每日一练社区 已提交
79 80 81 82 83 84 85 86 87 88 89 90
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]
每日一练社区's avatar
每日一练社区 已提交
91
        if 0 <= int(exercises_id) and int(exercises_id) < 63:
每日一练社区's avatar
每日一练社区 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
            solution_md_path = os.path.join(dir, 'solution.md')
            desc_html_path = os.path.join(dir, 'desc.html')
            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]
            new_content = desc_html_data + "\n<p>{}</p>".format(content)
            # 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)
            

每日一练社区's avatar
每日一练社区 已提交
107
leetcode_helper_delete_md()