diff options
| author | Benjamin Andre <benjaminandre23@gmail.com> | 2025-10-31 14:11:51 +0100 |
|---|---|---|
| committer | Benjamin Andre <benjaminandre23@gmail.com> | 2025-10-31 14:11:51 +0100 |
| commit | a338924c4714503d3905725bf511117993936083 (patch) | |
| tree | 5128a13d80973cd7ee43455f481a6e15025bcf14 | |
| parent | 88a930c6a8de82b2c59f19958b4e1493ede4a4e0 (diff) | |
| download | perlweeklychallenge-club-a338924c4714503d3905725bf511117993936083.tar.gz perlweeklychallenge-club-a338924c4714503d3905725bf511117993936083.tar.bz2 perlweeklychallenge-club-a338924c4714503d3905725bf511117993936083.zip | |
feat(345): completed both challenges
| -rwxr-xr-x | challenge-345/benjamin-andre/rust/ch-1.rs | 21 | ||||
| -rwxr-xr-x | challenge-345/benjamin-andre/rust/ch-2.rs | 31 |
2 files changed, 52 insertions, 0 deletions
diff --git a/challenge-345/benjamin-andre/rust/ch-1.rs b/challenge-345/benjamin-andre/rust/ch-1.rs new file mode 100755 index 0000000000..e5d7fb1416 --- /dev/null +++ b/challenge-345/benjamin-andre/rust/ch-1.rs @@ -0,0 +1,21 @@ +#!/bin/sh +//usr/bin/env rustc --test $0 -o kachow && ./kachow --nocapture; rm -f kachow ; exit + +fn peak_positions(ints: &[i32]) -> Vec<usize> { + (0..ints.len()) + .filter(|&i| { + let left_ok = i == 0 || ints[i] > ints[i - 1]; + let right_ok = i == ints.len() - 1 || ints[i] > ints[i + 1]; + left_ok && right_ok && !(i == 0 && i == ints.len() - 1) + }) + .collect() +} + +#[test] +fn example() { + assert_eq!(peak_positions(&[1, 3, 2]), vec![1]); + assert_eq!(peak_positions(&[2, 4, 6, 5, 3]), vec![2]); + assert_eq!(peak_positions(&[1, 2, 3, 2, 4, 1]), vec![2, 4]); + assert_eq!(peak_positions(&[5, 3, 1]), vec![0]); + assert_eq!(peak_positions(&[1, 5, 1, 5, 1, 5, 1]), vec![1, 3, 5]); +} diff --git a/challenge-345/benjamin-andre/rust/ch-2.rs b/challenge-345/benjamin-andre/rust/ch-2.rs new file mode 100755 index 0000000000..002414605f --- /dev/null +++ b/challenge-345/benjamin-andre/rust/ch-2.rs @@ -0,0 +1,31 @@ +#!/bin/sh +//usr/bin/env rustc --test $0 -o kachow && ./kachow --nocapture; rm -f kachow ; exit + +fn last_visitor(ints: &[i32]) -> Vec<i32> { + let mut seen = Vec::new(); + let mut ans = Vec::new(); + let mut neg_count = 0; + for &n in ints { + if n > 0 { + seen.insert(0, n); + neg_count = 0; + } else { + if neg_count < seen.len() { + ans.push(seen[neg_count]); + } else { + ans.push(-1); + } + neg_count += 1; + } + } + ans +} + +#[test] +fn example() { + assert_eq!(last_visitor(&[5, -1, -1]), vec![5, -1]); + assert_eq!(last_visitor(&[3, 7, -1, -1, -1]), vec![7, 3, -1]); + assert_eq!(last_visitor(&[2, -1, 4, -1, -1]), vec![2, 4, 2]); + assert_eq!(last_visitor(&[10, 20, -1, 30, -1, -1]), vec![20, 30, 20]); + assert_eq!(last_visitor(&[-1, -1, 5, -1]), vec![-1, -1, 5]); +} |
