diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-07-25 18:04:22 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-25 18:04:22 +0100 |
| commit | 4346d199925baf08f3422d3e0506b45a6508a4db (patch) | |
| tree | 3302cdf48572aebb589e473fa927c8320349b225 | |
| parent | b4dad4c7ca7dfcb4d5144b8608de8aebec6b5422 (diff) | |
| parent | 4b41cc2c7711aec989b064c5034841bc8949e543 (diff) | |
| download | perlweeklychallenge-club-4346d199925baf08f3422d3e0506b45a6508a4db.tar.gz perlweeklychallenge-club-4346d199925baf08f3422d3e0506b45a6508a4db.tar.bz2 perlweeklychallenge-club-4346d199925baf08f3422d3e0506b45a6508a4db.zip | |
Merge pull request #12405 from fer2o3/kachow
feat(331): completed both challenges
| -rwxr-xr-x | challenge-331/benjamin-andre/rust/ch-1.rs | 16 | ||||
| -rwxr-xr-x | challenge-331/benjamin-andre/rust/ch-2.rs | 24 |
2 files changed, 40 insertions, 0 deletions
diff --git a/challenge-331/benjamin-andre/rust/ch-1.rs b/challenge-331/benjamin-andre/rust/ch-1.rs new file mode 100755 index 0000000000..4fed8bf7e0 --- /dev/null +++ b/challenge-331/benjamin-andre/rust/ch-1.rs @@ -0,0 +1,16 @@ +#!/bin/sh +//usr/bin/env rustc --test $0 -o kachow && ./kachow --nocapture; rm -f kachow ; exit + +fn last_word(s: &str) -> usize { + s.trim() + .split_whitespace() + .last() + .map_or(0, |word| word.len()) +} + +#[test] +fn example() { + assert_eq!(last_word("The Weekly Challenge"), 9); + assert_eq!(last_word(" Hello World "), 5); + assert_eq!(last_word("Let's begin the fun"), 3); +} diff --git a/challenge-331/benjamin-andre/rust/ch-2.rs b/challenge-331/benjamin-andre/rust/ch-2.rs new file mode 100755 index 0000000000..33ed4dacdf --- /dev/null +++ b/challenge-331/benjamin-andre/rust/ch-2.rs @@ -0,0 +1,24 @@ +#!/bin/sh +//usr/bin/env rustc --test $0 -o kachow && ./kachow --nocapture; rm -f kachow ; exit + +fn buddy_strings(source: &str, target: &str) -> bool { + if source.len() != target.len() { + return false; + } + let s: Vec<char> = source.chars().collect(); + let t: Vec<char> = target.chars().collect(); + let diffs: Vec<usize> = (0..s.len()).filter(|&i| s[i] != t[i]).collect(); + match diffs.len() { + 0 => s.len() != s.iter().collect::<std::collections::HashSet<_>>().len(), + 2 => s[diffs[0]] == t[diffs[1]] && s[diffs[1]] == t[diffs[0]], + _ => false, + } +} + +#[test] +fn example() { + assert_eq!(buddy_strings("fuck", "fcuk"), true); + assert_eq!(buddy_strings("love", "love"), false); + assert_eq!(buddy_strings("fodo", "food"), true); + assert_eq!(buddy_strings("feed", "feed"), true); +} |
