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

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

每日一练社区's avatar
每日一练社区 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
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]
        if 0 <= int(exercises_id) and int(exercises_id) < 100:
            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)

leetcode_helper_delete_md()