Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 8348a9e

Browse files
committed
add a scrpit
1 parent 1d6bee7 commit 8348a9e

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed

src/lib.rs

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,19 @@ mod tests {
2323
let resp = get_question_msg("maximum-points-you-can-obtain-from-cards");
2424
write_to_readme(resp);
2525
}
26+
27+
#[test]
28+
fn test_get_question_no() {
29+
assert_eq!(374, get_question_no("- 374:猜数字大小"));
30+
assert_eq!(367, get_question_no("- 367:有效的完全平方数"));
31+
}
2632
}
2733

2834
extern crate reqwest;
2935

3036
use git2::{Repository, StatusOptions};
3137
use serde::Deserialize;
32-
use std::fs::{self, File, ReadDir};
33-
use std::ops::Deref;
38+
use std::fs::{self, File};
3439
use std::io::Write;
3540

3641

@@ -96,16 +101,16 @@ pub fn write_to_readme(question_info: Resp) {
96101
通过rust刷leetcode题目。
97102
通过刷leetcode题目学习rust。\n\n");
98103
write_string.push_str(format!("当前已刷:{}\n\n", get_question_num()).as_str());
99-
write_string.push_str("### 题目");
104+
write_string.push_str("### 题目\n");
100105

101106
let mut f = File::create("1.md").unwrap();
102-
f.write(readme.as_bytes());
103107

104108
let mut index = 0usize;
105109
let split = readme.split("\n").into_iter().collect::<Vec<&str>>();
106110
let mut flag = false;
107-
108-
loop {
111+
let mut flag1 = false;
112+
let no = question_info.data.question.question_id.parse::<i32>().unwrap();
113+
while index + 3 < split.len() {
109114
if !flag {
110115
if split[index] == "### 题目" {
111116
flag = true;
@@ -114,14 +119,28 @@ pub fn write_to_readme(question_info: Resp) {
114119
continue;
115120
}
116121

122+
let i1 = get_question_no(split[index]);
123+
if !flag1 && i1 > no {
124+
flag1 = true;
125+
write_string.push_str(format!("- {}:{}\n", no, question_info.data.question.translated_title).as_str());
126+
write_string.push_str(format!(" - [src](https://github.com/rustors/leetcode/blob/main/src/bin/{}.rs)\n", question_info.data.question.title_slug).as_str());
127+
write_string.push_str(format!(" - [leetcode](https://leetcode-cn.com/problems/{}/)\n", question_info.data.question.title_slug).as_str());
128+
}
117129

118-
119-
130+
write_string.push_str(format!("{}\n{}\n{}\n", split[index], split[index + 1], split[index + 2]).as_str());
120131
index += 3;
121132
}
133+
134+
if !flag1 {
135+
write_string.push_str(format!("- {}:{}\n", no, question_info.data.question.translated_title).as_str());
136+
write_string.push_str(format!(" - [src](https://github.com/rustors/leetcode/blob/main/src/bin/{}.rs)\n", question_info.data.question.title_slug).as_str());
137+
write_string.push_str(format!(" - [leetcode](https://leetcode-cn.com/problems/{}/)\n", question_info.data.question.title_slug).as_str());
138+
}
139+
140+
let _ = f.write(write_string.as_bytes());
122141
}
123142

124-
/// 获取题目数
143+
/// 获取题目总数
125144
fn get_question_num() -> usize {
126145
let dir = fs::read_dir("src/bin/").unwrap();
127146

@@ -135,3 +154,15 @@ fn get_question_num() -> usize {
135154
}
136155
}).count()
137156
}
157+
158+
/// 获取题目的编号
159+
fn get_question_no(s: &str) -> i32 {
160+
s.split(":")
161+
.into_iter()
162+
.collect::<Vec<&str>>()[0]
163+
.trim_end_matches(':')
164+
.trim_start_matches('-')
165+
.trim_start()
166+
.parse::<i32>()
167+
.unwrap()
168+
}

src/main.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
///! 根据src/bin中新加的题目生成相应的markdown文件
2+
use leetcode;
3+
use leetcode::write_to_readme;
4+
5+
fn main() {
6+
// 获取新加的文件
7+
let files = leetcode::get_new_file_in_bin();
8+
9+
for i in files.iter() {
10+
let res = leetcode::get_question_msg(i);
11+
write_to_readme(res);
12+
}
13+
}

0 commit comments

Comments
 (0)