diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-02-10 02:29:22 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-02-10 02:29:22 +0000 |
| commit | 764ae14f048a05b24215e12758b4ba2841afd53a (patch) | |
| tree | 85d49796b003628f7e67cb15c6d80a850ff2c4e3 | |
| parent | 61cdf9313a157f47033881a4ab8d0172edc0c6b9 (diff) | |
| parent | 42cdc5edff4eb8f5bcc2e1ca3cb95de6f3b43eb2 (diff) | |
| download | perlweeklychallenge-club-764ae14f048a05b24215e12758b4ba2841afd53a.tar.gz perlweeklychallenge-club-764ae14f048a05b24215e12758b4ba2841afd53a.tar.bz2 perlweeklychallenge-club-764ae14f048a05b24215e12758b4ba2841afd53a.zip | |
Merge pull request #3494 from xkr47/pwc099
099 rust, problem #1
| -rw-r--r-- | challenge-099/xkr47/README | 5 | ||||
| -rw-r--r-- | challenge-099/xkr47/rust/Cargo.lock | 5 | ||||
| -rw-r--r-- | challenge-099/xkr47/rust/Cargo.toml | 11 | ||||
| -rw-r--r-- | challenge-099/xkr47/rust/ch-1.rs | 90 |
4 files changed, 111 insertions, 0 deletions
diff --git a/challenge-099/xkr47/README b/challenge-099/xkr47/README index e4495cdc38..66625a1485 100644 --- a/challenge-099/xkr47/README +++ b/challenge-099/xkr47/README @@ -4,3 +4,8 @@ Install Rust from e.g. https://rustup.rs/ and run the Rust solutions like follow cd rust cargo run --release --bin ch-1 -- [<arguments>] + +And to run the unit tests (if any): + +cd rust +cargo test diff --git a/challenge-099/xkr47/rust/Cargo.lock b/challenge-099/xkr47/rust/Cargo.lock new file mode 100644 index 0000000000..9ad4f2381d --- /dev/null +++ b/challenge-099/xkr47/rust/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "pwd-099" +version = "0.1.0" diff --git a/challenge-099/xkr47/rust/Cargo.toml b/challenge-099/xkr47/rust/Cargo.toml new file mode 100644 index 0000000000..7372c0c975 --- /dev/null +++ b/challenge-099/xkr47/rust/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "pwd-099" +version = "0.1.0" +authors = ["Jonas Berlin <xkr47@outerspace.dyndns.org>"] +edition = "2018" + +[dependencies] + +[[bin]] +name = "ch-1" +path = "ch-1.rs" diff --git a/challenge-099/xkr47/rust/ch-1.rs b/challenge-099/xkr47/rust/ch-1.rs new file mode 100644 index 0000000000..50e83f14f4 --- /dev/null +++ b/challenge-099/xkr47/rust/ch-1.rs @@ -0,0 +1,90 @@ +#![allow(dead_code)] + +use std::env; + +fn main() { + let mut args = env::args().skip(1).take(2).collect::<Vec<_>>(); + let (text, pattern) = (args.remove(0), args.remove(0)); + + let is_match = do_match(&text, &pattern); + + println!("{}", if is_match { 1 } else { 0 }); +} + +fn do_match(text: &str, pattern: &str) -> bool { + fn inner(text: &[char], pattern: &[char]) -> bool { + if pattern.is_empty() { + text.is_empty() + } else { + match pattern[0] { + '*' => + inner(text, &pattern[1..]) || + (!text.is_empty() && inner(&text[1..], pattern)), + '?' => + !text.is_empty() && inner(&text[1..], &pattern[1..]), + ch => + text.get(0) == Some(&ch) && inner(&text[1..], &pattern[1..]) + } + } + } + inner(&text.chars().collect::<Vec<char>>(), &pattern.chars().collect::<Vec<char>>()) +} + +#[cfg(test)] +mod tests { + use crate::*; + + // from assignment + + #[test] + fn do_test1() { + assert_eq!(do_match("abcde", "a*e"), true); + } + + #[test] + fn do_test2() { + assert_eq!(do_match("abcde", "a*d"), false); + } + + #[test] + fn do_test3() { + assert_eq!(do_match("abcde", "?b*d"), false); + } + + #[test] + fn do_test4() { + assert_eq!(do_match("abcde", "a*c?e"), true); + } + + // additional + + #[test] + fn do_test10() { + assert_eq!(do_match("abc", "a*c?"), false); + } + + #[test] + fn do_test11() { + assert_eq!(do_match("abc", "a*c*"), true); + } + + #[test] + fn do_test12() { + assert_eq!(do_match("abc", "**"), true); + } + + #[test] + fn do_test13() { + assert_eq!(do_match("", "**"), true); + } + + #[test] + fn do_test14() { + assert_eq!(do_match("a", "*?"), true); + } + + #[test] + fn do_test15() { + assert_eq!(do_match("få", "f*"), true); + } +} |
