This commit is contained in:
Fizzizist 2025-03-02 11:42:59 -05:00
parent acb5c8976e
commit 843838f626
4 changed files with 37 additions and 8 deletions

View File

@ -1,13 +1,7 @@
mod solutions; mod solutions;
mod utils; mod utils;
use clap::Parser; use clap::Parser;
use utils::run_solution; use utils::{run_solution, Problem};
#[derive(clap::ValueEnum, Clone, Debug)]
enum Problem {
REVP,
PRTM,
}
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command( #[command(

25
rust/src/solutions/iev.rs Normal file
View File

@ -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::<f64>().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);
}
}

View File

@ -1,4 +1,6 @@
mod iev;
mod prtm; mod prtm;
mod revp; mod revp;
pub use iev::iev;
pub use prtm::prtm; pub use prtm::prtm;
pub use revp::revp; pub use revp::revp;

View File

@ -1,4 +1,4 @@
use crate::{solutions, Problem}; use crate::solutions;
use std::{fs, io}; use std::{fs, io};
pub const AMINO_ACID_MASS_TABLE: [(char, f64); 20] = [ pub const AMINO_ACID_MASS_TABLE: [(char, f64); 20] = [
@ -29,12 +29,20 @@ pub struct FastaRecord {
pub sequence: String, 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) { pub fn run_solution(problem: Problem, in_file: &String, out_file: &String) {
// parse the input file into a string // parse the input file into a string
let solution: io::Result<String> = match fs::read_to_string(in_file) { let solution: io::Result<String> = match fs::read_to_string(in_file) {
Ok(content) => match problem { Ok(content) => match problem {
Problem::REVP => Ok(solutions::revp(content)), Problem::REVP => Ok(solutions::revp(content)),
Problem::PRTM => Ok(solutions::prtm(content)), Problem::PRTM => Ok(solutions::prtm(content)),
Problem::IEV => Ok(solutions::iev(content)),
}, },
Err(e) => Err(e), Err(e) => Err(e),
}; };