Last active
December 11, 2018 04:00
-
-
Save voldyman/d550dda06c325a84ea829f4e16ae0ca7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "first" | |
version = "0.1.0" | |
authors = ["Your Name <you@example.com>"] | |
[dependencies] | |
serde = "1.0.81" | |
serde_json = "1.0.33" | |
serde_derive = "1.0.81" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::io; | |
use std::io::BufRead; | |
#[macro_use] | |
extern crate serde_derive; | |
extern crate serde; | |
extern crate serde_json; | |
#[derive(Serialize, Deserialize, Debug)] | |
struct Stats { | |
max: f64, | |
mean: f64, | |
min: f64, | |
size: u64, | |
sum: f64, | |
} | |
impl Stats { | |
fn new() -> Stats { | |
return Stats { | |
max: 0f64, | |
min: 0f64, | |
size: 0, | |
sum: 0f64, | |
mean: 0f64, | |
}; | |
} | |
fn update(&mut self, val: f64) { | |
if self.max < val { | |
self.max = val; | |
} | |
if self.min > val { | |
self.min = val; | |
} | |
self.sum = self.sum + val; | |
self.size += 1; | |
self.mean = self.sum / (self.size as f64); | |
} | |
} | |
fn main() { | |
let mut stats = Stats::new(); | |
let stdin = io::stdin(); | |
for line in stdin.lock().lines() { | |
let num: f64 = line.unwrap().parse().unwrap(); | |
stats.update(num); | |
} | |
println!("{:?}", stats); | |
let serialized = serde_json::to_string(&stats).unwrap(); | |
println!("{}", serialized); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment