Skip to content

Commit 2faabcc

Browse files
committed
Merge remote-tracking branch 'upstream/master' into separate-problems-answers
2 parents 695f53a + 7cd46a7 commit 2faabcc

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

src/fetcher.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
extern crate reqwest;
22
extern crate serde_json;
33

4+
use serde_json::Value;
45
use std::fmt::{Display, Error, Formatter};
56

67
const PROBLEMS_URL: &str = "https://leetcode.com/api/problems/algorithms/";
@@ -43,6 +44,10 @@ pub fn get_problem(frontend_question_id: u32) -> Option<Problem> {
4344
sample_test_case: resp.data.question.sample_test_case,
4445
difficulty: problem.difficulty.to_string(),
4546
question_id: problem.stat.frontend_question_id,
47+
return_type: {
48+
let v: Value = serde_json::from_str(&resp.data.question.meta_data).unwrap();
49+
v["return"]["type"].to_string().replace("\"", "")
50+
},
4651
});
4752
}
4853
}
@@ -64,6 +69,7 @@ pub struct Problem {
6469
pub sample_test_case: String,
6570
pub difficulty: String,
6671
pub question_id: u32,
72+
pub return_type: String,
6773
}
6874

6975
#[derive(Serialize, Deserialize)]

src/main.rs

+46-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,10 @@ fn main() {
121121
let source = template
122122
.replace("__PROBLEM_TITLE__", &problem.title)
123123
.replace("__PROBLEM_DESC__", &build_desc(&problem.content))
124-
.replace("__PROBLEM_DEFAULT_CODE__", &code.default_code)
124+
.replace(
125+
"__PROBLEM_DEFAULT_CODE__",
126+
&insert_return_in_code(&problem.return_type, &code.default_code),
127+
)
125128
.replace("__PROBLEM_ID__", &format!("{}", problem.question_id))
126129
.replace("__EXTRA_USE__", &parse_extra_use(&code.default_code));
127130

@@ -186,6 +189,48 @@ fn parse_extra_use(code: &str) -> String {
186189
extra_use_line
187190
}
188191

192+
fn insert_return_in_code(return_type: &str, code: &str) -> String {
193+
let re = Regex::new(r"\{[ \n]+}").unwrap();
194+
match return_type {
195+
"ListNode" => re
196+
.replace(&code, "{\n Some(Box::new(ListNode::new(0)))\n }")
197+
.to_string(),
198+
"ListNode[]" => re.replace(&code, "{\n vec![]\n }").to_string(),
199+
"TreeNode" => re
200+
.replace(
201+
&code,
202+
"{\n Some(Rc::new(RefCell::new(TreeNode::new(0))))\n }",
203+
)
204+
.to_string(),
205+
"boolean" => re.replace(&code, "{\n false\n }").to_string(),
206+
"character" => re.replace(&code, "{\n '0'\n }").to_string(),
207+
"character[][]" => re.replace(&code, "{\n vec![]\n }").to_string(),
208+
"double" => re.replace(&code, "{\n 0f64\n }").to_string(),
209+
"double[]" => re.replace(&code, "{\n vec![]\n }").to_string(),
210+
"int[]" => re.replace(&code, "{\n vec![]\n }").to_string(),
211+
"integer" => re.replace(&code, "{\n 0\n }").to_string(),
212+
"integer[]" => re.replace(&code, "{\n vec![]\n }").to_string(),
213+
"integer[][]" => re.replace(&code, "{\n vec![]\n }").to_string(),
214+
"list<String>" => re.replace(&code, "{\n vec![]\n }").to_string(),
215+
"list<TreeNode>" => re.replace(&code, "{\n vec![]\n }").to_string(),
216+
"list<boolean>" => re.replace(&code, "{\n vec![]\n }").to_string(),
217+
"list<double>" => re.replace(&code, "{\n vec![]\n }").to_string(),
218+
"list<integer>" => re.replace(&code, "{\n vec![]\n }").to_string(),
219+
"list<list<integer>>" => re.replace(&code, "{\n vec![]\n }").to_string(),
220+
"list<list<string>>" => re.replace(&code, "{\n vec![]\n }").to_string(),
221+
"list<string>" => re.replace(&code, "{\n vec![]\n }").to_string(),
222+
"null" => code.to_string(),
223+
"string" => re
224+
.replace(&code, "{\n String::new()\n }")
225+
.to_string(),
226+
"string[]" => re.replace(&code, "{\n vec![]\n }").to_string(),
227+
"void" => code.to_string(),
228+
"NestedInteger" => code.to_string(),
229+
"Node" => code.to_string(),
230+
_ => code.to_string(),
231+
}
232+
}
233+
189234
fn build_desc(content: &str) -> String {
190235
// TODO: fix this shit
191236
content

0 commit comments

Comments
 (0)