-
Notifications
You must be signed in to change notification settings - Fork 1
/
gen.py
52 lines (40 loc) · 1.62 KB
/
gen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# -*- coding:utf-8 -*-
"""
This module is used to generate README file content list
"""
import os
import re
_relative_path = "./Algorithm"
_README_TEMPLE = """
# LeetCode Solution
[Leetcode](https://leetcode-cn.com/problemset/all/) algorithm or database problemset.
Problem | Solution | Level
:---|:---:|:--:
%s
"""
_LINE_TEMPLE = "[%s %s](%s)|[%s](./Algorithm/%s)|%s"
_HyperLink_Pattern = r"((http|ftp|https)://)(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,4})*(/[a-zA-Z0-9\&%_\./-~-]*)?"
_Language_Pattern = r"lang=.*"
_Level_pattern = r"(Easy|Medium|Hard)"
def generate():
rows = []
for file_name in os.listdir(_relative_path):
segments = file_name.split(".")
index = segments[0]
title = segments[1]
with open(os.path.join(_relative_path, file_name), "r", encoding='utf-8') as f:
content = f.read()
result = re.search(_HyperLink_Pattern, content)
hyperlink = content[result.start():result.end()]
result = re.search(_Language_Pattern, content)
language = content[result.start():result.end()][5:]
result = re.search(_Level_pattern, content)
level = content[result.start():result.end()]
rows.append(_LINE_TEMPLE %
(index, title, hyperlink, language, file_name, level))
sorted_rows = sorted(rows, key=lambda x: int(x.split(" ")[0][1:]))
readme_content = _README_TEMPLE % ("\n".join(sorted_rows))
with open("./README.md", 'w', encoding='utf-8') as f:
f.write(readme_content)
if __name__ == "__main__":
generate()