dont use vectors

This commit is contained in:
Aurel Feer 2024-08-18 11:37:09 +02:00
parent 146a04c39a
commit 80b0cf2263

View File

@ -5,7 +5,10 @@ use std::io::prelude::*;
use std::collections::HashMap;
struct Station {
values: Vec<f64>
min: f64,
max: f64,
sum: f64,
n: usize,
}
fn main() {
@ -42,9 +45,17 @@ fn main() {
let name = String::from(&line[..n - i - 1]);
if let Some(station) = stations.get_mut(&name) {
station.values.push(temp);
station.min = station.min.min(temp);
station.max = station.max.max(temp);
station.sum += temp;
station.n += 1;
} else {
stations.insert(name, Station { values: vec![temp] });
stations.insert(name, Station {
min: temp,
max: temp,
sum: temp,
n: 1,
});
}
break;
}
@ -54,10 +65,11 @@ fn main() {
let mut stations: Vec<_> = stations.iter().collect();
stations.sort_unstable_by(|(n1, _), (n2, _)| n1.cmp(n2));
for (name, station) in stations {
assert!(!station.values.is_empty());
println!("{};{:.1};{:.1};{:.1}", name,
station.values.iter().cloned().fold(100., f64::min),
station.values.iter().sum::<f64>() / station.values.len() as f64,
station.values.iter().cloned().fold(-100., f64::max))
println!("{};{:.1};{:.1};{:.1}",
name,
station.min,
station.sum / station.n as f64,
station.max
);
}
}