From 843838f626b183d4cd1d45939759d88fbb058cba Mon Sep 17 00:00:00 2001 From: Fizzizist Date: Sun, 2 Mar 2025 11:42:59 -0500 Subject: [PATCH] IEV --- rust/src/main.rs | 8 +------- rust/src/solutions/iev.rs | 25 +++++++++++++++++++++++++ rust/src/solutions/mod.rs | 2 ++ rust/src/utils.rs | 10 +++++++++- 4 files changed, 37 insertions(+), 8 deletions(-) create mode 100644 rust/src/solutions/iev.rs diff --git a/rust/src/main.rs b/rust/src/main.rs index ffafb47..67092bb 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -1,13 +1,7 @@ mod solutions; mod utils; use clap::Parser; -use utils::run_solution; - -#[derive(clap::ValueEnum, Clone, Debug)] -enum Problem { - REVP, - PRTM, -} +use utils::{run_solution, Problem}; #[derive(Parser, Debug)] #[command( diff --git a/rust/src/solutions/iev.rs b/rust/src/solutions/iev.rs new file mode 100644 index 0000000..96a781b --- /dev/null +++ b/rust/src/solutions/iev.rs @@ -0,0 +1,25 @@ +pub fn iev(file_content: String) -> String { + let prob: [f64; 6] = [1.0, 1.0, 1.0, 0.75, 0.5, 0.0]; + let mut result: f64 = file_content + .trim() + .split_whitespace() + .zip(prob.iter()) + .map(|(a, b)| a.parse::().unwrap() * b) + .sum(); + // the problem specifies that the assumption is that every couple has exactly 2 offspring, + // thus, we multiply the result by 2 here. + result *= 2.0; + return result.to_string(); +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_iev() { + let input_content = "1 0 0 1 0 1".to_string(); + let expected = "3.5"; + assert_eq!(iev(input_content), expected); + } +} diff --git a/rust/src/solutions/mod.rs b/rust/src/solutions/mod.rs index 2c5307d..e66f048 100644 --- a/rust/src/solutions/mod.rs +++ b/rust/src/solutions/mod.rs @@ -1,4 +1,6 @@ +mod iev; mod prtm; mod revp; +pub use iev::iev; pub use prtm::prtm; pub use revp::revp; diff --git a/rust/src/utils.rs b/rust/src/utils.rs index 1882ee6..9060232 100644 --- a/rust/src/utils.rs +++ b/rust/src/utils.rs @@ -1,4 +1,4 @@ -use crate::{solutions, Problem}; +use crate::solutions; use std::{fs, io}; pub const AMINO_ACID_MASS_TABLE: [(char, f64); 20] = [ @@ -29,12 +29,20 @@ pub struct FastaRecord { pub sequence: String, } +#[derive(clap::ValueEnum, Clone, Debug)] +pub enum Problem { + REVP, + PRTM, + IEV, +} + pub fn run_solution(problem: Problem, in_file: &String, out_file: &String) { // parse the input file into a string let solution: io::Result = match fs::read_to_string(in_file) { Ok(content) => match problem { Problem::REVP => Ok(solutions::revp(content)), Problem::PRTM => Ok(solutions::prtm(content)), + Problem::IEV => Ok(solutions::iev(content)), }, Err(e) => Err(e), };