diff options
| -rwxr-xr-x | challenge-337/benjamin-andre/rust/ch-1.rs | 20 | ||||
| -rwxr-xr-x | challenge-337/benjamin-andre/rust/ch-2.rs | 27 |
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); +} |
