diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-07-08 17:08:37 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-08 17:08:37 +0100 |
| commit | 7f90f31f14e1fc23b2be25498fde469f95eed51b (patch) | |
| tree | f5650a78a8a8e5be4a7cdd6e02c342341ef3995e | |
| parent | 465374a428addbacac62ccb79495e2cfbf210bce (diff) | |
| parent | d901130b3cb916aab7de69287095766ae63a54a3 (diff) | |
| download | perlweeklychallenge-club-7f90f31f14e1fc23b2be25498fde469f95eed51b.tar.gz perlweeklychallenge-club-7f90f31f14e1fc23b2be25498fde469f95eed51b.tar.bz2 perlweeklychallenge-club-7f90f31f14e1fc23b2be25498fde469f95eed51b.zip | |
Merge pull request #12309 from fer2o3/kachow
feat(329): completed both challenges in rust
| -rwxr-xr-x | challenge-329/benjamin-andre/rust/ch-1.rs | 22 | ||||
| -rwxr-xr-x | challenge-329/benjamin-andre/rust/ch-2.rs | 27 |
2 files changed, 49 insertions, 0 deletions
diff --git a/challenge-329/benjamin-andre/rust/ch-1.rs b/challenge-329/benjamin-andre/rust/ch-1.rs new file mode 100755 index 0000000000..4c069e3b8b --- /dev/null +++ b/challenge-329/benjamin-andre/rust/ch-1.rs @@ -0,0 +1,22 @@ +#!/bin/sh +//usr/bin/env rustc --test $0 -o kachow && ./kachow --nocapture; rm -f kachow ; exit + +use std::collections::HashSet; + +fn counter_integer(s: &str) -> Vec<i32> { + let mut res: Vec<i32> = s + .split(|c: char| !c.is_ascii_digit()) + .filter_map(|num_str| num_str.parse().ok()) + .collect::<HashSet<_>>() + .into_iter() + .collect(); + res.sort(); + res +} + +#[test] +fn example() { + assert_eq!(counter_integer("the1weekly2challenge2"), vec![1, 2]); + assert_eq!(counter_integer("go21od1lu5c7k"), vec![1, 5, 7, 21]); + assert_eq!(counter_integer("4p3e2r1l"), vec![1, 2, 3, 4]); +} diff --git a/challenge-329/benjamin-andre/rust/ch-2.rs b/challenge-329/benjamin-andre/rust/ch-2.rs new file mode 100755 index 0000000000..8951c30ae0 --- /dev/null +++ b/challenge-329/benjamin-andre/rust/ch-2.rs @@ -0,0 +1,27 @@ +#!/bin/sh +//usr/bin/env rustc --test $0 -o kachow && ./kachow --nocapture; rm -f kachow ; exit + +// Ok this is kind of ugly, but hey it works +fn nice_string(s: &str) -> &str { + let mut longest = ""; + for i in 0..s.len() { + for j in i..s.len() { + let sub = &s[i..=j]; + if sub.chars().all(|c| { + !c.is_ascii_alphabetic() + || sub.contains(c.to_ascii_lowercase()) && sub.contains(c.to_ascii_uppercase()) + }) && sub.len() > longest.len() + { + longest = sub; + } + } + } + longest +} + +#[test] +fn example() { + assert_eq!(nice_string("YaaAho"), "aaA"); + assert_eq!(nice_string("cC"), "cC"); + assert_eq!(nice_string("A"), ""); +} |
