From bc6fb496f8cd0caacf806c80b599b72ff4137a79 Mon Sep 17 00:00:00 2001 From: songyzh Date: Fri, 7 Feb 2020 14:10:59 +0800 Subject: [PATCH 01/14] extract solving logic --- src/main.rs | 76 +++++++++++++++++++++++++++++------------------------ 1 file changed, 42 insertions(+), 34 deletions(-) diff --git a/src/main.rs b/src/main.rs index 35bb8363..68776113 100644 --- a/src/main.rs +++ b/src/main.rs @@ -48,6 +48,7 @@ fn main() { .as_str() .parse() .unwrap(); + deal_solving(&id); } else { id = id_arg .parse::() @@ -79,40 +80,6 @@ fn main() { problem.title_slug.replace("-", "_") ); let file_path = Path::new("./src/problem").join(format!("{}.rs", file_name)); - if is_solving { - // check problem/ existence - if !file_path.exists() { - panic!("problem does not exist"); - } - // check solution/ no existence - let solution_name = format!( - "s{:04}_{}", - problem.question_id, - problem.title_slug.replace("-", "_") - ); - let solution_path = Path::new("./src/solution").join(format!("{}.rs", solution_name)); - if solution_path.exists() { - panic!("solution exists"); - } - // rename/move file - fs::rename(file_path, solution_path).unwrap(); - // remove from problem/mod.rs - let mod_file = "./src/problem/mod.rs"; - let target_line = format!("mod {};", file_name); - let lines: Vec = io::BufReader::new(File::open(mod_file).unwrap()) - .lines() - .map(|x| x.unwrap()) - .filter(|x| *x != target_line) - .collect(); - fs::write(mod_file, lines.join("\n")); - // insert into solution/mod.rs - let mut lib_file = fs::OpenOptions::new() - .append(true) - .open("./src/solution/mod.rs") - .unwrap(); - writeln!(lib_file, "mod {};", solution_name); - break; - } if file_path.exists() { panic!("problem already initialized"); } @@ -265,3 +232,44 @@ fn build_desc(content: &str) -> String { .replace("\n\n", "\n") .replace("\n", "\n * ") } + +fn deal_solving(id: &u32) { + let problem = fetcher::get_problem(*id).unwrap(); + let file_name = format!( + "p{:04}_{}", + problem.question_id, + problem.title_slug.replace("-", "_") + ); + let file_path = Path::new("./src/problem").join(format!("{}.rs", file_name)); + // check problem/ existence + if !file_path.exists() { + panic!("problem does not exist"); + } + // check solution/ no existence + let solution_name = format!( + "s{:04}_{}", + problem.question_id, + problem.title_slug.replace("-", "_") + ); + let solution_path = Path::new("./src/solution").join(format!("{}.rs", solution_name)); + if solution_path.exists() { + panic!("solution exists"); + } + // rename/move file + fs::rename(file_path, solution_path).unwrap(); + // remove from problem/mod.rs + let mod_file = "./src/problem/mod.rs"; + let target_line = format!("mod {};", file_name); + let lines: Vec = io::BufReader::new(File::open(mod_file).unwrap()) + .lines() + .map(|x| x.unwrap()) + .filter(|x| *x != target_line) + .collect(); + fs::write(mod_file, lines.join("\n")); + // insert into solution/mod.rs + let mut lib_file = fs::OpenOptions::new() + .append(true) + .open("./src/solution/mod.rs") + .unwrap(); + writeln!(lib_file, "mod {};", solution_name); +} From b3d28ecd485350fe584308ca1a1584663bf94ed3 Mon Sep 17 00:00:00 2001 From: songyzh Date: Fri, 7 Feb 2020 14:16:02 +0800 Subject: [PATCH 02/14] fix --- src/main.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main.rs b/src/main.rs index 68776113..de4129e7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -49,6 +49,7 @@ fn main() { .parse() .unwrap(); deal_solving(&id); + break; } else { id = id_arg .parse::() From 77a6accae94c94f37ec2d7b95bcc21e46d7b34c5 Mon Sep 17 00:00:00 2001 From: songyzh Date: Fri, 7 Feb 2020 14:20:38 +0800 Subject: [PATCH 03/14] extract problem dealing logic --- src/main.rs | 80 ++++++++++++++++++++++++++++------------------------- 1 file changed, 42 insertions(+), 38 deletions(-) diff --git a/src/main.rs b/src/main.rs index de4129e7..677fb3d6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ extern crate serde_json; mod fetcher; +use crate::fetcher::{CodeDefinition, Problem}; use regex::Regex; use std::env; use std::fs; @@ -74,44 +75,7 @@ fn main() { continue; } let code = code.unwrap(); - - let file_name = format!( - "p{:04}_{}", - problem.question_id, - problem.title_slug.replace("-", "_") - ); - let file_path = Path::new("./src/problem").join(format!("{}.rs", file_name)); - if file_path.exists() { - panic!("problem already initialized"); - } - - let template = fs::read_to_string("./template.rs").unwrap(); - let source = template - .replace("__PROBLEM_TITLE__", &problem.title) - .replace("__PROBLEM_DESC__", &build_desc(&problem.content)) - .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)); - - let mut file = fs::OpenOptions::new() - .write(true) - .create(true) - .truncate(true) - .open(&file_path) - .unwrap(); - - file.write_all(source.as_bytes()).unwrap(); - drop(file); - - let mut lib_file = fs::OpenOptions::new() - .write(true) - .append(true) - .open("./src/problem/mod.rs") - .unwrap(); - writeln!(lib_file, "mod {};", file_name); + deal_problem(&problem, &code); break; } } @@ -274,3 +238,43 @@ fn deal_solving(id: &u32) { .unwrap(); writeln!(lib_file, "mod {};", solution_name); } + +fn deal_problem(problem: &Problem, code: &CodeDefinition) { + let file_name = format!( + "p{:04}_{}", + problem.question_id, + problem.title_slug.replace("-", "_") + ); + let file_path = Path::new("./src/problem").join(format!("{}.rs", file_name)); + if file_path.exists() { + panic!("problem already initialized"); + } + + let template = fs::read_to_string("./template.rs").unwrap(); + let source = template + .replace("__PROBLEM_TITLE__", &problem.title) + .replace("__PROBLEM_DESC__", &build_desc(&problem.content)) + .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)); + + let mut file = fs::OpenOptions::new() + .write(true) + .create(true) + .truncate(true) + .open(&file_path) + .unwrap(); + + file.write_all(source.as_bytes()).unwrap(); + drop(file); + + let mut lib_file = fs::OpenOptions::new() + .write(true) + .append(true) + .open("./src/problem/mod.rs") + .unwrap(); + writeln!(lib_file, "mod {};", file_name); +} From 0d9c9f98bdf6b78cd83b995ba430e44b20cbdb11 Mon Sep 17 00:00:00 2001 From: songyzh Date: Fri, 7 Feb 2020 14:51:13 +0800 Subject: [PATCH 04/14] make code work --- Cargo.lock | 486 +++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 + src/fetcher.rs | 50 ++++- src/main.rs | 41 ++++- 4 files changed, 574 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 69f4ab42..a282f353 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13,6 +13,11 @@ dependencies = [ "memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "anyhow" +version = "1.0.26" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "arrayvec" version = "0.4.11" @@ -59,6 +64,11 @@ name = "bitflags" version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "bumpalo" +version = "3.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "byteorder" version = "1.3.2" @@ -149,6 +159,14 @@ dependencies = [ "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "crossbeam-channel" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "crossbeam-utils 0.6.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "crossbeam-deque" version = "0.7.1" @@ -188,6 +206,35 @@ dependencies = [ "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "curl" +version = "0.4.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "curl-sys 0.4.25 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)", + "openssl-probe 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "openssl-sys 0.9.50 (registry+https://github.com/rust-lang/crates.io-index)", + "schannel 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)", + "socket2 0.3.11 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "curl-sys" +version = "0.4.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cc 1.0.45 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)", + "libnghttp2-sys 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "libz-sys 1.0.25 (registry+https://github.com/rust-lang/crates.io-index)", + "openssl-sys 0.9.50 (registry+https://github.com/rust-lang/crates.io-index)", + "pkg-config 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)", + "vcpkg 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "dtoa" version = "0.4.4" @@ -288,6 +335,48 @@ name = "futures" version = "0.1.29" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "futures" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "futures-channel 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-core 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-executor 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-io 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-sink 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-task 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-util 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "futures-channel" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "futures-core 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-sink 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "futures-channel-preview" +version = "0.3.0-alpha.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "futures-core-preview 0.3.0-alpha.19 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-sink-preview 0.3.0-alpha.19 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "futures-core" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "futures-core-preview" +version = "0.3.0-alpha.19" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "futures-cpupool" version = "0.1.8" @@ -297,6 +386,110 @@ dependencies = [ "num_cpus 1.10.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "futures-executor" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "futures-core 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-task 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-util 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", + "num_cpus 1.10.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "futures-executor-preview" +version = "0.3.0-alpha.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "futures-core-preview 0.3.0-alpha.19 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-util-preview 0.3.0-alpha.19 (registry+https://github.com/rust-lang/crates.io-index)", + "num_cpus 1.10.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "futures-io" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "futures-io-preview" +version = "0.3.0-alpha.19" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "futures-macro" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro-hack 0.5.11 (registry+https://github.com/rust-lang/crates.io-index)", + "proc-macro2 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "futures-preview" +version = "0.3.0-alpha.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "futures-channel-preview 0.3.0-alpha.19 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-core-preview 0.3.0-alpha.19 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-executor-preview 0.3.0-alpha.19 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-io-preview 0.3.0-alpha.19 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-sink-preview 0.3.0-alpha.19 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-util-preview 0.3.0-alpha.19 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "futures-sink" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "futures-sink-preview" +version = "0.3.0-alpha.19" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "futures-task" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "futures-util" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "futures-channel 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-core 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-io 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-macro 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-sink 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-task 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "pin-utils 0.1.0-alpha.4 (registry+https://github.com/rust-lang/crates.io-index)", + "proc-macro-hack 0.5.11 (registry+https://github.com/rust-lang/crates.io-index)", + "proc-macro-nested 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "futures-util-preview" +version = "0.3.0-alpha.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "futures 0.1.29 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-channel-preview 0.3.0-alpha.19 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-core-preview 0.3.0-alpha.19 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-io-preview 0.3.0-alpha.19 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-sink-preview 0.3.0-alpha.19 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "pin-utils 0.1.0-alpha.4 (registry+https://github.com/rust-lang/crates.io-index)", + "slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "getrandom" version = "0.1.12" @@ -324,6 +517,14 @@ dependencies = [ "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "heck" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "unicode-segmentation 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "http" version = "0.1.18" @@ -424,11 +625,38 @@ dependencies = [ "libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "isahc" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", + "crossbeam-channel 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", + "crossbeam-utils 0.6.6 (registry+https://github.com/rust-lang/crates.io-index)", + "curl 0.4.25 (registry+https://github.com/rust-lang/crates.io-index)", + "curl-sys 0.4.25 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-io-preview 0.3.0-alpha.19 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-util-preview 0.3.0-alpha.19 (registry+https://github.com/rust-lang/crates.io-index)", + "http 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", + "slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", + "sluice 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "itoa" version = "0.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "js-sys" +version = "0.3.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "wasm-bindgen 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "kernel32-sys" version = "0.2.2" @@ -447,12 +675,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index" name = "leetcode-rust" version = "0.1.0" dependencies = [ + "futures 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", "regex 1.3.4 (registry+https://github.com/rust-lang/crates.io-index)", "reqwest 0.9.21 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.101 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.101 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.41 (registry+https://github.com/rust-lang/crates.io-index)", + "surf 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -460,6 +690,26 @@ name = "libc" version = "0.2.62" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "libnghttp2-sys" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cc 1.0.45 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "libz-sys" +version = "1.0.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cc 1.0.45 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)", + "pkg-config 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)", + "vcpkg 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "lock_api" version = "0.3.1" @@ -576,6 +826,15 @@ name = "nodrop" version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "nom" +version = "4.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "num_cpus" version = "1.10.1" @@ -648,6 +907,11 @@ name = "percent-encoding" version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "pin-utils" +version = "0.1.0-alpha.4" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "pkg-config" version = "0.3.16" @@ -658,6 +922,21 @@ name = "ppv-lite86" version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "proc-macro-hack" +version = "0.5.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "proc-macro-nested" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "proc-macro2" version = "0.4.30" @@ -1008,16 +1287,53 @@ dependencies = [ "url 1.7.2 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "serde_urlencoded" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "dtoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)", + "itoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.101 (registry+https://github.com/rust-lang/crates.io-index)", + "url 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "slab" version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "sluice" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "futures-channel-preview 0.3.0-alpha.19 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-core-preview 0.3.0-alpha.19 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-io-preview 0.3.0-alpha.19 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "smallvec" version = "0.6.10" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "socket2" +version = "0.3.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_syscall 0.1.56 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "sourcefile" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "string" version = "0.2.1" @@ -1026,6 +1342,27 @@ dependencies = [ "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "surf" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "futures-preview 0.3.0-alpha.19 (registry+https://github.com/rust-lang/crates.io-index)", + "http 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)", + "isahc 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)", + "js-sys 0.3.35 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", + "mime 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", + "mime_guess 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.101 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.41 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_urlencoded 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", + "url 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "wasm-bindgen 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)", + "wasm-bindgen-futures 0.3.27 (registry+https://github.com/rust-lang/crates.io-index)", + "web-sys 0.3.35 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "syn" version = "0.15.44" @@ -1248,6 +1585,11 @@ dependencies = [ "smallvec 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "unicode-segmentation" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "unicode-xid" version = "0.1.0" @@ -1311,6 +1653,105 @@ name = "wasi" version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "wasm-bindgen" +version = "0.2.58" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", + "wasm-bindgen-macro 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.58" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bumpalo 3.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", + "proc-macro2 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", + "wasm-bindgen-shared 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "wasm-bindgen-futures" +version = "0.3.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", + "futures 0.1.29 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-channel-preview 0.3.0-alpha.19 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-util-preview 0.3.0-alpha.19 (registry+https://github.com/rust-lang/crates.io-index)", + "js-sys 0.3.35 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "wasm-bindgen 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)", + "web-sys 0.3.35 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.58" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "wasm-bindgen-macro-support 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.58" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", + "wasm-bindgen-backend 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)", + "wasm-bindgen-shared 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.58" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "wasm-bindgen-webidl" +version = "0.2.58" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "anyhow 1.0.26 (registry+https://github.com/rust-lang/crates.io-index)", + "heck 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", + "proc-macro2 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", + "wasm-bindgen-backend 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)", + "weedle 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "web-sys" +version = "0.3.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "anyhow 1.0.26 (registry+https://github.com/rust-lang/crates.io-index)", + "js-sys 0.3.35 (registry+https://github.com/rust-lang/crates.io-index)", + "sourcefile 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", + "wasm-bindgen 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)", + "wasm-bindgen-webidl 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "weedle" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "nom 4.2.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "winapi" version = "0.2.8" @@ -1360,12 +1801,14 @@ dependencies = [ [metadata] "checksum adler32 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "5d2e7343e7fc9de883d1b0341e0b13970f764c14101234857d2ddafa1cb1cac2" "checksum aho-corasick 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)" = "58fb5e95d83b38284460a5fda7d6470aa0b8844d283a0b614b8535e880800d2d" +"checksum anyhow 1.0.26 (registry+https://github.com/rust-lang/crates.io-index)" = "7825f6833612eb2414095684fcf6c635becf3ce97fe48cf6421321e93bfbd53c" "checksum arrayvec 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)" = "b8d73f9beda665eaa98ab9e4f7442bd4e7de6652587de55b2525e52e29c1b0ba" "checksum autocfg 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "b671c8fb71b457dd4ae18c4ba1e59aa81793daacc361d82fcd410cef0d491875" "checksum backtrace 0.3.38 (registry+https://github.com/rust-lang/crates.io-index)" = "690a62be8920ccf773ee00ef0968649b0e724cda8bd5b12286302b4ae955fdf5" "checksum backtrace-sys 0.1.31 (registry+https://github.com/rust-lang/crates.io-index)" = "82a830b4ef2d1124a711c71d263c5abdc710ef8e907bd508c88be475cebc422b" "checksum base64 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0b25d992356d2eb0ed82172f5248873db5560c4721f564b13cb5193bda5e668e" "checksum bitflags 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8a606a02debe2813760609f57a64a2ffd27d9fdf5b2f133eaca0b248dd92cdd2" +"checksum bumpalo 3.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "5fb8038c1ddc0a5f73787b130f4cc75151e96ed33e417fde765eb5a81e3532f4" "checksum byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a7c3dd8985a7111efc5c80b44e23ecdd8c007de8ade3b96595387e812b957cf5" "checksum bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)" = "206fdffcfa2df7cbe15601ef46c813fce0965eb3286db6b56c583b814b51c81c" "checksum c2-chacha 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7d64d04786e0f528460fc884753cf8dddcc466be308f6026f8e355c41a0e4101" @@ -1377,10 +1820,13 @@ dependencies = [ "checksum core-foundation 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "25b9e03f145fd4f2bf705e07b900cd41fc636598fe5dc452fd0db1441c3f496d" "checksum core-foundation-sys 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e7ca8a5221364ef15ce201e8ed2f609fc312682a8f4e0e3d4aa5879764e0fa3b" "checksum crc32fast 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ba125de2af0df55319f41944744ad91c71113bf74a4646efff39afe1f6842db1" +"checksum crossbeam-channel 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "c8ec7fcd21571dc78f96cc96243cab8d8f035247c3efd16c687be154c3fa9efa" "checksum crossbeam-deque 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b18cd2e169ad86297e6bc0ad9aa679aee9daa4f19e8163860faf7c164e4f5a71" "checksum crossbeam-epoch 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "fedcd6772e37f3da2a9af9bf12ebe046c0dfe657992377b4df982a2b54cd37a9" "checksum crossbeam-queue 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7c979cd6cfe72335896575c6b5688da489e420d36a27a0b9eb0c73db574b4a4b" "checksum crossbeam-utils 0.6.6 (registry+https://github.com/rust-lang/crates.io-index)" = "04973fa96e96579258a5091af6003abde64af786b860f18622b82e026cca60e6" +"checksum curl 0.4.25 (registry+https://github.com/rust-lang/crates.io-index)" = "06aa71e9208a54def20792d877bc663d6aae0732b9852e612c4a933177c31283" +"checksum curl-sys 0.4.25 (registry+https://github.com/rust-lang/crates.io-index)" = "0c38ca47d60b86d0cc9d42caa90a0885669c2abc9791f871c81f58cdf39e979b" "checksum dtoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "ea57b42383d091c85abcc2706240b94ab2a8fa1fc81c10ff23c4de06e2a90b5e" "checksum either 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "bb1f6b1ce1c140482ea30ddd3335fc0024ac7ee112895426e0a629a6c20adfe3" "checksum encoding_rs 0.8.20 (registry+https://github.com/rust-lang/crates.io-index)" = "87240518927716f79692c2ed85bfe6e98196d18c6401ec75355760233a7e12e9" @@ -1395,9 +1841,26 @@ dependencies = [ "checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82" "checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" "checksum futures 0.1.29 (registry+https://github.com/rust-lang/crates.io-index)" = "1b980f2816d6ee8673b6517b52cb0e808a180efc92e5c19d02cdda79066703ef" +"checksum futures 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "5c329ae8753502fb44ae4fc2b622fa2a94652c41e795143765ba0927f92ab780" +"checksum futures-channel 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "f0c77d04ce8edd9cb903932b608268b3fffec4163dc053b3b402bf47eac1f1a8" +"checksum futures-channel-preview 0.3.0-alpha.19 (registry+https://github.com/rust-lang/crates.io-index)" = "d5e5f4df964fa9c1c2f8bddeb5c3611631cacd93baf810fc8bb2fb4b495c263a" +"checksum futures-core 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "f25592f769825e89b92358db00d26f965761e094951ac44d3663ef25b7ac464a" +"checksum futures-core-preview 0.3.0-alpha.19 (registry+https://github.com/rust-lang/crates.io-index)" = "b35b6263fb1ef523c3056565fa67b1d16f0a8604ff12b11b08c25f28a734c60a" "checksum futures-cpupool 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "ab90cde24b3319636588d0c35fe03b1333857621051837ed769faefb4c2162e4" +"checksum futures-executor 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "f674f3e1bcb15b37284a90cedf55afdba482ab061c407a9c0ebbd0f3109741ba" +"checksum futures-executor-preview 0.3.0-alpha.19 (registry+https://github.com/rust-lang/crates.io-index)" = "75236e88bd9fe88e5e8bfcd175b665d0528fe03ca4c5207fabc028c8f9d93e98" +"checksum futures-io 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "a638959aa96152c7a4cddf50fcb1e3fede0583b27157c26e67d6f99904090dc6" +"checksum futures-io-preview 0.3.0-alpha.19 (registry+https://github.com/rust-lang/crates.io-index)" = "f4914ae450db1921a56c91bde97a27846287d062087d4a652efc09bb3a01ebda" +"checksum futures-macro 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "9a5081aa3de1f7542a794a397cde100ed903b0630152d0973479018fd85423a7" +"checksum futures-preview 0.3.0-alpha.19 (registry+https://github.com/rust-lang/crates.io-index)" = "3b1dce2a0267ada5c6ff75a8ba864b4e679a9e2aa44262af7a3b5516d530d76e" +"checksum futures-sink 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "3466821b4bc114d95b087b850a724c6f83115e929bc88f1fa98a3304a944c8a6" +"checksum futures-sink-preview 0.3.0-alpha.19 (registry+https://github.com/rust-lang/crates.io-index)" = "86f148ef6b69f75bb610d4f9a2336d4fc88c4b5b67129d1a340dd0fd362efeec" +"checksum futures-task 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "7b0a34e53cf6cdcd0178aa573aed466b646eb3db769570841fda0c7ede375a27" +"checksum futures-util 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "22766cf25d64306bedf0384da004d05c9974ab104fcc4528f1236181c18004c5" +"checksum futures-util-preview 0.3.0-alpha.19 (registry+https://github.com/rust-lang/crates.io-index)" = "5ce968633c17e5f97936bd2797b6e38fb56cf16a7422319f7ec2e30d3c470e8d" "checksum getrandom 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "473a1265acc8ff1e808cd0a1af8cee3c2ee5200916058a2ca113c29f2d903571" "checksum h2 0.1.26 (registry+https://github.com/rust-lang/crates.io-index)" = "a5b34c246847f938a410a03c5458c7fee2274436675e76d8b903c08efc29c462" +"checksum heck 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "20564e78d53d2bb135c343b3f47714a56af2061f1c928fdb541dc7b9fdd94205" "checksum http 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)" = "372bcb56f939e449117fb0869c2e8fd8753a8223d92a172c6e808cf123a5b6e4" "checksum http-body 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6741c859c1b2463a423a1dbce98d418e6c3c3fc720fb0d45528657320920292d" "checksum httparse 1.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "cd179ae861f0c2e53da70d892f5f3029f9594be0c41dc5269cd371691b1dc2f9" @@ -1407,10 +1870,14 @@ dependencies = [ "checksum idna 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "02e2673c30ee86b5b96a9cb52ad15718aa1f966f5ab9ad54a8b95d5ca33120a9" "checksum indexmap 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a61202fbe46c4a951e9404a720a0180bcf3212c750d735cb5c4ba4dc551299f3" "checksum iovec 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c9636900aa73ffed13cdbb199f17cd955670bb300927c8d25b517dfa136b6567" +"checksum isahc 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)" = "17b77027f12e53ae59a379f7074259d32eb10867e6183388020e922832d9c3fb" "checksum itoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "501266b7edd0174f8530248f87f99c88fbe60ca4ef3dd486835b8d8d53136f7f" +"checksum js-sys 0.3.35 (registry+https://github.com/rust-lang/crates.io-index)" = "7889c7c36282151f6bf465be4700359318aef36baa951462382eae49e9577cf9" "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" "checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" "checksum libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)" = "34fcd2c08d2f832f376f4173a231990fa5aef4e99fb569867318a227ef4c06ba" +"checksum libnghttp2-sys 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "02254d44f4435dd79e695f2c2b83cd06a47919adea30216ceaf0c57ca0a72463" +"checksum libz-sys 1.0.25 (registry+https://github.com/rust-lang/crates.io-index)" = "2eb5e43362e38e2bca2fd5f5134c4d4564a23a5c28e9b95411652021a8675ebe" "checksum lock_api 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f8912e782533a93a167888781b836336a6ca5da6175c05944c86cf28c31104dc" "checksum log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)" = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7" "checksum matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08" @@ -1424,6 +1891,7 @@ dependencies = [ "checksum native-tls 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "4b2df1a4c22fd44a62147fd8f13dd0f95c9d8ca7b2610299b2a2f9cf8964274e" "checksum net2 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)" = "42550d9fb7b6684a6d404d9fa7250c2eb2646df731d1c06afc06dcee9e1bcf88" "checksum nodrop 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)" = "2f9667ddcc6cc8a43afc9b7917599d7216aa09c463919ea32c59ed6cac8bc945" +"checksum nom 4.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2ad2a91a8e869eeb30b9cb3119ae87773a8f4ae617f41b1eb9c154b2905f7bd6" "checksum num_cpus 1.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "bcef43580c035376c0705c42792c294b66974abbfd2789b511784023f71f3273" "checksum openssl 0.10.25 (registry+https://github.com/rust-lang/crates.io-index)" = "2f372b2b53ce10fb823a337aaa674e3a7d072b957c6264d0f4ff0bd86e657449" "checksum openssl-probe 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "77af24da69f9d9341038eba93a073b1fdaaa1b788221b00a69bce9e762cb32de" @@ -1432,8 +1900,11 @@ dependencies = [ "checksum parking_lot_core 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b876b1b9e7ac6e1a74a6da34d25c42e17e8862aa409cbbbdcfc8d86c6f3bc62b" "checksum percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "31010dd2e1ac33d5b46a5b413495239882813e0369f8ed8a5e266f173602f831" "checksum percent-encoding 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" +"checksum pin-utils 0.1.0-alpha.4 (registry+https://github.com/rust-lang/crates.io-index)" = "5894c618ce612a3fa23881b152b608bafb8c56cfc22f434a3ba3120b40f7b587" "checksum pkg-config 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)" = "72d5370d90f49f70bd033c3d75e87fc529fbfff9d6f7cccef07d6170079d91ea" "checksum ppv-lite86 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e3cbf9f658cdb5000fcf6f362b8ea2ba154b9f146a61c7a20d647034c6b6561b" +"checksum proc-macro-hack 0.5.11 (registry+https://github.com/rust-lang/crates.io-index)" = "ecd45702f76d6d3c75a80564378ae228a85f0b59d2f3ed43c91b4a69eb2ebfc5" +"checksum proc-macro-nested 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "369a6ed065f249a159e06c45752c780bda2fb53c995718f9e484d08daa9eb42e" "checksum proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)" = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759" "checksum proc-macro2 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "90cf5f418035b98e655e9cdb225047638296b862b42411c4e45bb88d700f7fc0" "checksum publicsuffix 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "9bf259a81de2b2eb9850ec990ec78e6a25319715584fd7652b9b26f96fcb1510" @@ -1472,9 +1943,14 @@ dependencies = [ "checksum serde_derive 1.0.101 (registry+https://github.com/rust-lang/crates.io-index)" = "4b133a43a1ecd55d4086bd5b4dc6c1751c68b1bfbeba7a5040442022c7e7c02e" "checksum serde_json 1.0.41 (registry+https://github.com/rust-lang/crates.io-index)" = "2f72eb2a68a7dc3f9a691bfda9305a1c017a6215e5a4545c258500d2099a37c2" "checksum serde_urlencoded 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)" = "642dd69105886af2efd227f75a520ec9b44a820d65bc133a9131f7d229fd165a" +"checksum serde_urlencoded 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9ec5d77e2d4c73717816afac02670d5c4f534ea95ed430442cad02e7a6e32c97" "checksum slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" +"checksum sluice 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "0a7d06dfb3e8743bc19e6de8a302277471d08077d68946b307280496dc5a3531" "checksum smallvec 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)" = "ab606a9c5e214920bb66c458cd7be8ef094f813f20fe77a54cc7dbfff220d4b7" +"checksum socket2 0.3.11 (registry+https://github.com/rust-lang/crates.io-index)" = "e8b74de517221a2cb01a53349cf54182acdc31a074727d3079068448c0676d85" +"checksum sourcefile 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "4bf77cb82ba8453b42b6ae1d692e4cdc92f9a47beaf89a847c8be83f4e328ad3" "checksum string 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d24114bfcceb867ca7f71a0d3fe45d45619ec47a6fbfa98cb14e14250bfa5d6d" +"checksum surf 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "741a8008f8a833ef16f47df94a30754478fb2c2bf822b9c2e6f7f09203b97ace" "checksum syn 0.15.44 (registry+https://github.com/rust-lang/crates.io-index)" = "9ca4b3b69a77cbe1ffc9e198781b7acb0c7365a883670e8f1c1bc66fba79a5c5" "checksum syn 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "66850e97125af79138385e9b88339cbcd037e3f28ceab8c5ad98e64f0f1f80bf" "checksum synstructure 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)" = "02353edf96d6e4dc81aea2d8490a7e9db177bf8acb0e951c24940bf866cb313f" @@ -1496,6 +1972,7 @@ dependencies = [ "checksum unicase 2.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2e2e6bd1e59e56598518beb94fd6db628ded570326f0a98c679a304bd9f00150" "checksum unicode-bidi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "49f2bd0c6468a8230e1db229cff8029217cf623c767ea5d60bfbd42729ea54d5" "checksum unicode-normalization 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "141339a08b982d942be2ca06ff8b076563cbe223d1befd5450716790d44e2426" +"checksum unicode-segmentation 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e83e153d1053cbb5a118eeff7fd5be06ed99153f00dbcd8ae310c5fb2b22edc0" "checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" "checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" "checksum url 1.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dd4e7c0d531266369519a4aa4f399d748bd37043b00bde1e4ff1f60a120b355a" @@ -1505,6 +1982,15 @@ dependencies = [ "checksum version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd" "checksum want 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b6395efa4784b027708f7451087e647ec73cc74f5d9bc2e418404248d679a230" "checksum wasi 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b89c3ce4ce14bdc6fb6beaf9ec7928ca331de5df7e5ea278375642a2f478570d" +"checksum wasm-bindgen 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)" = "5205e9afdf42282b192e2310a5b463a6d1c1d774e30dc3c791ac37ab42d2616c" +"checksum wasm-bindgen-backend 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)" = "11cdb95816290b525b32587d76419facd99662a07e59d3cdb560488a819d9a45" +"checksum wasm-bindgen-futures 0.3.27 (registry+https://github.com/rust-lang/crates.io-index)" = "83420b37346c311b9ed822af41ec2e82839bfe99867ec6c54e2da43b7538771c" +"checksum wasm-bindgen-macro 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)" = "574094772ce6921576fb6f2e3f7497b8a76273b6db092be18fc48a082de09dc3" +"checksum wasm-bindgen-macro-support 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)" = "e85031354f25eaebe78bb7db1c3d86140312a911a106b2e29f9cc440ce3e7668" +"checksum wasm-bindgen-shared 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)" = "f5e7e61fc929f4c0dddb748b102ebf9f632e2b8d739f2016542b4de2965a9601" +"checksum wasm-bindgen-webidl 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)" = "ef012a0d93fc0432df126a8eaf547b2dce25a8ce9212e1d3cbeef5c11157975d" +"checksum web-sys 0.3.35 (registry+https://github.com/rust-lang/crates.io-index)" = "aaf97caf6aa8c2b1dac90faf0db529d9d63c93846cca4911856f78a83cebf53b" +"checksum weedle 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3bb43f70885151e629e2a19ce9e50bd730fd436cfd4b666894c9ce4de9141164" "checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" "checksum winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6" "checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" diff --git a/Cargo.toml b/Cargo.toml index f6b7c1c5..9fdbe995 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,8 @@ serde_json = "1.0" serde_derive = "1.0" rand = "0.6.5" regex = "1.3.4" +futures = { version = "0.3.3", features = ["thread-pool"] } +surf = "1.0.3" [lib] doctest = false diff --git a/src/fetcher.rs b/src/fetcher.rs index a60fb39e..85db1424 100644 --- a/src/fetcher.rs +++ b/src/fetcher.rs @@ -54,7 +54,49 @@ pub fn get_problem(frontend_question_id: u32) -> Option { None } -fn get_problems() -> Option { +pub async fn get_problem_async(problem_stat: StatWithStatus) -> Option { + if problem_stat.paid_only { + println!( + "Problem {} is paid-only", + &problem_stat.stat.frontend_question_id + ); + return None; + } + let resp = surf::post(GRAPHQL_URL).body_json(&Query::question_query( + problem_stat.stat.question_title_slug.as_ref().unwrap(), + )); + if resp.is_err() { + println!( + "Problem {} not initialized due to some error", + &problem_stat.stat.frontend_question_id + ); + return None; + } + let resp = resp.unwrap().recv_json().await; + if resp.is_err() { + println!( + "Problem {} not initialized due to some error", + &problem_stat.stat.frontend_question_id + ); + return None; + } + let resp: RawProblem = resp.unwrap(); + return Some(Problem { + title: problem_stat.stat.question_title.clone().unwrap(), + title_slug: problem_stat.stat.question_title_slug.clone().unwrap(), + code_definition: serde_json::from_str(&resp.data.question.code_definition).unwrap(), + content: resp.data.question.content, + sample_test_case: resp.data.question.sample_test_case, + difficulty: problem_stat.difficulty.to_string(), + question_id: problem_stat.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("\"", "") + }, + }); +} + +pub fn get_problems() -> Option { reqwest::get(PROBLEMS_URL).unwrap().json().unwrap() } @@ -121,18 +163,18 @@ struct Question { } #[derive(Debug, Serialize, Deserialize)] -struct Problems { +pub struct Problems { user_name: String, num_solved: u32, num_total: u32, ac_easy: u32, ac_medium: u32, ac_hard: u32, - stat_status_pairs: Vec, + pub stat_status_pairs: Vec, } #[derive(Debug, Serialize, Deserialize)] -struct StatWithStatus { +pub struct StatWithStatus { stat: Stat, difficulty: Difficulty, paid_only: bool, diff --git a/src/main.rs b/src/main.rs index 677fb3d6..1e78c3a0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,6 +14,12 @@ use std::io; use std::io::{BufRead, Write}; use std::path::Path; +use futures::executor::block_on; +use futures::executor::ThreadPool; +use futures::future::join_all; +use futures::stream::StreamExt; +use futures::task::SpawnExt; + /// main() helps to generate the submission template .rs fn main() { println!("Welcome to leetcode-rust system."); @@ -31,6 +37,7 @@ fn main() { let random_pattern = Regex::new(r"^random$").unwrap(); let solving_pattern = Regex::new(r"^solve (\d+)$").unwrap(); + let all_pattern = Regex::new(r"^all$").unwrap(); if random_pattern.is_match(id_arg) { println!("You select random mode."); @@ -51,6 +58,35 @@ fn main() { .unwrap(); deal_solving(&id); break; + } else if all_pattern.is_match(id_arg) { + // deal all problems + let pool = ThreadPool::new().unwrap(); + let mut tasks = vec![]; + let problems = fetcher::get_problems().unwrap(); + for stat in problems.stat_status_pairs { + tasks.push( + pool.spawn_with_handle(async move { + let problem = fetcher::get_problem_async(stat).await; + if problem.is_none() { + return; + } + let problem = problem.unwrap(); + let code = problem + .code_definition + .iter() + .find(|&d| d.value == "rust".to_string()); + if code.is_none() { + println!("Problem {} has no rust version.", id); + return; + } + let code = code.unwrap(); + async { deal_problem(&problem, &code) }.await + }) + .unwrap(), + ); + } + block_on(join_all(tasks)); + break; } else { id = id_arg .parse::() @@ -68,7 +104,10 @@ fn main() { id ) }); - let code = problem.code_definition.iter().find(|&d| d.value == "rust"); + let code = problem + .code_definition + .iter() + .find(|&d| d.value == "rust".to_string()); if code.is_none() { println!("Problem {} has no rust version.", &id); initialized_ids.push(problem.question_id); From ccd52d0c14329dc20bffef71a3736fd479e78d7f Mon Sep 17 00:00:00 2001 From: songyzh Date: Fri, 7 Feb 2020 15:31:36 +0800 Subject: [PATCH 05/14] write mod file separately --- src/fetcher.rs | 8 ++++---- src/main.rs | 41 ++++++++++++++++++++++++++++++----------- 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/src/fetcher.rs b/src/fetcher.rs index 85db1424..a15dd6b2 100644 --- a/src/fetcher.rs +++ b/src/fetcher.rs @@ -175,7 +175,7 @@ pub struct Problems { #[derive(Debug, Serialize, Deserialize)] pub struct StatWithStatus { - stat: Stat, + pub stat: Stat, difficulty: Difficulty, paid_only: bool, is_favor: bool, @@ -184,19 +184,19 @@ pub struct StatWithStatus { } #[derive(Debug, Serialize, Deserialize)] -struct Stat { +pub struct Stat { question_id: u32, #[serde(rename = "question__article__slug")] question_article_slug: Option, #[serde(rename = "question__title")] question_title: Option, #[serde(rename = "question__title_slug")] - question_title_slug: Option, + pub question_title_slug: Option, #[serde(rename = "question__hide")] question_hide: bool, total_acs: u32, total_submitted: u32, - frontend_question_id: u32, + pub frontend_question_id: u32, is_new_question: bool, } diff --git a/src/main.rs b/src/main.rs index 1e78c3a0..9b10a7a9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -63,10 +63,21 @@ fn main() { let pool = ThreadPool::new().unwrap(); let mut tasks = vec![]; let problems = fetcher::get_problems().unwrap(); - for stat in problems.stat_status_pairs { + let mut mod_file_addon = vec![]; + for problem_stat in problems.stat_status_pairs { + mod_file_addon.push(format!( + "mod p{:04}_{};", + problem_stat.stat.frontend_question_id, + problem_stat + .stat + .question_title_slug + .clone() + .unwrap() + .replace("-", "_") + )); tasks.push( pool.spawn_with_handle(async move { - let problem = fetcher::get_problem_async(stat).await; + let problem = fetcher::get_problem_async(problem_stat).await; if problem.is_none() { return; } @@ -80,12 +91,18 @@ fn main() { return; } let code = code.unwrap(); - async { deal_problem(&problem, &code) }.await + async { deal_problem(&problem, &code, false) }.await }) .unwrap(), ); } block_on(join_all(tasks)); + let mut lib_file = fs::OpenOptions::new() + .write(true) + .append(true) + .open("./src/problem/mod.rs") + .unwrap(); + writeln!(lib_file, "{}", mod_file_addon.join("\n")); break; } else { id = id_arg @@ -114,7 +131,7 @@ fn main() { continue; } let code = code.unwrap(); - deal_problem(&problem, &code); + deal_problem(&problem, &code, true); break; } } @@ -278,7 +295,7 @@ fn deal_solving(id: &u32) { writeln!(lib_file, "mod {};", solution_name); } -fn deal_problem(problem: &Problem, code: &CodeDefinition) { +fn deal_problem(problem: &Problem, code: &CodeDefinition, write_mod_file: bool) { let file_name = format!( "p{:04}_{}", problem.question_id, @@ -310,10 +327,12 @@ fn deal_problem(problem: &Problem, code: &CodeDefinition) { file.write_all(source.as_bytes()).unwrap(); drop(file); - let mut lib_file = fs::OpenOptions::new() - .write(true) - .append(true) - .open("./src/problem/mod.rs") - .unwrap(); - writeln!(lib_file, "mod {};", file_name); + if write_mod_file { + let mut lib_file = fs::OpenOptions::new() + .write(true) + .append(true) + .open("./src/problem/mod.rs") + .unwrap(); + writeln!(lib_file, "mod {};", file_name); + } } From 6bfeb47d69f4327107f93c27acc8b13e67c49fb6 Mon Sep 17 00:00:00 2001 From: songyzh Date: Fri, 7 Feb 2020 15:35:44 +0800 Subject: [PATCH 06/14] fix --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 9b10a7a9..755bcbd7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -87,7 +87,7 @@ fn main() { .iter() .find(|&d| d.value == "rust".to_string()); if code.is_none() { - println!("Problem {} has no rust version.", id); + println!("Problem {} has no rust version.", problem.question_id); return; } let code = code.unwrap(); From 88741beff4fb5e168fe856ca8212971b5f70f4a1 Mon Sep 17 00:00:00 2001 From: songyzh Date: Fri, 7 Feb 2020 15:44:38 +0800 Subject: [PATCH 07/14] use arc mutex --- src/fetcher.rs | 8 ++++---- src/main.rs | 24 ++++++++++++------------ 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/fetcher.rs b/src/fetcher.rs index a15dd6b2..85db1424 100644 --- a/src/fetcher.rs +++ b/src/fetcher.rs @@ -175,7 +175,7 @@ pub struct Problems { #[derive(Debug, Serialize, Deserialize)] pub struct StatWithStatus { - pub stat: Stat, + stat: Stat, difficulty: Difficulty, paid_only: bool, is_favor: bool, @@ -184,19 +184,19 @@ pub struct StatWithStatus { } #[derive(Debug, Serialize, Deserialize)] -pub struct Stat { +struct Stat { question_id: u32, #[serde(rename = "question__article__slug")] question_article_slug: Option, #[serde(rename = "question__title")] question_title: Option, #[serde(rename = "question__title_slug")] - pub question_title_slug: Option, + question_title_slug: Option, #[serde(rename = "question__hide")] question_hide: bool, total_acs: u32, total_submitted: u32, - pub frontend_question_id: u32, + frontend_question_id: u32, is_new_question: bool, } diff --git a/src/main.rs b/src/main.rs index 755bcbd7..fd9f434f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,6 +19,7 @@ use futures::executor::ThreadPool; use futures::future::join_all; use futures::stream::StreamExt; use futures::task::SpawnExt; +use std::sync::{Arc, Mutex}; /// main() helps to generate the submission template .rs fn main() { @@ -63,18 +64,9 @@ fn main() { let pool = ThreadPool::new().unwrap(); let mut tasks = vec![]; let problems = fetcher::get_problems().unwrap(); - let mut mod_file_addon = vec![]; + let mut mod_file_addon = Arc::new(Mutex::new(vec![])); for problem_stat in problems.stat_status_pairs { - mod_file_addon.push(format!( - "mod p{:04}_{};", - problem_stat.stat.frontend_question_id, - problem_stat - .stat - .question_title_slug - .clone() - .unwrap() - .replace("-", "_") - )); + let mod_file_addon = mod_file_addon.clone(); tasks.push( pool.spawn_with_handle(async move { let problem = fetcher::get_problem_async(problem_stat).await; @@ -90,6 +82,14 @@ fn main() { println!("Problem {} has no rust version.", problem.question_id); return; } + async { + mod_file_addon.lock().unwrap().push(format!( + "mod p{:04}_{};", + problem.question_id, + problem.title_slug.replace("-", "_") + )); + } + .await; let code = code.unwrap(); async { deal_problem(&problem, &code, false) }.await }) @@ -102,7 +102,7 @@ fn main() { .append(true) .open("./src/problem/mod.rs") .unwrap(); - writeln!(lib_file, "{}", mod_file_addon.join("\n")); + writeln!(lib_file, "{}", mod_file_addon.lock().unwrap().join("\n")); break; } else { id = id_arg From 29e4dd6adaac5ae9eef00839f6768d8f73bc0a84 Mon Sep 17 00:00:00 2001 From: songyzh Date: Fri, 7 Feb 2020 15:50:32 +0800 Subject: [PATCH 08/14] fix --- src/main.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index fd9f434f..60683184 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,10 +23,15 @@ use std::sync::{Arc, Mutex}; /// main() helps to generate the submission template .rs fn main() { - println!("Welcome to leetcode-rust system."); + println!("Welcome to leetcode-rust system.\n"); let mut initialized_ids = get_initialized_ids(); loop { - println!("Please enter a frontend problem id, or \"random\" to generate a random one, or \"solve $i\" to move problem to solution/"); + println!( + "Please enter a frontend problem id, \n\ + or \"random\" to generate a random one, \n\ + or \"solve $i\" to move problem to solution/, \n\ + or \"all\" to initialize all problems" + ); let mut is_random = false; let mut is_solving = false; let mut id: u32 = 0; From 4aa113b7db747d58cd85e40b3d9cfba6024d934d Mon Sep 17 00:00:00 2001 From: songyzh Date: Fri, 7 Feb 2020 16:12:07 +0800 Subject: [PATCH 09/14] fix --- src/fetcher.rs | 6 +++--- src/main.rs | 5 ++++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/fetcher.rs b/src/fetcher.rs index 85db1424..c356dee8 100644 --- a/src/fetcher.rs +++ b/src/fetcher.rs @@ -175,7 +175,7 @@ pub struct Problems { #[derive(Debug, Serialize, Deserialize)] pub struct StatWithStatus { - stat: Stat, + pub stat: Stat, difficulty: Difficulty, paid_only: bool, is_favor: bool, @@ -184,7 +184,7 @@ pub struct StatWithStatus { } #[derive(Debug, Serialize, Deserialize)] -struct Stat { +pub struct Stat { question_id: u32, #[serde(rename = "question__article__slug")] question_article_slug: Option, @@ -196,7 +196,7 @@ struct Stat { question_hide: bool, total_acs: u32, total_submitted: u32, - frontend_question_id: u32, + pub frontend_question_id: u32, is_new_question: bool, } diff --git a/src/main.rs b/src/main.rs index 60683184..5f763bdf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,7 +30,7 @@ fn main() { "Please enter a frontend problem id, \n\ or \"random\" to generate a random one, \n\ or \"solve $i\" to move problem to solution/, \n\ - or \"all\" to initialize all problems" + or \"all\" to initialize all problems \n" ); let mut is_random = false; let mut is_solving = false; @@ -71,6 +71,9 @@ fn main() { let problems = fetcher::get_problems().unwrap(); let mut mod_file_addon = Arc::new(Mutex::new(vec![])); for problem_stat in problems.stat_status_pairs { + if initialized_ids.contains(&problem_stat.stat.frontend_question_id) { + continue; + } let mod_file_addon = mod_file_addon.clone(); tasks.push( pool.spawn_with_handle(async move { From 34c2b0d77abcc433e044451b257e6b9e0f24bc92 Mon Sep 17 00:00:00 2001 From: songyzh Date: Fri, 7 Feb 2020 16:39:58 +0800 Subject: [PATCH 10/14] add comment --- src/main.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main.rs b/src/main.rs index 5f763bdf..ae16b34d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -90,6 +90,7 @@ fn main() { println!("Problem {} has no rust version.", problem.question_id); return; } + // not sure this can be async async { mod_file_addon.lock().unwrap().push(format!( "mod p{:04}_{};", @@ -99,6 +100,8 @@ fn main() { } .await; let code = code.unwrap(); + // not sure this can be async + // maybe should use async-std io async { deal_problem(&problem, &code, false) }.await }) .unwrap(), From c632067859cdaa95523c3dfc2e3b0ad5e32e6576 Mon Sep 17 00:00:00 2001 From: songyzh Date: Sun, 9 Feb 2020 17:02:49 +0800 Subject: [PATCH 11/14] solve 318, 319, 326, 344, 345 --- src/solution/mod.rs | 5 ++ .../s0318_maximum_product_of_word_lengths.rs | 64 ++++++++++++++++++ src/solution/s0319_bulb_switcher.rs | 43 ++++++++++++ src/solution/s0326_power_of_three.rs | 58 ++++++++++++++++ src/solution/s0344_reverse_string.rs | 62 +++++++++++++++++ .../s0345_reverse_vowels_of_a_string.rs | 66 +++++++++++++++++++ 6 files changed, 298 insertions(+) create mode 100644 src/solution/s0318_maximum_product_of_word_lengths.rs create mode 100644 src/solution/s0319_bulb_switcher.rs create mode 100644 src/solution/s0326_power_of_three.rs create mode 100644 src/solution/s0344_reverse_string.rs create mode 100644 src/solution/s0345_reverse_vowels_of_a_string.rs diff --git a/src/solution/mod.rs b/src/solution/mod.rs index 3b079b74..f47b270c 100644 --- a/src/solution/mod.rs +++ b/src/solution/mod.rs @@ -235,3 +235,8 @@ mod s0111_minimum_depth_of_binary_tree; mod s0092_reverse_linked_list_ii; mod s0303_range_sum_query_immutable; mod s0102_binary_tree_level_order_traversal; +mod s0318_maximum_product_of_word_lengths; +mod s0319_bulb_switcher; +mod s0326_power_of_three; +mod s0344_reverse_string; +mod s0345_reverse_vowels_of_a_string; diff --git a/src/solution/s0318_maximum_product_of_word_lengths.rs b/src/solution/s0318_maximum_product_of_word_lengths.rs new file mode 100644 index 00000000..c9ac8b46 --- /dev/null +++ b/src/solution/s0318_maximum_product_of_word_lengths.rs @@ -0,0 +1,64 @@ +/** + * [318] Maximum Product of Word Lengths + * + * Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume that each word will contain only lower case letters. If no such two words exist, return 0. + * + * Example 1: + * + * + * Input: ["abcw","baz","foo","bar","xtfn","abcdef"] + * Output: 16 + * Explanation: The two words can be "abcw", "xtfn". + * + * Example 2: + * + * + * Input: ["a","ab","abc","d","cd","bcd","abcd"] + * Output: 4 + * Explanation: The two words can be "ab", "cd". + * + * Example 3: + * + * + * Input: ["a","aa","aaa","aaaa"] + * Output: 0 + * Explanation: No such pair of words. + * + * + */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn max_product(words: Vec) -> i32 { + let mut ret = 0; + // use a int value to store word characters + // if a word contains 'a', then the resulting int is b'00000000000000000000000000000001' + // if it also contains 'b', then the resulting int is b'00000000000000000000000000000011' and so on + let mut values = vec![0; words.len()]; + for (i, word) in words.iter().enumerate() { + for c in word.as_bytes() { + values[i] |= 1 << (c - b'a'); + } + } + for i in 0..words.len() { + for j in i + 1..words.len() { + if values[i] & values[j] == 0 && words[i].len() * words[j].len() > ret { + ret = words[i].len() * words[j].len(); + } + } + } + ret as i32 + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_318() {} +} diff --git a/src/solution/s0319_bulb_switcher.rs b/src/solution/s0319_bulb_switcher.rs new file mode 100644 index 00000000..a5ad315f --- /dev/null +++ b/src/solution/s0319_bulb_switcher.rs @@ -0,0 +1,43 @@ +/** + * [319] Bulb Switcher + * + * There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every second bulb. On the third round, you toggle every third bulb (turning on if it's off or turning off if it's on). For the i-th round, you toggle every i bulb. For the n-th round, you only toggle the last bulb. Find how many bulbs are on after n rounds. + * + * Example: + * + * + * Input: 3 + * Output: 1 + * Explanation: + * At first, the three bulbs are [off, off, off]. + * After first round, the three bulbs are [on, on, on]. + * After second round, the three bulbs are [on, off, on]. + * After third round, the three bulbs are [on, off, off]. + * + * So you should return 1, because there is only one bulb is on. + * + * + */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn bulb_switch(n: i32) -> i32 { + // number i bulb is toggled when round divides i, e.g. + // bulb 12 is toggled in round 1, 2, 3, 4, 6, 12 + // bulb 12's divisor comes in pairs, for example (2, 6), (3, 4) + // except for squres, e.g. 9 => 1, 3, 9, so it will be on + (n as f64).sqrt() as i32 + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_319() {} +} diff --git a/src/solution/s0326_power_of_three.rs b/src/solution/s0326_power_of_three.rs new file mode 100644 index 00000000..6fa7467d --- /dev/null +++ b/src/solution/s0326_power_of_three.rs @@ -0,0 +1,58 @@ +/** + * [326] Power of Three + * + * Given an integer, write a function to determine if it is a power of three. + * + * Example 1: + * + * + * Input: 27 + * Output: true + * + * + * Example 2: + * + * + * Input: 0 + * Output: false + * + * Example 3: + * + * + * Input: 9 + * Output: true + * + * Example 4: + * + * + * Input: 45 + * Output: false + * + * Follow up:
+ * Could you do it without using any loop / recursion? + */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn is_power_of_three(n: i32) -> bool { + if n == 0 { + return false; + } + if n == 1 { + return true; + } + n % 3 == 0 && Self::is_power_of_three(n / 3) + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_326() {} +} diff --git a/src/solution/s0344_reverse_string.rs b/src/solution/s0344_reverse_string.rs new file mode 100644 index 00000000..a3cd9d8e --- /dev/null +++ b/src/solution/s0344_reverse_string.rs @@ -0,0 +1,62 @@ +/** + * [344] Reverse String + * + * Write a function that reverses a string. The input string is given as an array of characters char[]. + * + * Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. + * + * You may assume all the characters consist of printable ascii characters. + * + * + * + *
+ * Example 1: + * + * + * Input: ["h","e","l","l","o"] + * Output: ["o","l","l","e","h"] + * + * + *
+ * Example 2: + * + * + * Input: ["H","a","n","n","a","h"] + * Output: ["h","a","n","n","a","H"] + * + *
+ *
+ */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn reverse_string(s: &mut Vec) { + if s.is_empty() { + return; + } + let (mut left, mut right) = (0, s.len() - 1); + while left < right { + let tmp = s[left]; + s[left] = s[right]; + s[right] = tmp; + left += 1; + right -= 1; + } + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_344() { + let mut s = vec![]; + Solution::reverse_string(&mut s); + println!("{:?}", s); + } +} diff --git a/src/solution/s0345_reverse_vowels_of_a_string.rs b/src/solution/s0345_reverse_vowels_of_a_string.rs new file mode 100644 index 00000000..28b4690a --- /dev/null +++ b/src/solution/s0345_reverse_vowels_of_a_string.rs @@ -0,0 +1,66 @@ +/** + * [345] Reverse Vowels of a String + * + * Write a function that takes a string as input and reverse only the vowels of a string. + * + * Example 1: + * + * + * Input: "hello" + * Output: "holle" + * + * + *
+ * Example 2: + * + * + * Input: "leetcode" + * Output: "leotcede" + *
+ * + * Note:
+ * The vowels does not include the letter "y". + * + * + * + */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn reverse_vowels(s: String) -> String { + if s.is_empty() { + return s; + } + let vowels: Vec = "aeiouAEIOU".chars().collect(); + let mut chars: Vec = s.chars().collect(); + let (mut left, mut right) = (0, chars.len() - 1); + while left < right { + while !vowels.contains(&chars[left]) && left < right { + left += 1; + } + while !vowels.contains(&chars[right]) && left < right { + right -= 1; + } + if left < right { + let tmp = chars[left]; + chars[left] = chars[right]; + chars[right] = tmp; + left += 1; + right -= 1; + } + } + chars.into_iter().collect() + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_345() {} +} From cfd38d9175a52022523e91dbbe10c60d5345704e Mon Sep 17 00:00:00 2001 From: songyzh Date: Sun, 9 Feb 2020 19:55:48 +0800 Subject: [PATCH 12/14] solve 349, 350, 367, 371 --- src/solution/mod.rs | 4 ++ .../s0349_intersection_of_two_arrays.rs | 60 +++++++++++++++++ .../s0350_intersection_of_two_arrays_ii.rs | 67 +++++++++++++++++++ src/solution/s0367_valid_perfect_square.rs | 61 +++++++++++++++++ src/solution/s0371_sum_of_two_integers.rs | 57 ++++++++++++++++ 5 files changed, 249 insertions(+) create mode 100644 src/solution/s0349_intersection_of_two_arrays.rs create mode 100644 src/solution/s0350_intersection_of_two_arrays_ii.rs create mode 100644 src/solution/s0367_valid_perfect_square.rs create mode 100644 src/solution/s0371_sum_of_two_integers.rs diff --git a/src/solution/mod.rs b/src/solution/mod.rs index f47b270c..13d6eaf9 100644 --- a/src/solution/mod.rs +++ b/src/solution/mod.rs @@ -240,3 +240,7 @@ mod s0319_bulb_switcher; mod s0326_power_of_three; mod s0344_reverse_string; mod s0345_reverse_vowels_of_a_string; +mod s0349_intersection_of_two_arrays; +mod s0350_intersection_of_two_arrays_ii; +mod s0367_valid_perfect_square; +mod s0371_sum_of_two_integers; diff --git a/src/solution/s0349_intersection_of_two_arrays.rs b/src/solution/s0349_intersection_of_two_arrays.rs new file mode 100644 index 00000000..7f8063f5 --- /dev/null +++ b/src/solution/s0349_intersection_of_two_arrays.rs @@ -0,0 +1,60 @@ +use std::hash::Hash; + +/** + * [349] Intersection of Two Arrays + * + * Given two arrays, write a function to compute their intersection. + * + * Example 1: + * + * + * Input: nums1 = [1,2,2,1], nums2 = [2,2] + * Output: [2] + * + * + *
+ * Example 2: + * + * + * Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4] + * Output: [9,4] + *
+ * + * Note: + * + * + * Each element in the result must be unique. + * The result can be in any order. + * + * + * + * + */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn intersection(nums1: Vec, nums2: Vec) -> Vec { + use std::collections::HashSet; + let s1: HashSet = nums1.into_iter().collect(); + let s2: HashSet = nums2.into_iter().collect(); + let mut ret: HashSet = HashSet::new(); + for num1 in s1 { + if s2.contains(&num1) { + ret.insert(num1); + } + } + ret.into_iter().collect() + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_349() {} +} diff --git a/src/solution/s0350_intersection_of_two_arrays_ii.rs b/src/solution/s0350_intersection_of_two_arrays_ii.rs new file mode 100644 index 00000000..101916a3 --- /dev/null +++ b/src/solution/s0350_intersection_of_two_arrays_ii.rs @@ -0,0 +1,67 @@ +/** + * [350] Intersection of Two Arrays II + * + * Given two arrays, write a function to compute their intersection. + * + * Example 1: + * + * + * Input: nums1 = [1,2,2,1], nums2 = [2,2] + * Output: [2,2] + * + * + *
+ * Example 2: + * + * + * Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4] + * Output: [4,9] + *
+ * + * Note: + * + * + * Each element in the result should appear as many times as it shows in both arrays. + * The result can be in any order. + * + * + * Follow up: + * + * + * What if the given array is already sorted? How would you optimize your algorithm? + * What if nums1's size is small compared to nums2's size? Which algorithm is better? + * What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once? + * + * + */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn intersect(nums1: Vec, nums2: Vec) -> Vec { + let mut ret = vec![]; + let mut nums1 = nums1; + nums1.sort(); + let mut nums2 = nums2; + nums2.sort(); + + for num1 in nums1 { + if let Ok(index) = nums2.binary_search(&num1) { + ret.push(num1); + nums2.remove(index); + } + } + ret + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_350() {} +} diff --git a/src/solution/s0367_valid_perfect_square.rs b/src/solution/s0367_valid_perfect_square.rs new file mode 100644 index 00000000..e9f99ab7 --- /dev/null +++ b/src/solution/s0367_valid_perfect_square.rs @@ -0,0 +1,61 @@ +/** + * [367] Valid Perfect Square + * + * Given a positive integer num, write a function which returns True if num is a perfect square else False. + * + * Note: Do not use any built-in library function such as sqrt. + * + * Example 1: + * + *
+ * + * Input: 16 + * Output: true + * + * + *
+ * Example 2: + * + * + * Input: 14 + * Output: false + * + *
+ *
+ */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn is_perfect_square(num: i32) -> bool { + if num == 0 || num == 1 { + return true; + } + // binary search + let (mut left, mut right) = (0, 100_000); + while left <= right { + let mid = left + (right - left) / 2; + if mid == num / mid { + return mid * mid == num; + } else if mid > num / mid { + right = mid - 1; + } else { + left = mid + 1; + } + } + false + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_367() { + println!("{}", Solution::is_perfect_square(1)); + } +} diff --git a/src/solution/s0371_sum_of_two_integers.rs b/src/solution/s0371_sum_of_two_integers.rs new file mode 100644 index 00000000..ef8f48b9 --- /dev/null +++ b/src/solution/s0371_sum_of_two_integers.rs @@ -0,0 +1,57 @@ +/** + * [371] Sum of Two Integers + * + * Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. + * + *
+ * Example 1: + * + * + * Input: a = 1, b = 2 + * Output: 3 + * + * + *
+ * Example 2: + * + * + * Input: a = -2, b = 3 + * Output: 1 + * + *
+ *
+ * + */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn get_sum(a: i32, b: i32) -> i32 { + // use `sum` to store direct sum without carriers + // use `carrier` to store carriers + let mut sum = a; + let mut carrier = b; + while sum != 0 && carrier != 0 { + let tmp_sum = sum; + sum = sum ^ carrier; + carrier = tmp_sum & carrier; + carrier <<= 1; + } + if sum == 0 { + carrier + } else { + sum + } + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_371() {} +} From 01222fe4060a6199ab2e5dc8f5dab318c1914dce Mon Sep 17 00:00:00 2001 From: songyzh Date: Mon, 10 Feb 2020 11:52:16 +0800 Subject: [PATCH 13/14] solve 383, 387 --- src/solution/mod.rs | 2 + src/solution/s0383_ransom_note.rs | 58 +++++++++++++++++++ ...0387_first_unique_character_in_a_string.rs | 48 +++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 src/solution/s0383_ransom_note.rs create mode 100644 src/solution/s0387_first_unique_character_in_a_string.rs diff --git a/src/solution/mod.rs b/src/solution/mod.rs index 3b079b74..df98dd66 100644 --- a/src/solution/mod.rs +++ b/src/solution/mod.rs @@ -235,3 +235,5 @@ mod s0111_minimum_depth_of_binary_tree; mod s0092_reverse_linked_list_ii; mod s0303_range_sum_query_immutable; mod s0102_binary_tree_level_order_traversal; +mod s0383_ransom_note; +mod s0387_first_unique_character_in_a_string; diff --git a/src/solution/s0383_ransom_note.rs b/src/solution/s0383_ransom_note.rs new file mode 100644 index 00000000..07632028 --- /dev/null +++ b/src/solution/s0383_ransom_note.rs @@ -0,0 +1,58 @@ +use std::collections::HashMap; + +/** + * [383] Ransom Note + * + * + * Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom + * note can be constructed from the magazines ; otherwise, it will return false. + * + * + * Each letter in the magazine string can only be used once in your ransom note. + * + * + * Note:
+ * You may assume that both strings contain only lowercase letters. + * + * + * + * canConstruct("a", "b") -> false + * canConstruct("aa", "ab") -> false + * canConstruct("aa", "aab") -> true + * + * + */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn can_construct(ransom_note: String, magazine: String) -> bool { + use std::collections::HashMap; + let mut map = HashMap::new(); + for c in magazine.chars() { + if map.contains_key(&c) { + map.insert(c, map.get(&c).unwrap() + 1); + } else { + map.insert(c, 1); + } + } + for c in ransom_note.chars() { + if !map.contains_key(&c) || map.get(&c).unwrap() < &1 { + return false; + } + map.insert(c, map.get(&c).unwrap() - 1); + } + true + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_383() {} +} diff --git a/src/solution/s0387_first_unique_character_in_a_string.rs b/src/solution/s0387_first_unique_character_in_a_string.rs new file mode 100644 index 00000000..e229c412 --- /dev/null +++ b/src/solution/s0387_first_unique_character_in_a_string.rs @@ -0,0 +1,48 @@ +/** + * [387] First Unique Character in a String + * + * + * Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. + * + * Examples: + * + * s = "leetcode" + * return 0. + * + * s = "loveleetcode", + * return 2. + * + * + * + * + * Note: You may assume the string contain only lowercase letters. + * + */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn first_uniq_char(s: String) -> i32 { + let mut lookup = [0; 256]; + for c in s.chars() { + lookup[c as usize] = lookup[c as usize] + 1; + } + for (i, c) in s.chars().enumerate() { + if lookup[c as usize] == 1 { + return i as i32; + } + } + -1 + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_387() {} +} From 094ba2aba8f03e04defe4ebe8c4595b486e39340 Mon Sep 17 00:00:00 2001 From: songyzh Date: Mon, 10 Feb 2020 17:26:38 +0800 Subject: [PATCH 14/14] solve 343, 409, 412, 414, 415, 434, 475 --- src/solution/mod.rs | 7 ++ src/solution/s0343_integer_break.rs | 72 ++++++++++++++++ src/solution/s0409_longest_palindrome.rs | 57 +++++++++++++ src/solution/s0412_fizz_buzz.rs | 63 ++++++++++++++ src/solution/s0414_third_maximum_number.rs | 81 ++++++++++++++++++ src/solution/s0415_add_strings.rs | 71 ++++++++++++++++ .../s0434_number_of_segments_in_a_string.rs | 42 ++++++++++ src/solution/s0475_heaters.rs | 82 +++++++++++++++++++ 8 files changed, 475 insertions(+) create mode 100644 src/solution/s0343_integer_break.rs create mode 100644 src/solution/s0409_longest_palindrome.rs create mode 100644 src/solution/s0412_fizz_buzz.rs create mode 100644 src/solution/s0414_third_maximum_number.rs create mode 100644 src/solution/s0415_add_strings.rs create mode 100644 src/solution/s0434_number_of_segments_in_a_string.rs create mode 100644 src/solution/s0475_heaters.rs diff --git a/src/solution/mod.rs b/src/solution/mod.rs index df98dd66..713b0962 100644 --- a/src/solution/mod.rs +++ b/src/solution/mod.rs @@ -237,3 +237,10 @@ mod s0303_range_sum_query_immutable; mod s0102_binary_tree_level_order_traversal; mod s0383_ransom_note; mod s0387_first_unique_character_in_a_string; +mod s0409_longest_palindrome; +mod s0414_third_maximum_number; +mod s0475_heaters; +mod s0343_integer_break; +mod s0434_number_of_segments_in_a_string; +mod s0412_fizz_buzz; +mod s0415_add_strings; diff --git a/src/solution/s0343_integer_break.rs b/src/solution/s0343_integer_break.rs new file mode 100644 index 00000000..6b59b3ee --- /dev/null +++ b/src/solution/s0343_integer_break.rs @@ -0,0 +1,72 @@ +/** + * [343] Integer Break + * + * Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get. + * + * Example 1: + * + *
+ * + * Input: 2 + * Output: 1 + * Explanation: 2 = 1 + 1, 1 × 1 = 1. + * + *
+ * Example 2: + * + * + * Input: 10 + * Output: 36 + * Explanation: 10 = 3 + 3 + 4, 3 × 3 × 4 = 36. + * + * Note: You may assume that n is not less than 2 and not larger than 58. + *
+ *
+ */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn integer_break(n: i32) -> i32 { + // dp + if n == 2 { + return 1; + } + if n == 3 { + return 2; + } + let mut dp = vec![0; (n + 1) as usize]; + // when used as factor, no need to break to addends + dp[2] = 2; + dp[3] = 3; + for i in 4..(n + 1) { + Self::helper(&mut dp, i); + } + dp[n as usize] + } + + fn helper(dp: &mut Vec, n: i32) { + let mut num1: usize = 2; + let mut num2: usize = n as usize - 2; + let mut res = 0; + while num1 <= num2 { + res = res.max(dp[num1] * dp[num2]); + num1 += 1; + num2 -= 1; + } + dp[n as usize] = res; + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_343() { + println!("{}", Solution::integer_break(10)); + } +} diff --git a/src/solution/s0409_longest_palindrome.rs b/src/solution/s0409_longest_palindrome.rs new file mode 100644 index 00000000..dc278265 --- /dev/null +++ b/src/solution/s0409_longest_palindrome.rs @@ -0,0 +1,57 @@ +/** + * [409] Longest Palindrome + * + * Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters. + * + * This is case sensitive, for example "Aa" is not considered a palindrome here. + * + * Note:
+ * Assume the length of given string will not exceed 1,010. + * + * + * Example: + * + * Input: + * "abccccdd" + * + * Output: + * 7 + * + * Explanation: + * One longest palindrome that can be built is "dccaccd", whose length is 7. + * + * + */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn longest_palindrome(s: String) -> i32 { + let mut lookup = vec![0; 256]; + for c in s.chars() { + lookup[c as usize] = lookup[c as usize] + 1; + } + let mut ret = 0; + let mut extra = 0; + for i in lookup { + if i % 2 == 1 { + extra = 1; + } + ret += i / 2 * 2; + } + ret + extra + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_409() { + println!("{}", Solution::longest_palindrome("abccccdd".to_string())); + } +} diff --git a/src/solution/s0412_fizz_buzz.rs b/src/solution/s0412_fizz_buzz.rs new file mode 100644 index 00000000..e68fa06f --- /dev/null +++ b/src/solution/s0412_fizz_buzz.rs @@ -0,0 +1,63 @@ +/** + * [412] Fizz Buzz + * + * Write a program that outputs the string representation of numbers from 1 to n. + * + * But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”. + * + * Example: + * + * n = 15, + * + * Return: + * [ + * "1", + * "2", + * "Fizz", + * "4", + * "Buzz", + * "Fizz", + * "7", + * "8", + * "Fizz", + * "Buzz", + * "11", + * "Fizz", + * "13", + * "14", + * "FizzBuzz" + * ] + * + * + */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn fizz_buzz(n: i32) -> Vec { + let mut ret = vec![]; + for i in 1..(n + 1) { + if i % 15 == 0 { + ret.push("FizzBuzz".to_string()); + } else if i % 3 == 0 { + ret.push("Fizz".to_string()); + } else if i % 5 == 0 { + ret.push("Buzz".to_string()); + } else { + ret.push(i.to_string()); + } + } + ret + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_412() {} +} diff --git a/src/solution/s0414_third_maximum_number.rs b/src/solution/s0414_third_maximum_number.rs new file mode 100644 index 00000000..4b4c7219 --- /dev/null +++ b/src/solution/s0414_third_maximum_number.rs @@ -0,0 +1,81 @@ +/** + * [414] Third Maximum Number + * + * Given a non-empty array of integers, return the third maximum number in this array. If it does not exist, return the maximum number. The time complexity must be in O(n). + * + * Example 1:
+ * + * Input: [3, 2, 1] + * + * Output: 1 + * + * Explanation: The third maximum is 1. + * + * + * + * Example 2:
+ * + * Input: [1, 2] + * + * Output: 2 + * + * Explanation: The third maximum does not exist, so the maximum (2) is returned instead. + * + * + * + * Example 3:
+ * + * Input: [2, 2, 3, 1] + * + * Output: 1 + * + * Explanation: Note that the third maximum here means the third maximum distinct number. + * Both numbers with value 2 are both considered as second maximum. + * + * + */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn third_max(nums: Vec) -> i32 { + // use min heap + let mut heap = vec![std::i64::MIN; 3]; + for num in nums { + let num = num as i64; + if heap.contains(&num) { + continue; + } + if num > heap[0] { + heap[0] = num; + if heap[0] > heap[1] { + let tmp = heap[0]; + heap[0] = heap[1]; + heap[1] = tmp; + } + if heap[0] > heap[2] { + let tmp = heap[0]; + heap[0] = heap[2]; + heap[2] = tmp; + } + } + } + if heap.contains(&std::i64::MIN) { + return heap[1].max(heap[2]) as i32; + } + heap[0] as i32 + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_414() { + println!("{}", Solution::third_max(vec![1, 2, 2, 4])) + } +} diff --git a/src/solution/s0415_add_strings.rs b/src/solution/s0415_add_strings.rs new file mode 100644 index 00000000..d3ad1386 --- /dev/null +++ b/src/solution/s0415_add_strings.rs @@ -0,0 +1,71 @@ +/** + * [415] Add Strings + * + * Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2. + * + * Note: + *
    + * The length of both num1 and num2 is < 5100. + * Both num1 and num2 contains only digits 0-9. + * Both num1 and num2 does not contain any leading zero. + * You must not use any built-in BigInteger library or convert the inputs to integer directly. + *
+ * + */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn add_strings(num1: String, num2: String) -> String { + let num1: Vec = num1.chars().collect(); + let num2: Vec = num2.chars().collect(); + let mut ret = String::new(); + let mut carrier = 0; + let mut index1 = (num1.len() - 1) as i32; + let mut index2 = (num2.len() - 1) as i32; + while index1 >= 0 && index2 >= 0 { + let sum = num1[index1 as usize].to_digit(10).unwrap() + + num2[index2 as usize].to_digit(10).unwrap() + + carrier; + let curr = sum % 10; + carrier = sum / 10; + ret = curr.to_string() + ret.as_str(); + index1 -= 1; + index2 -= 1; + } + while index1 >= 0 { + let sum = num1[index1 as usize].to_digit(10).unwrap() + carrier; + let curr = sum % 10; + carrier = sum / 10; + ret = curr.to_string() + ret.as_str(); + index1 -= 1; + } + while index2 >= 0 { + let sum = num2[index2 as usize].to_digit(10).unwrap() + carrier; + let curr = sum % 10; + carrier = sum / 10; + ret = curr.to_string() + ret.as_str(); + index2 -= 1; + } + if carrier == 1 { + ret = 1.to_string() + ret.as_str(); + } + ret + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_415() { + println!( + "{}", + Solution::add_strings("1".to_string(), "9999".to_string()) + ); + } +} diff --git a/src/solution/s0434_number_of_segments_in_a_string.rs b/src/solution/s0434_number_of_segments_in_a_string.rs new file mode 100644 index 00000000..7c9bb060 --- /dev/null +++ b/src/solution/s0434_number_of_segments_in_a_string.rs @@ -0,0 +1,42 @@ +/** + * [434] Number of Segments in a String + * + * Count the number of segments in a string, where a segment is defined to be a contiguous sequence of non-space characters. + * + * Please note that the string does not contain any non-printable characters. + * + * Example: + * + * Input: "Hello, my name is John" + * Output: 5 + * + * + */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn count_segments(s: String) -> i32 { + let mut ret = 0; + let mut prev_is_space = true; + for c in s.chars() { + let curr_is_space = c.is_whitespace(); + if prev_is_space && !curr_is_space { + ret += 1; + } + prev_is_space = curr_is_space; + } + ret + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_434() {} +} diff --git a/src/solution/s0475_heaters.rs b/src/solution/s0475_heaters.rs new file mode 100644 index 00000000..e661f1aa --- /dev/null +++ b/src/solution/s0475_heaters.rs @@ -0,0 +1,82 @@ +/** + * [475] Heaters + * + * Winter is coming! Your first job during the contest is to design a standard heater with fixed warm radius to warm all the houses. + * + * Now, you are given positions of houses and heaters on a horizontal line, find out minimum radius of heaters so that all houses could be covered by those heaters. + * + * So, your input will be the positions of houses and heaters seperately, and your expected output will be the minimum radius standard of heaters. + * + * Note: + * + *
    + * Numbers of houses and heaters you are given are non-negative and will not exceed 25000. + * Positions of houses and heaters you are given are non-negative and will not exceed 10^9. + * As long as a house is in the heaters' warm radius range, it can be warmed. + * All the heaters follow your radius standard and the warm radius will the same. + *
+ * + * + * + * Example 1: + * + * + * Input: [1,2,3],[2] + * Output: 1 + * Explanation: The only heater was placed in the position 2, and if we use the radius 1 standard, then all the houses can be warmed. + * + * + * + * + * Example 2: + * + * + * Input: [1,2,3,4],[1,4] + * Output: 1 + * Explanation: The two heater was placed in the position 1 and 4. We need to use radius 1 standard, then all the houses can be warmed. + * + * + * + * + */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn find_radius(houses: Vec, heaters: Vec) -> i32 { + use std::i32::MAX; + let mut houses = houses; + houses.sort(); + let mut heaters = heaters; + heaters.sort(); + let mut ret = 0; + for house in houses { + let index = heaters.binary_search(&house); + if let Err(index) = index { + let left = if index == 0 { + MAX + } else { + house - heaters[index - 1] + }; + let right = if index == heaters.len() { + MAX + } else { + heaters[index] - house + }; + ret = left.min(right).max(ret); + } + } + ret + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_475() {} +}