Skip to content

Commit

Permalink
Add readme and docstring
Browse files Browse the repository at this point in the history
  • Loading branch information
europeanplaice committed Feb 15, 2022
1 parent 71e62ec commit b6e076b
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "subset_sum"
version = "0.6.1"
version = "0.7.0"
edition = "2018"
authors = ["Tomohiro Endo <[email protected]>"]
description = "Solves subset sum problem and return a set of decomposed integers."
Expand Down
3 changes: 3 additions & 0 deletions key.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
3
5
7
56 changes: 53 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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`
Expand All @@ -35,19 +66,38 @@ 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);
}
```
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)]
]
```
4 changes: 4 additions & 0 deletions src/dp_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,10 @@ pub mod dp {
/// (VecDeque::from(vec![4, 3]), 7),
/// ],
/// ]);
///
/// let answer_unchanged: Vec<Vec<(VecDeque<i32>, 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<i32>, targets: &mut Vec<i32>) -> Vec<Vec<(VecDeque<i32>, i32)>>{
let mut group: Vec<(VecDeque<i32>, i32)> = Vec::new();
Expand Down
36 changes: 27 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> = env::args().collect();

let file = File::open(args[1].clone()).unwrap();
let lines = io::BufReader::new(file).lines();
let mut a: Vec<i32> = Vec::new();
for line in lines{
a.push(line.unwrap().trim().parse::<i32>().unwrap());
}
if a.iter().min().unwrap() >= &0 {
let b: Vec<u32> = a.iter().map(|x| *x as u32).collect();
println!("{:?}", dp_module::dp::find_subset_fast_only_positive(&b, args[2].parse::<usize>().unwrap()));
if Path::new(&args[2]).exists(){
let mut key: Vec<i32> = Vec::new();
for line in lines{
key.push(line.unwrap().trim().parse::<i32>().unwrap());
}
let file = File::open(args[2].clone()).unwrap();
let line2 = io::BufReader::new(file).lines();
let mut targets: Vec<i32> = Vec::new();
for line in line2{
targets.push(line.unwrap().trim().parse::<i32>().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::<i32>().unwrap());
println!("{:?}", result);
let mut a: Vec<i32> = Vec::new();
for line in lines{
a.push(line.unwrap().trim().parse::<i32>().unwrap());
}
if a.iter().min().unwrap() >= &0 {
let b: Vec<u32> = a.iter().map(|x| *x as u32).collect();
println!("{:?}", dp_module::dp::find_subset_fast_only_positive(&b, args[2].parse::<usize>().unwrap()));
} else {
let result = dp_module::dp::find_subset(&a, args[2].parse::<i32>().unwrap());
println!("{:?}", result);
}
}
}
6 changes: 6 additions & 0 deletions targets.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
1
5
-3
4
5
3

0 comments on commit b6e076b

Please sign in to comment.