Skip to content

Commit d54d4f8

Browse files
authored
Merge pull request MisterBooo#53 from nettee/template
用脚本生成题解框架
2 parents d297eb2 + dface28 commit d54d4f8

File tree

9 files changed

+145
-0
lines changed

9 files changed

+145
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Python files
2+
__pycache__/

anima.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env python3
2+
import fire
3+
4+
import anima
5+
6+
if __name__ == '__main__':
7+
fire.Fire(anima.Anima())

anima/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from anima.create import create_solution
2+
3+
4+
class Anima:
5+
"""
6+
LeetCode Animation Manager
7+
"""
8+
9+
def new(self, id: str, title: str):
10+
create_solution(id, title)

anima/base.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import os
2+
from pathlib import Path
3+
4+
5+
def get_project_path() -> Path:
6+
script_path = os.path.realpath(__file__)
7+
project_path = Path(script_path).parent.parent
8+
return project_path
9+
10+
11+
def get_md_template_path() -> Path:
12+
return get_project_path() / 'template' / 'template.md'

anima/create.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import shutil
2+
3+
from anima.base import get_project_path, get_md_template_path
4+
from anima.model import ProblemInfo, Solution
5+
6+
7+
def create_solution(problem_id: int, problem_title: str) -> None:
8+
problem = ProblemInfo(problem_id, problem_title)
9+
solution_dir = get_project_path() / problem.title_slug()
10+
11+
if solution_dir.exists():
12+
print(f'创建失败,文件夹 {solution_dir} 已存在')
13+
exit(1)
14+
solution_dir.mkdir()
15+
16+
solution = Solution.create(problem, solution_dir)
17+
18+
template = get_md_template_path()
19+
shutil.copy(template, solution.doc_path())
20+
21+
print(f'题解框架创建完毕,位于文件夹 {solution.path}')
22+

anima/model.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import re
2+
from dataclasses import dataclass
3+
from pathlib import Path
4+
5+
6+
@dataclass
7+
class ProblemInfo:
8+
id: int
9+
title: str
10+
11+
def title_slug(self):
12+
title_parts = re.split(r'\s+', self.title)
13+
return f'{self.id:04d}-' + '-'.join(title_parts)
14+
15+
16+
@dataclass
17+
class Solution:
18+
problem: ProblemInfo
19+
path: Path
20+
21+
@classmethod
22+
def create(cls, problem: ProblemInfo, path: Path):
23+
solution = Solution(problem, path)
24+
solution._create_dirs()
25+
return solution
26+
27+
def _create_dirs(self):
28+
self.animation_path().mkdir()
29+
self.article_path().mkdir()
30+
self.code_path().mkdir()
31+
(self.animation_path() / 'Animation.m4v').touch()
32+
(self.animation_path() / 'Animation.gif').touch()
33+
34+
def _path_to(self, s: str) -> Path:
35+
return self.path / s
36+
37+
def animation_path(self) -> Path:
38+
return self.path / 'Animation'
39+
40+
def article_path(self) -> Path:
41+
return self.path / 'Article'
42+
43+
def doc_path(self) -> Path:
44+
return self.article_path() / (self.problem.title_slug() + '.md')
45+
46+
def code_path(self) -> Path:
47+
return self.path / 'Code'

contributing.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
## LeetCode Animation 管理脚本 anima 使用说明
2+
3+
需要使用 Python 3.6+ 版本
4+
5+
安装依赖:
6+
7+
```
8+
pip install -r requirements.txt
9+
```
10+
11+
### 创建新题解
12+
13+
生成题解目录框架,以及 Markdown 文件模板。
14+
15+
```
16+
python anima.py new 1 'Two Sum'
17+
```
18+

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fire

template/template.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# LeetCode 图解 |
2+
3+
> 本文首发于公众号「图解面试算法」,是 [图解 LeetCode](<https://github.com/MisterBooo/LeetCodeAnimation>) 系列文章之一。
4+
>
5+
> 同步博客:https://www.algomooc.com
6+
7+
本题解作者:
8+
9+
## 题目描述
10+
11+
12+
13+
## 题目解析
14+
15+
16+
17+
## 动画理解
18+
19+
![](../Animation/Animation.gif)
20+
21+
## 参考代码
22+
23+
24+
25+
## 复杂度分析
26+

0 commit comments

Comments
 (0)