-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.rs
81 lines (67 loc) · 1.7 KB
/
main.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use std::io::BufRead;
fn main() {
let numbers = get_readings(std::io::stdin().lock());
println!("{:?}", numbers);
}
fn get_readings(input: impl BufRead) -> Vec<f64> {
input.lines()
.map(Result::unwrap)
.take_while(|s| s != "999")
.filter_map(|s|
match s.parse() {
Ok(f) if f >= 0.0 => Some(f),
_ => None,
})
.collect()
}
/*
std::vector<double>
get_readings(std::istream& input)
{
std::vector<double> result;
std::string line;
while (std::getline(input, line) && line != "999") {
std::istringstream iss(line);
if (double reading; iss >> reading && reading >= 0) {
result.push_back(reading);
}
}
return result;
}
*/
fn mean(readings: &[f64]) -> f64 {
let mut sum = 0.0;
for d in readings {
sum += *d;
}
sum / readings.len() as f64
}
#[test]
fn mean_works() {
assert_eq!( mean(&[7.0, 8.0, 9.0]), 8.0 );
}
fn count_ranges(mean: f64, readings: &[f64]) -> (usize, usize) {
// let mut below = 0;
// let mut above = 0;
//
// for &d in readings {
// if mean - 5. <= d && d < mean { below += 1; }
// if mean < d && d <= mean + 5. { above += 1; }
// }
//
// (below, above);
(readings.iter().copied().filter(|d| mean - 5. <= *d && *d < mean).count(),
readings.iter().copied().filter(|d| mean < *d && *d <= mean + 5.).count())
}
/*
int main()
{
auto readings = get_readings(std::cin);
auto m = mean(readings);
auto counts = count_ranges(m, readings);
std::cout
<< "Mean: " << m << "\n"
<< "Below: " << counts.first << "\n"
<< "Above: " << counts.second << "\n";
}
*/