@@ -23,14 +23,19 @@ mod tests {
23
23
let resp = get_question_msg ( "maximum-points-you-can-obtain-from-cards" ) ;
24
24
write_to_readme ( resp) ;
25
25
}
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
+ }
26
32
}
27
33
28
34
extern crate reqwest;
29
35
30
36
use git2:: { Repository , StatusOptions } ;
31
37
use serde:: Deserialize ;
32
- use std:: fs:: { self , File , ReadDir } ;
33
- use std:: ops:: Deref ;
38
+ use std:: fs:: { self , File } ;
34
39
use std:: io:: Write ;
35
40
36
41
@@ -96,16 +101,16 @@ pub fn write_to_readme(question_info: Resp) {
96
101
通过rust刷leetcode题目。
97
102
通过刷leetcode题目学习rust。\n \n " ) ;
98
103
write_string. push_str ( format ! ( "当前已刷:{}\n \n " , get_question_num( ) ) . as_str ( ) ) ;
99
- write_string. push_str ( "### 题目" ) ;
104
+ write_string. push_str ( "### 题目\n " ) ;
100
105
101
106
let mut f = File :: create ( "1.md" ) . unwrap ( ) ;
102
- f. write ( readme. as_bytes ( ) ) ;
103
107
104
108
let mut index = 0usize ;
105
109
let split = readme. split ( "\n " ) . into_iter ( ) . collect :: < Vec < & str > > ( ) ;
106
110
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 ( ) {
109
114
if !flag {
110
115
if split[ index] == "### 题目" {
111
116
flag = true ;
@@ -114,14 +119,28 @@ pub fn write_to_readme(question_info: Resp) {
114
119
continue ;
115
120
}
116
121
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
+ }
117
129
118
-
119
-
130
+ write_string. push_str ( format ! ( "{}\n {}\n {}\n " , split[ index] , split[ index + 1 ] , split[ index + 2 ] ) . as_str ( ) ) ;
120
131
index += 3 ;
121
132
}
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 ( ) ) ;
122
141
}
123
142
124
- /// 获取题目数
143
+ /// 获取题目总数
125
144
fn get_question_num ( ) -> usize {
126
145
let dir = fs:: read_dir ( "src/bin/" ) . unwrap ( ) ;
127
146
@@ -135,3 +154,15 @@ fn get_question_num() -> usize {
135
154
}
136
155
} ) . count ( )
137
156
}
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
+ }
0 commit comments