From 45fab565a31a535324a335fa96df8f010d855b5c Mon Sep 17 00:00:00 2001 From: Andinus Date: Tue, 28 Sep 2021 23:38:47 +0530 Subject: Rust: Assembly Line: Use a helper function for success rate --- rust/assembly-line/src/lib.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'rust') diff --git a/rust/assembly-line/src/lib.rs b/rust/assembly-line/src/lib.rs index 4d8d248..287172b 100644 --- a/rust/assembly-line/src/lib.rs +++ b/rust/assembly-line/src/lib.rs @@ -1,14 +1,16 @@ -const CARS_PER_HR: u32 = 221; - -pub fn production_rate_per_hour(speed: u8) -> f64 { - let mut cars: f64 = ((speed as u32) * CARS_PER_HR) as f64; +const CARS_PER_HR: f64 = 221.0; +fn success_rate(speed: u8) -> f64 { match speed { - 5..=8 => cars *= 0.90, - 9 | 10 => cars *= 0.77, - _ => {}, + 1..=4 => 1.0, + 5..=8 => 0.9, + 9|10 => 0.77, + _ => 0.0, } - cars +} + +pub fn production_rate_per_hour(speed: u8) -> f64 { + (speed as f64) * CARS_PER_HR * success_rate(speed) } pub fn working_items_per_minute(speed: u8) -> u32 { -- cgit 1.4.1-2-gfad0