diff options
| author | Benjamin Andre <benjaminandre23@gmail.com> | 2025-10-20 10:45:16 +0200 |
|---|---|---|
| committer | Benjamin Andre <benjaminandre23@gmail.com> | 2025-10-20 10:45:16 +0200 |
| commit | dc7f882c5b61adc75d648d1d9b0b80e056d85bdb (patch) | |
| tree | 1352f6e4e2e3d2d514d3ce5a5d7aecd13f3b070f | |
| parent | 0089e40542c8edad54c99aad1b7e01bfe7050231 (diff) | |
| download | perlweeklychallenge-club-dc7f882c5b61adc75d648d1d9b0b80e056d85bdb.tar.gz perlweeklychallenge-club-dc7f882c5b61adc75d648d1d9b0b80e056d85bdb.tar.bz2 perlweeklychallenge-club-dc7f882c5b61adc75d648d1d9b0b80e056d85bdb.zip | |
feat(344): completed both challenges
| -rwxr-xr-x | challenge-344/benjamin-andre/rust/ch-1.rs | 23 | ||||
| -rwxr-xr-x | challenge-344/benjamin-andre/rust/ch-2.rs | 28 |
2 files changed, 51 insertions, 0 deletions
diff --git a/challenge-344/benjamin-andre/rust/ch-1.rs b/challenge-344/benjamin-andre/rust/ch-1.rs new file mode 100755 index 0000000000..a0445b65dc --- /dev/null +++ b/challenge-344/benjamin-andre/rust/ch-1.rs @@ -0,0 +1,23 @@ +#!/bin/sh +//usr/bin/env rustc --test $0 -o kachow && ./kachow --nocapture; rm -f kachow ; exit + +fn array_form_compute(ints: &[i32], x: i32) -> Vec<i32> { + let num: i64 = ints.iter().fold(0, |acc, &d| acc * 10 + d as i64); + let sum = num + x as i64; + sum.to_string() + .chars() + .map(|c| c.to_digit(10).unwrap() as i32) + .collect() +} + +#[test] +fn example() { + assert_eq!(array_form_compute(&[1, 2, 3, 4], 12), vec![1, 2, 4, 6]); + assert_eq!(array_form_compute(&[2, 7, 4], 181), vec![4, 5, 5]); + assert_eq!(array_form_compute(&[9, 9, 9], 1), vec![1, 0, 0, 0]); + assert_eq!( + array_form_compute(&[1, 0, 0, 0, 0], 9999), + vec![1, 9, 9, 9, 9] + ); + assert_eq!(array_form_compute(&[0], 1000), vec![1, 0, 0, 0]); +} diff --git a/challenge-344/benjamin-andre/rust/ch-2.rs b/challenge-344/benjamin-andre/rust/ch-2.rs new file mode 100755 index 0000000000..740ee45d31 --- /dev/null +++ b/challenge-344/benjamin-andre/rust/ch-2.rs @@ -0,0 +1,28 @@ +#!/bin/sh +//usr/bin/env rustc --test $0 -o kachow && ./kachow --nocapture; rm -f kachow ; exit + +fn array_formation(source: &[&[i32]], target: &[i32]) -> bool { + let mut pos = 0; + while pos < target.len() { + let chunk = source + .iter() + .find(|&&s| s.len() <= target.len() - pos && s == &target[pos..pos + s.len()]); + match chunk { + Some(c) => pos += c.len(), + None => return false, + } + } + true +} + +#[test] +fn example() { + assert_eq!(array_formation(&[&[2, 3], &[1], &[4]], &[1, 2, 3, 4]), true); + assert_eq!(array_formation(&[&[1, 3], &[2, 4]], &[1, 2, 3, 4]), false); + assert_eq!( + array_formation(&[&[9, 1], &[5, 8], &[2]], &[5, 8, 2, 9, 1]), + true + ); + assert_eq!(array_formation(&[&[1], &[3]], &[1, 2, 3]), false); + assert_eq!(array_formation(&[&[7, 4, 6]], &[7, 4, 6]), true); +} |
