-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmedian.rs
65 lines (59 loc) · 1.52 KB
/
median.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
* Median of two sorted vectors
*
* Median of two sorted vectors challenge
*
* To run the following, run:
* =============================================================================
*
* cargo run --bin m6
* cargo test --bin m6
*
* =============================================================================
*/
struct Solution;
impl Solution {
fn find_median_sorted_arrays(mut nums1: Vec<i32>, nums2: Vec<i32>) -> f64 {
nums1.extend(nums2);
nums1.sort();
let high = nums1.len() / 2;
match nums1.len() % 2 {
1 => nums1[high] as f64,
_ => (nums1[high] as f64 + nums1[high - 1] as f64) / 2f64,
}
}
}
fn main() {
let (v1, v2) = (vec![1, 3], vec![2]);
println!(
"the median value of the following vectors {:?}, {:?} is: {}",
v1.clone(),
v2.clone(),
Solution::find_median_sorted_arrays(v1, v2)
);
let (v1, v2) = (vec![1, 2], vec![3, 4]);
println!(
"the median value of the following vectors {:?}, {:?} is: {}",
v1.clone(),
v2.clone(),
Solution::find_median_sorted_arrays(v1, v2)
);
}
#[cfg(test)]
mod tests {
use crate::Solution;
#[test]
fn test_median_odd_elements() {
assert_eq!(
Solution::find_median_sorted_arrays(vec![1, 3], vec![2]),
2f64
)
}
#[test]
fn test_median_even_elements() {
assert_eq!(
Solution::find_median_sorted_arrays(vec![1, 2], vec![3, 4]),
2.5
)
}
}