diff options
| author | e. alvarez <55966724+ealvar3z@users.noreply.github.com> | 2023-02-26 15:31:18 -0800 |
|---|---|---|
| committer | e. alvarez <55966724+ealvar3z@users.noreply.github.com> | 2023-02-26 15:31:18 -0800 |
| commit | 35eb014d2f27611900f59e11cb7f22f1c3f75e88 (patch) | |
| tree | d74a389c2df9d60d4eda5a46bac73f253ca7e95a | |
| parent | 164cb2c66176378c79e036b63be79867afbb29a6 (diff) | |
| download | perlweeklychallenge-club-35eb014d2f27611900f59e11cb7f22f1c3f75e88.tar.gz perlweeklychallenge-club-35eb014d2f27611900f59e11cb7f22f1c3f75e88.tar.bz2 perlweeklychallenge-club-35eb014d2f27611900f59e11cb7f22f1c3f75e88.zip | |
fixes PR #7638
| -rw-r--r-- | challenge-205/ealvar3z/README | 2 | ||||
| -rw-r--r-- | challenge-205/ealvar3z/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-205/ealvar3z/go/.gitignore | 1 | ||||
| -rw-r--r-- | challenge-205/ealvar3z/rust/.gitignore | 3 | ||||
| -rw-r--r-- | challenge-205/ealvar3z/rust/src/main.rs | 63 |
5 files changed, 68 insertions, 2 deletions
diff --git a/challenge-205/ealvar3z/README b/challenge-205/ealvar3z/README index a50c065308..d35a294b26 100644 --- a/challenge-205/ealvar3z/README +++ b/challenge-205/ealvar3z/README @@ -1 +1 @@ -Solution by ealvar3z +Solution by ealvar3z. Run `cargo test` to check them. diff --git a/challenge-205/ealvar3z/blog.txt b/challenge-205/ealvar3z/blog.txt new file mode 100644 index 0000000000..c849c43be3 --- /dev/null +++ b/challenge-205/ealvar3z/blog.txt @@ -0,0 +1 @@ +https://eax.bearblog.dev/perl-weekly-challenge-205/ diff --git a/challenge-205/ealvar3z/go/.gitignore b/challenge-205/ealvar3z/go/.gitignore deleted file mode 100644 index 654da7a30f..0000000000 --- a/challenge-205/ealvar3z/go/.gitignore +++ /dev/null @@ -1 +0,0 @@ -go.mod diff --git a/challenge-205/ealvar3z/rust/.gitignore b/challenge-205/ealvar3z/rust/.gitignore new file mode 100644 index 0000000000..9a30948bdd --- /dev/null +++ b/challenge-205/ealvar3z/rust/.gitignore @@ -0,0 +1,3 @@ +/target +Cargo.toml +Cargo.lock diff --git a/challenge-205/ealvar3z/rust/src/main.rs b/challenge-205/ealvar3z/rust/src/main.rs new file mode 100644 index 0000000000..0a38c837a5 --- /dev/null +++ b/challenge-205/ealvar3z/rust/src/main.rs @@ -0,0 +1,63 @@ +#[cfg(test)] +mod tests { + use super::*; + + #[test] + pub fn test_task_one() { + let a = [5,3,4]; + let b = [5,6]; + let c = [5,4,4,3]; + + assert_eq!(task_one(&a), 3); + assert_eq!(task_one(&b), 6); + assert_eq!(task_one(&c), 3); + } + + #[test] + pub fn test_task_two() { + let a = [1,2,3,4,5,6,7]; + let b = [2,4,1,3]; + let c = [10,5,7,12,8]; + + assert_eq!(task_two(&a), 7); + assert_eq!(task_two(&b), 7); + assert_eq!(task_two(&c), 15); + } +} + +pub fn task_one(arr: &[i32]) -> i32 { + let highest = arr.iter().max().cloned().unwrap_or_default(); + let x = arr.iter().filter(|&i| *i != highest); + let snd_highest = x.clone().max().cloned().unwrap_or_default(); + let third_highest = x.filter(|&i| *i != snd_highest); + return *third_highest.max().unwrap_or(&highest); +} + +pub fn task_two(arr: &[i32]) -> i32 { + arr.iter() + .enumerate() + .fold(0, |max_xor, (i, &x)| { + arr[i+1..] + .iter() + .fold(max_xor, |max_xor, &y| { + let xor = x ^ y; + return xor.max(max_xor) + }) + }) +} + +fn main() { + #[cfg(test)] + mod test_runner { + use super::tests; + + pub fn run() { + tests::test_task_one(); + tests::test_task_two(); + } + } + + #[cfg(test)] + test_runner::run(); +} + |
