diff options
author | Orangerot <purple@orangerot.dev> | 2024-06-27 11:30:16 +0200 |
---|---|---|
committer | Orangerot <purple@orangerot.dev> | 2024-06-27 11:30:16 +0200 |
commit | 4b0a6a01b051a4ebfbc17661d14cb23fe4f275fb (patch) | |
tree | 0072cca328fe5adb2ed61004010228ff85e2164d /median-of-two-sorted-arrays/src/main.rs |
Diffstat (limited to 'median-of-two-sorted-arrays/src/main.rs')
-rw-r--r-- | median-of-two-sorted-arrays/src/main.rs | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/median-of-two-sorted-arrays/src/main.rs b/median-of-two-sorted-arrays/src/main.rs new file mode 100644 index 0000000..a8557aa --- /dev/null +++ b/median-of-two-sorted-arrays/src/main.rs @@ -0,0 +1,23 @@ +fn main() { + println!("Hello, world!"); + let tests = vec![ + (vec![1,3], vec![2], 2.0), + (vec![1,2], vec![3,4], 2.5) + ]; + for test in tests { + println!("{:?}{:?} is {} should be {}", test.0, test.1, + Solution::find_median_sorted_arrays(test.0.clone(), test.1.clone()), test.2); + } +} + +struct Solution {} +impl Solution { + pub fn find_median_sorted_arrays(nums1: Vec<i32>, nums2: Vec<i32>) -> f64 { + let mut a = nums1.clone(); + a.append(&mut nums2.clone()); + a.sort(); + let median = a.get((a.len() / 2)-1..((a.len() + 1)/2)).unwrap(); + println!("{:?}", median); + (median.iter().sum::<i32>() as f64) / (median.len() as f64) + } +} |