Skip to content

增加问题的默认返回值, 方便书写代码 #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Feb 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ serde = "1.0"
serde_json = "1.0"
serde_derive = "1.0"
rand = "0.6.5"
regex = "1.3.4"

[lib]
doctest = false
Expand Down
48 changes: 47 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ extern crate serde_json;

mod problem;

use regex::Regex;
use std::env;
use std::fs;
use std::io;
Expand Down Expand Up @@ -74,7 +75,10 @@ fn main() {
let source = template
.replace("__PROBLEM_TITLE__", &problem.title)
.replace("__PROBLEM_DESC__", &build_desc(&problem.content))
.replace("__PROBLEM_DEFAULT_CODE__", &code.default_code)
.replace(
"__PROBLEM_DEFAULT_CODE__",
&insert_return_in_code(&problem.return_type, &code.default_code),
)
.replace("__PROBLEM_ID__", &format!("{}", problem.question_id))
.replace("__EXTRA_USE__", &parse_extra_use(&code.default_code));

Expand Down Expand Up @@ -147,6 +151,48 @@ fn parse_extra_use(code: &str) -> String {
extra_use_line
}

fn insert_return_in_code(return_type: &str, code: &str) -> String {
let re = Regex::new(r"\{[ \n]+}").unwrap();
match return_type {
"ListNode" => re
.replace(&code, "{\n Some(Box::new(ListNode::new(0)))\n }")
.to_string(),
"ListNode[]" => re.replace(&code, "{\n vec![]\n }").to_string(),
"TreeNode" => re
.replace(
&code,
"{\n Some(Rc::new(RefCell::new(TreeNode::new(0))))\n }",
)
.to_string(),
"boolean" => re.replace(&code, "{\n false\n }").to_string(),
"character" => re.replace(&code, "{\n '0'\n }").to_string(),
"character[][]" => re.replace(&code, "{\n vec![]\n }").to_string(),
"double" => re.replace(&code, "{\n 0f64\n }").to_string(),
"double[]" => re.replace(&code, "{\n vec![]\n }").to_string(),
"int[]" => re.replace(&code, "{\n vec![]\n }").to_string(),
"integer" => re.replace(&code, "{\n 0\n }").to_string(),
"integer[]" => re.replace(&code, "{\n vec![]\n }").to_string(),
"integer[][]" => re.replace(&code, "{\n vec![]\n }").to_string(),
"list<String>" => re.replace(&code, "{\n vec![]\n }").to_string(),
"list<TreeNode>" => re.replace(&code, "{\n vec![]\n }").to_string(),
"list<boolean>" => re.replace(&code, "{\n vec![]\n }").to_string(),
"list<double>" => re.replace(&code, "{\n vec![]\n }").to_string(),
"list<integer>" => re.replace(&code, "{\n vec![]\n }").to_string(),
"list<list<integer>>" => re.replace(&code, "{\n vec![]\n }").to_string(),
"list<list<string>>" => re.replace(&code, "{\n vec![]\n }").to_string(),
"list<string>" => re.replace(&code, "{\n vec![]\n }").to_string(),
"null" => code.to_string(),
"string" => re
.replace(&code, "{\n String::new()\n }")
.to_string(),
"string[]" => re.replace(&code, "{\n vec![]\n }").to_string(),
"void" => code.to_string(),
"NestedInteger" => code.to_string(),
"Node" => code.to_string(),
_ => code.to_string(),
}
}

fn build_desc(content: &str) -> String {
// TODO: fix this shit
content
Expand Down
6 changes: 6 additions & 0 deletions src/problem.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
extern crate reqwest;
extern crate serde_json;

use serde_json::Value;
use std::fmt::{Display, Error, Formatter};

const PROBLEMS_URL: &str = "https://leetcode.com/api/problems/algorithms/";
Expand Down Expand Up @@ -43,6 +44,10 @@ pub fn get_problem(frontend_question_id: u32) -> Option<Problem> {
sample_test_case: resp.data.question.sample_test_case,
difficulty: problem.difficulty.to_string(),
question_id: problem.stat.frontend_question_id,
return_type: {
let v: Value = serde_json::from_str(&resp.data.question.meta_data).unwrap();
v["return"]["type"].to_string().replace("\"", "")
},
});
}
}
Expand All @@ -64,6 +69,7 @@ pub struct Problem {
pub sample_test_case: String,
pub difficulty: String,
pub question_id: u32,
pub return_type: String,
}

#[derive(Serialize, Deserialize)]
Expand Down