diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-02-26 23:08:44 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-02-26 23:08:44 +0000 |
| commit | e67f938553ce59fdaad58ae8d08855fa3ce1b55a (patch) | |
| tree | 814b83e1cc65a2710266f5a1aea9b44a41df21cb | |
| parent | ee7818c61a0849d8181f7781de225977060fb66f (diff) | |
| parent | 4ee1533e93a29c58271b7b2a264a41d30d41c3e6 (diff) | |
| download | perlweeklychallenge-club-e67f938553ce59fdaad58ae8d08855fa3ce1b55a.tar.gz perlweeklychallenge-club-e67f938553ce59fdaad58ae8d08855fa3ce1b55a.tar.bz2 perlweeklychallenge-club-e67f938553ce59fdaad58ae8d08855fa3ce1b55a.zip | |
Merge pull request #7638 from ealvar3z/challenge-205_eax
Rust solution for week 205
| -rw-r--r-- | challenge-202/ealvar3z/README | 2 | ||||
| -rw-r--r-- | challenge-202/ealvar3z/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-202/ealvar3z/go/.gitignore | 1 | ||||
| -rw-r--r-- | challenge-202/ealvar3z/rust/.gitignore | 3 | ||||
| -rw-r--r-- | challenge-202/ealvar3z/rust/src/main.rs | 63 |
5 files changed, 68 insertions, 2 deletions
diff --git a/challenge-202/ealvar3z/README b/challenge-202/ealvar3z/README index a50c065308..d35a294b26 100644 --- a/challenge-202/ealvar3z/README +++ b/challenge-202/ealvar3z/README @@ -1 +1 @@ -Solution by ealvar3z +Solution by ealvar3z. Run `cargo test` to check them. diff --git a/challenge-202/ealvar3z/blog.txt b/challenge-202/ealvar3z/blog.txt new file mode 100644 index 0000000000..c849c43be3 --- /dev/null +++ b/challenge-202/ealvar3z/blog.txt @@ -0,0 +1 @@ +https://eax.bearblog.dev/perl-weekly-challenge-205/ diff --git a/challenge-202/ealvar3z/go/.gitignore b/challenge-202/ealvar3z/go/.gitignore deleted file mode 100644 index 654da7a30f..0000000000 --- a/challenge-202/ealvar3z/go/.gitignore +++ /dev/null @@ -1 +0,0 @@ -go.mod diff --git a/challenge-202/ealvar3z/rust/.gitignore b/challenge-202/ealvar3z/rust/.gitignore new file mode 100644 index 0000000000..9a30948bdd --- /dev/null +++ b/challenge-202/ealvar3z/rust/.gitignore @@ -0,0 +1,3 @@ +/target +Cargo.toml +Cargo.lock diff --git a/challenge-202/ealvar3z/rust/src/main.rs b/challenge-202/ealvar3z/rust/src/main.rs new file mode 100644 index 0000000000..0a38c837a5 --- /dev/null +++ b/challenge-202/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(); +} + |
