aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-08-04 23:48:14 +0100
committerGitHub <noreply@github.com>2025-08-04 23:48:14 +0100
commitb62727b8c00d25c705786d1c53ee574269147613 (patch)
tree1815396b23c3b1cc4310e21da89f727cad8b2c60
parent183db446669b4cbba6f267b91787a0742b84383d (diff)
parent5f40b4b610c5c79c25908a7879e7c50a5b8d3a33 (diff)
downloadperlweeklychallenge-club-b62727b8c00d25c705786d1c53ee574269147613.tar.gz
perlweeklychallenge-club-b62727b8c00d25c705786d1c53ee574269147613.tar.bz2
perlweeklychallenge-club-b62727b8c00d25c705786d1c53ee574269147613.zip
Merge pull request #12469 from fer2o3/kachow
feat(333): finished both challenges
-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]);
+}