leetcode_helper.py 8.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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
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:
            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)
        




if helper_function == 'count_tag_class':
    count_tag_class()
if helper_function == 'count_exercises':
    count_exercises()


每日一练社区's avatar
每日一练社区 已提交
224 225 226



每日一练社区's avatar
每日一练社区 已提交
227 228
# leetcode_helper_update_md()
# leetcode_helper_update_config()