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