leetcode_helper.py 7.4 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1
import os
每日一练社区's avatar
每日一练社区 已提交
2
import re
每日一练社区's avatar
每日一练社区 已提交
3
import shutil
4 5 6
import json
import uuid

7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
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
每日一练社区 已提交
22 23 24
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
每日一练社区 已提交
25
    crawer_leetcode_dir = '/Users/zhangzc/Desktop/workplace/LeetCodeCN-Problem-Crawler/leetcode_html'
每日一练社区's avatar
每日一练社区 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
    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
每日一练社区 已提交
43 44
            # print(cpp_code_src_path)
            # print(cpp_code_dst_path)
每日一练社区's avatar
每日一练社区 已提交
45 46 47
            shutil.copy(desc_src_path, desc_dst_path)
            shutil.copy(cpp_code_src_path, cpp_code_dst_path)
        else:
每日一练社区's avatar
每日一练社区 已提交
48
            cpp_code_dst_path = os.path.join(dir, 'solution.cpp')
每日一练社区's avatar
每日一练社区 已提交
49 50 51
            shell_code_dst_path = os.path.join(dir, 'solution.sh')
            sql_code_dst_path = os.path.join(dir, 'solution.sql')

每日一练社区's avatar
每日一练社区 已提交
52 53
            if not os.path.exists(cpp_code_dst_path):
                open(cpp_code_dst_path, 'w', encoding='utf-8')
每日一练社区's avatar
每日一练社区 已提交
54 55 56 57 58 59
            
            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
每日一练社区 已提交
60 61 62 63 64 65 66 67
            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
每日一练社区 已提交
68 69 70 71
                
            else:
                pass
                # print("该路径不存在,请检查: {}".format(desc_src_path))
每日一练社区's avatar
每日一练社区 已提交
72
            
每日一练社区's avatar
每日一练社区 已提交
73

每日一练社区's avatar
每日一练社区 已提交
74 75 76 77 78 79 80 81 82 83 84 85
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
每日一练社区 已提交
86
        if 0 <= int(exercises_id) and int(exercises_id) < 100:
每日一练社区's avatar
每日一练社区 已提交
87 88 89 90 91 92 93 94 95 96
            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
每日一练社区 已提交
97 98 99 100 101 102 103 104 105 106 107 108
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]
109
        if 0 <= int(exercises_id) and int(exercises_id) < 500:
每日一练社区's avatar
每日一练社区 已提交
110 111
            solution_md_path = os.path.join(dir, 'solution.md')
            desc_html_path = os.path.join(dir, 'desc.html')
112 113
            if not os.path.exists(desc_html_path):
                continue
每日一练社区's avatar
每日一练社区 已提交
114 115 116 117 118
            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]
119
            new_content = desc_html_data + "\n<p>{}</p>".format("以下错误的选项是?")
每日一练社区's avatar
每日一练社区 已提交
120 121 122 123 124 125
            # 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)

126 127 128 129 130 131 132 133 134 135 136 137
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]
138
        if 0 <= int(exercises_id) and int(exercises_id) < 500:
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
            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)


164 165 166 167 168 169 170 171 172 173 174 175 176 177
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)


178
leetcode_helper_update_md()
179
leetcode_helper_update_config()