-
Notifications
You must be signed in to change notification settings - Fork 0
/
mean-of-array-after-removing-some-elements.rs
49 lines (45 loc) · 1.19 KB
/
mean-of-array-after-removing-some-elements.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
struct Solution;
impl Solution {
pub fn trim_mean(arr: Vec<i32>) -> f64 {
let mut arr = arr;
arr.sort();
let a = (arr.len() as f64 * 0.05).round() as usize;
let new = &arr[a..arr.len() - a];
new.iter().sum::<i32>() as f64 / new.len() as f64
}
}
fn main() {
println!(
"{}",
Solution::trim_mean(vec![
6, 2, 7, 5, 1, 2, 0, 3, 10, 2, 5, 0, 5, 5, 0, 8, 7, 6, 8, 0
])
);
println!(
"{}",
Solution::trim_mean(vec![
6, 0, 7, 0, 7, 5, 7, 8, 3, 4, 0, 7, 8, 1, 6, 8, 1, 1, 2, 4, 8, 1, 9, 5, 4, 3, 8, 5, 10,
8, 6, 6, 1, 0, 6, 10, 8, 2, 3, 4
])
);
}
#[cfg(test)]
mod tests {
use crate::Solution;
#[test]
fn test() {
assert_eq!(
4.0,
Solution::trim_mean(vec![
6, 2, 7, 5, 1, 2, 0, 3, 10, 2, 5, 0, 5, 5, 0, 8, 7, 6, 8, 0
])
);
assert_eq!(
4.777777777777778,
Solution::trim_mean(vec![
6, 0, 7, 0, 7, 5, 7, 8, 3, 4, 0, 7, 8, 1, 6, 8, 1, 1, 2, 4, 8, 1, 9, 5, 4, 3, 8, 5,
10, 8, 6, 6, 1, 0, 6, 10, 8, 2, 3, 4
])
);
}
}