diff --git a/rust/tests/edit_distance_test/Cargo.lock b/rust/tests/edit_distance_test/Cargo.lock new file mode 100644 index 00000000..66ca318d --- /dev/null +++ b/rust/tests/edit_distance_test/Cargo.lock @@ -0,0 +1,12 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "edit_distance" +version = "0.1.0" + +[[package]] +name = "edit_distance_test" +version = "0.1.0" +dependencies = [ + "edit_distance", +] diff --git a/rust/tests/edit_distance_test/Cargo.toml b/rust/tests/edit_distance_test/Cargo.toml new file mode 100644 index 00000000..4d034666 --- /dev/null +++ b/rust/tests/edit_distance_test/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "edit_distance_test" +version = "0.1.0" +authors = ["Augusto "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +edit_distance = { path = "../../../../rust-piscine-solutions/edit_distance"} \ No newline at end of file diff --git a/rust/tests/edit_distance_test/src/main.rs b/rust/tests/edit_distance_test/src/main.rs new file mode 100644 index 00000000..2ba866c3 --- /dev/null +++ b/rust/tests/edit_distance_test/src/main.rs @@ -0,0 +1,70 @@ +// Create a function call `edit_distance` that calculates the minimum +// number of changes (insertion, deletions and substitutions) that +// need to be made to a string `source` to arrive to another `target` +// string + +// For more information and examples https://en.wikipedia.org/wiki/Edit_distance + +// pub fn edit_distance(source: &str, target: &str) -> usize { +// let src = source.chars().collect::>(); +// let tar = target.chars().collect::>(); +// let source_len = src.len() + 1; +// let target_len = tar.len() + 1; + +// if source_len == 0 { +// return target_len; +// } +// if target_len == 0 { +// return source_len; +// } + +// let mut matrix = vec![vec![0; source_len]; target_len]; + +// for i in 1..target_len { +// matrix[i][0] = i +// } +// for j in 1..source_len { +// matrix[0][j] = j +// } + +// for i in 1..target_len { +// for j in 1..source_len { +// let x = if src[j - 1] == tar[i - 1] { +// matrix[i - 1][j - 1] +// } else { +// 1 + std::cmp::min( +// std::cmp::min(matrix[i][j - 1], matrix[i - 1][j]), +// matrix[i - 1][j - 1], +// ) +// }; +// matrix[i][j] = x; +// } +// } +// matrix[target_len - 1][source_len - 1] +// } + +use edit_distance::edit_distance; + +#[allow(dead_code)] +fn main() { + let source = "alignment"; + let target = "assignment"; + println!( + "It's necessary to make {} change(s) to {}, to get {}", + edit_distance(source, target), + source, + target + ); +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn test_distance() { + assert_eq!(edit_distance("gumbo", "gambol"), 2); + assert_eq!(edit_distance("kitten", "sitting"), 3); + assert_eq!(edit_distance("rosettacode", "raisethysword"), 8); + } +} diff --git a/subjects/edit_distance/README.md b/subjects/edit_distance/README.md new file mode 100644 index 00000000..91195bb6 --- /dev/null +++ b/subjects/edit_distance/README.md @@ -0,0 +1,41 @@ +## edit_distance + +### Instructions + +Create a function call `edit_distance` that calculates the minimum number of changes (insertion, deletions and substitutions) that need to be made to a string `source` to arrive to another `target` string + +### Expected Function + +```rust +pub fn edit_distance(source: &str, target: &str) -> usize { +} +``` + +### Notions + +For more information and examples https://en.wikipedia.org/wiki/Edit_distance + +### Usage + +Here is a program to test your function. + +```rust +fn main() { + let source = "alignment"; + let target = "assignment"; + println!( + "It's necessary to make {} change(s) to {}, to get {}", + edit_distance(source, target), + source, + target + ); +} +``` + +And its output + +```console +student@ubuntu:~/[[ROOT]]/test$ cargo run +It's necessary to make 2 change(s) to alignment, to get assignment +student@ubuntu:~/[[ROOT]]/test$ +```