aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-09-01 20:18:36 +0100
committerGitHub <noreply@github.com>2025-09-01 20:18:36 +0100
commit167fa7566630b177f499ddd63cfb2290cd665ef5 (patch)
tree34e8666949698725db58e90718751b2cee3f103c
parent1802a14995644415a4a1c1155c3b86956cc31a82 (diff)
parent121494d51835e3b286b1e781fa63a6b0bf4949c5 (diff)
downloadperlweeklychallenge-club-167fa7566630b177f499ddd63cfb2290cd665ef5.tar.gz
perlweeklychallenge-club-167fa7566630b177f499ddd63cfb2290cd665ef5.tar.bz2
perlweeklychallenge-club-167fa7566630b177f499ddd63cfb2290cd665ef5.zip
Merge pull request #12615 from fer2o3/kachow
feat(337): completed both challenges
-rwxr-xr-xchallenge-337/benjamin-andre/rust/ch-1.rs20
-rwxr-xr-xchallenge-337/benjamin-andre/rust/ch-2.rs27
2 files changed, 47 insertions, 0 deletions
diff --git a/challenge-337/benjamin-andre/rust/ch-1.rs b/challenge-337/benjamin-andre/rust/ch-1.rs
new file mode 100755
index 0000000000..855f7b2ee0
--- /dev/null
+++ b/challenge-337/benjamin-andre/rust/ch-1.rs
@@ -0,0 +1,20 @@
+#!/bin/sh
+//usr/bin/env rustc --test $0 -o kachow && ./kachow --nocapture; rm -f kachow ; exit
+
+fn smaller_than_current(nums: &[i32]) -> Vec<i32> {
+ nums.iter()
+ .map(|&x| nums.iter().filter(|&&y| y <= x).count() as i32 - 1)
+ .collect()
+}
+
+#[test]
+fn example() {
+ assert_eq!(smaller_than_current(&[6, 5, 4, 8]), vec![2, 1, 0, 3]);
+ assert_eq!(smaller_than_current(&[7, 7, 7, 7]), vec![3, 3, 3, 3]);
+ assert_eq!(smaller_than_current(&[5, 4, 3, 2, 1]), vec![4, 3, 2, 1, 0]);
+ assert_eq!(
+ smaller_than_current(&[-1, 0, 3, -2, 1]),
+ vec![1, 2, 4, 0, 3]
+ );
+ assert_eq!(smaller_than_current(&[0, 1, 1, 2, 0]), vec![1, 3, 3, 4, 1]);
+}
diff --git a/challenge-337/benjamin-andre/rust/ch-2.rs b/challenge-337/benjamin-andre/rust/ch-2.rs
new file mode 100755
index 0000000000..115575be0e
--- /dev/null
+++ b/challenge-337/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
+
+fn odd_matrix(row: usize, col: usize, locations: &[[usize; 2]]) -> i32 {
+ let mut row_counts = vec![0; row];
+ let mut col_counts = vec![0; col];
+ for &[r, c] in locations {
+ row_counts[r] += 1;
+ col_counts[c] += 1;
+ }
+ (0..row)
+ .map(|r| {
+ (0..col)
+ .filter(|&c| (row_counts[r] + col_counts[c]) % 2 == 1)
+ .count()
+ })
+ .sum::<usize>() as i32
+}
+
+#[test]
+fn example() {
+ assert_eq!(odd_matrix(2, 3, &[[0, 1], [1, 1]]), 6);
+ assert_eq!(odd_matrix(2, 2, &[[1, 1], [0, 0]]), 0);
+ assert_eq!(odd_matrix(3, 3, &[[0, 0], [1, 2], [2, 1]]), 0);
+ assert_eq!(odd_matrix(1, 5, &[[0, 2], [0, 4]]), 2);
+ assert_eq!(odd_matrix(4, 2, &[[1, 0], [3, 1], [2, 0], [0, 1]]), 8);
+}