aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKang-min Liu <gugod@gugod.org>2021-01-21 08:05:31 +0900
committerKang-min Liu <gugod@gugod.org>2021-01-21 08:05:31 +0900
commit2423900c332638e4081df3956e73479cfee65df8 (patch)
tree47b99093acd03e7a4713d8b26eee904c0dde71ac
parent34531261097ff884893213a0dd4dea05fa8533bd (diff)
downloadperlweeklychallenge-club-2423900c332638e4081df3956e73479cfee65df8.tar.gz
perlweeklychallenge-club-2423900c332638e4081df3956e73479cfee65df8.tar.bz2
perlweeklychallenge-club-2423900c332638e4081df3956e73479cfee65df8.zip
a solution to pwc 096.2 in rust
-rw-r--r--challenge-096/gugod/rust/ch-2.rs35
1 files changed, 35 insertions, 0 deletions
diff --git a/challenge-096/gugod/rust/ch-2.rs b/challenge-096/gugod/rust/ch-2.rs
new file mode 100644
index 0000000000..7a6761191a
--- /dev/null
+++ b/challenge-096/gugod/rust/ch-2.rs
@@ -0,0 +1,35 @@
+
+fn main() {
+ let examples: [[&str; 2]; 2] = [
+ ["kitten", "sitting"],
+ ["sunday", "monday"],
+ ];
+ for x in examples.iter() {
+ println!("Input: S1={}, S2={}", x[0], x[1]);
+ println!("Output: {}", edit_distance(x[0], x[1]));
+ }
+}
+
+fn edit_distance(a: &str, b: &str) -> u32 {
+ let chars_a = a.chars().collect();
+ let chars_b = b.chars().collect();
+ return lev( &chars_a, 0, &chars_b, 0);
+}
+
+fn lev (a: &Vec<char>, offset_a: usize, b: &Vec<char>, offset_b: usize) -> u32 {
+ if offset_a == a.len() {
+ return (b.len() - offset_b) as u32;
+ }
+ if offset_b == b.len() {
+ return (a.len() - offset_a) as u32;
+ }
+
+ let n3 = lev( a, offset_a+1, b, offset_b+1 );
+ if a[offset_a] == b[offset_b] {
+ return n3;
+ } else {
+ let n1 = lev( a, offset_a+1, b, offset_b );
+ let n2 = lev( a, offset_a, b, offset_b+1 );
+ return [n1, n2, n3].iter().min().unwrap() + 1_u32;
+ }
+}