From b6e076bcebb661cf24924f591018415eb5c35e5d Mon Sep 17 00:00:00 2001 From: europeanplaice Date: Tue, 15 Feb 2022 12:18:27 +0900 Subject: [PATCH] Add readme and docstring --- Cargo.lock | 2 +- Cargo.toml | 2 +- key.txt | 3 +++ readme.md | 56 +++++++++++++++++++++++++++++++++++++++++++++--- src/dp_module.rs | 4 ++++ src/main.rs | 36 +++++++++++++++++++++++-------- targets.txt | 6 ++++++ 7 files changed, 95 insertions(+), 14 deletions(-) create mode 100644 key.txt create mode 100644 targets.txt diff --git a/Cargo.lock b/Cargo.lock index e4c02c6..c0e6ec7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -451,7 +451,7 @@ dependencies = [ [[package]] name = "subset_sum" -version = "0.6.1" +version = "0.7.0" dependencies = [ "criterion", ] diff --git a/Cargo.toml b/Cargo.toml index 011e659..b3f6275 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "subset_sum" -version = "0.6.1" +version = "0.7.0" edition = "2018" authors = ["Tomohiro Endo "] description = "Solves subset sum problem and return a set of decomposed integers." diff --git a/key.txt b/key.txt new file mode 100644 index 0000000..869f9c7 --- /dev/null +++ b/key.txt @@ -0,0 +1,3 @@ +3 +5 +7 \ No newline at end of file diff --git a/readme.md b/readme.md index 4c53b7a..7c9a5b0 100644 --- a/readme.md +++ b/readme.md @@ -7,6 +7,8 @@ Binary files are provided on the [Releases](https://github.com/europeanplaice/su ## Usage +### Subset sum + First, you need to prepare a text file containing a set of integers like this ``` 1 @@ -19,13 +21,42 @@ and save it at any place. Second, call `subset_sum` with the path of the text file and the target sum. -### Example +#### Example + Call `subset_sum.exe num_set.txt 3` The executable's name `subset_sum.exe` would be different from your choice. Change this example along with your environment. In this example, the output is `[[1, 2], [2, -3, 4], [1, -3, 5]]` +### Sequence Matcher + +`key.txt` +``` +3 +5 +7 +``` + +`targets.txt` +``` +1 +5 +-3 +4 +5 +3 +``` + +Call `subset_sum.exe key.txt targets.txt` + +In this example, the output is +``` +[([3], 3), ([5], 5), ([1, -3, 4, 5], 7)] +[([3], 3), ([1, 4], 5), ([-3, 5, 5], 7)] +[([1, -3, 5], 3), ([5], 5), ([4, 3], 7)] +``` + ## Use in Rust `Cargo.toml` @@ -35,15 +66,16 @@ subset_sum = "(version)" ``` Example ``` -subset_sum = "0.5.0" +subset_sum = "0.7.0" ``` +### Subset sum `main.rs` ```rust use subset_sum::dp::find_subset; fn main() { - let result = find_subset(&vec![1, 2, 3, 4, 5, 6, 7, -8, 9, -10], -18); + let result = sequence_matcher(&mut vec![3, 5, 7], &mut vec![1, 5, -3, 4, 5, 3]); println!("{:?}", result); } ``` @@ -51,3 +83,21 @@ Output ``` [[-8, -10]] ``` +### Sequence Matcher +`main.rs` +```rust +use subset_sum::dp::sequence_matcher; + +fn main() { + let result = sequence_matcher(&mut vec![3, 5, 7], &mut vec![1, 5, -3, 4, 5, 3]); + println!("{:?}", result); +} +``` +Output +``` +[ + [([3], 3), ([5], 5), ([1, -3, 4, 5], 7)], + [([3], 3), ([1, 4], 5), ([-3, 5, 5], 7)], + [([1, -3, 5], 3), ([5], 5), ([4, 3], 7)] +] +``` diff --git a/src/dp_module.rs b/src/dp_module.rs index b2e8933..734f943 100644 --- a/src/dp_module.rs +++ b/src/dp_module.rs @@ -247,6 +247,10 @@ pub mod dp { /// (VecDeque::from(vec![4, 3]), 7), /// ], /// ]); + /// + /// let answer_unchanged: Vec, i32)>> = Vec::new(); + /// let answer = sequence_matcher(&mut vec![10, 20], &mut vec![9, 21]); + /// assert_eq!(answer, answer_unchanged); /// ``` pub fn sequence_matcher(key: &mut Vec, targets: &mut Vec) -> Vec, i32)>>{ let mut group: Vec<(VecDeque, i32)> = Vec::new(); diff --git a/src/main.rs b/src/main.rs index b185194..bc5a9a2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,21 +3,39 @@ use std::env; use std::fs::File; use std::io; use std::io::BufRead; +use std::path::Path; fn main() { let args: Vec = env::args().collect(); let file = File::open(args[1].clone()).unwrap(); let lines = io::BufReader::new(file).lines(); - let mut a: Vec = Vec::new(); - for line in lines{ - a.push(line.unwrap().trim().parse::().unwrap()); - } - if a.iter().min().unwrap() >= &0 { - let b: Vec = a.iter().map(|x| *x as u32).collect(); - println!("{:?}", dp_module::dp::find_subset_fast_only_positive(&b, args[2].parse::().unwrap())); + if Path::new(&args[2]).exists(){ + let mut key: Vec = Vec::new(); + for line in lines{ + key.push(line.unwrap().trim().parse::().unwrap()); + } + let file = File::open(args[2].clone()).unwrap(); + let line2 = io::BufReader::new(file).lines(); + let mut targets: Vec = Vec::new(); + for line in line2{ + targets.push(line.unwrap().trim().parse::().unwrap()); + } + let result = dp_module::dp::sequence_matcher(&mut key, &mut targets); + for elem in result{ + println!("{:?}", elem); + } } else { - let result = dp_module::dp::find_subset(&a, args[2].parse::().unwrap()); - println!("{:?}", result); + let mut a: Vec = Vec::new(); + for line in lines{ + a.push(line.unwrap().trim().parse::().unwrap()); + } + if a.iter().min().unwrap() >= &0 { + let b: Vec = a.iter().map(|x| *x as u32).collect(); + println!("{:?}", dp_module::dp::find_subset_fast_only_positive(&b, args[2].parse::().unwrap())); + } else { + let result = dp_module::dp::find_subset(&a, args[2].parse::().unwrap()); + println!("{:?}", result); + } } } \ No newline at end of file diff --git a/targets.txt b/targets.txt new file mode 100644 index 0000000..8918c92 --- /dev/null +++ b/targets.txt @@ -0,0 +1,6 @@ +1 +5 +-3 +4 +5 +3 \ No newline at end of file