From 4b0a6a01b051a4ebfbc17661d14cb23fe4f275fb Mon Sep 17 00:00:00 2001 From: Orangerot Date: Thu, 27 Jun 2024 11:30:16 +0200 Subject: Initial commit --- median-of-two-sorted-arrays/src/main.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 median-of-two-sorted-arrays/src/main.rs (limited to 'median-of-two-sorted-arrays/src/main.rs') 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, nums2: Vec) -> 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::() as f64) / (median.len() as f64) + } +} -- cgit v1.2.3