aboutsummaryrefslogtreecommitdiff
path: root/challenge-333/benjamin-andre/rust
diff options
context:
space:
mode:
authorBenjamin Andre <benjaminandre23@gmail.com>2025-08-04 20:30:11 +0200
committerBenjamin Andre <benjaminandre23@gmail.com>2025-08-04 20:30:11 +0200
commit5f40b4b610c5c79c25908a7879e7c50a5b8d3a33 (patch)
tree8a8e2c3775f5dd364c3090fd64a28bd7e81842e0 /challenge-333/benjamin-andre/rust
parentce2f933a023e15e5dac73508e56a9aec0e87fae6 (diff)
downloadperlweeklychallenge-club-5f40b4b610c5c79c25908a7879e7c50a5b8d3a33.tar.gz
perlweeklychallenge-club-5f40b4b610c5c79c25908a7879e7c50a5b8d3a33.tar.bz2
perlweeklychallenge-club-5f40b4b610c5c79c25908a7879e7c50a5b8d3a33.zip
feat(333): finished both challenges
Diffstat (limited to 'challenge-333/benjamin-andre/rust')
-rwxr-xr-xchallenge-333/benjamin-andre/rust/ch-1.rs27
-rwxr-xr-xchallenge-333/benjamin-andre/rust/ch-2.rs28
2 files changed, 55 insertions, 0 deletions
diff --git a/challenge-333/benjamin-andre/rust/ch-1.rs b/challenge-333/benjamin-andre/rust/ch-1.rs
new file mode 100755
index 0000000000..e6864d6aed
--- /dev/null
+++ b/challenge-333/benjamin-andre/rust/ch-1.rs
@@ -0,0 +1,27 @@
+#!/bin/sh
+//usr/bin/env rustc --test $0 -o kachow && ./kachow --nocapture; rm -f kachow ; exit
+
+fn straight_line(points: &[[i64; 2]]) -> bool {
+ if points.len() <= 2 {
+ return true;
+ }
+
+ let [x1, y1] = points[0];
+ let [x2, y2] = points[1];
+
+ points[2..]
+ .iter()
+ .all(|&[x3, y3]| (y2 - y1) * (x3 - x1) == (y3 - y1) * (x2 - x1))
+}
+
+#[test]
+fn example() {
+ assert_eq!(straight_line(&[[2, 1], [2, 3], [2, 5]]), true);
+ assert_eq!(straight_line(&[[1, 4], [3, 4], [10, 4]]), true);
+ assert_eq!(straight_line(&[[0, 0], [1, 1], [2, 3]]), false);
+ assert_eq!(straight_line(&[[1, 1], [1, 1], [1, 1]]), true);
+ assert_eq!(
+ straight_line(&[[1000000, 1000000], [2000000, 2000000], [3000000, 3000000]]),
+ true
+ );
+}
diff --git a/challenge-333/benjamin-andre/rust/ch-2.rs b/challenge-333/benjamin-andre/rust/ch-2.rs
new file mode 100755
index 0000000000..5f15671a76
--- /dev/null
+++ b/challenge-333/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 duplicate_zeros(ints: &[i32]) -> Vec<i32> {
+ let mut result = Vec::new();
+ for &n in ints {
+ if n == 0 {
+ result.push(0);
+ result.push(0);
+ } else {
+ result.push(n);
+ }
+ }
+ result.truncate(ints.len());
+ result
+}
+
+#[test]
+fn example() {
+ assert_eq!(
+ duplicate_zeros(&[1, 0, 2, 3, 0, 4, 5, 0]),
+ vec![1, 0, 0, 2, 3, 0, 0, 4]
+ );
+ assert_eq!(duplicate_zeros(&[1, 2, 3]), vec![1, 2, 3]);
+ assert_eq!(duplicate_zeros(&[1, 2, 3, 0]), vec![1, 2, 3, 0]);
+ assert_eq!(duplicate_zeros(&[0, 0, 1, 2]), vec![0, 0, 0, 0]);
+ assert_eq!(duplicate_zeros(&[1, 2, 0, 3, 4]), vec![1, 2, 0, 0, 3]);
+}