Решение на Сметки с ДНК от Теодора Колева

Обратно към всички решения

Към профила на Теодора Колева

Резултати

  • 20 точки от тестове
  • 0 бонус точки
  • 20 точки общо
  • 12 успешни тест(а)
  • 0 неуспешни тест(а)

Код

pub struct NucleotideCounter {
pub a: usize,
pub c: usize,
pub g: usize,
pub t: usize,
}
impl NucleotideCounter {
fn new() -> Self {
Self { a: 0, c: 0, g: 0, t: 0 }
}
}
pub fn counts(dna: &[char]) -> NucleotideCounter {
let mut counter = NucleotideCounter::new();
for base in dna {
match base {
'A' => counter.a += 1,
'C' => counter.c += 1,
'G' => counter.g += 1,
'T' => counter.t += 1,
_ => panic!("wrong input")
}
}
counter
}
pub fn dna_complement(dna: &[char]) -> Vec<char> {
let mut res: Vec<char> = Vec::new();
for base in dna {
match base {
'A' => res.push('T'),
'C' => res.push('G'),
'G' => res.push('C'),
'T' => res.push('A'),
_ => panic!("wrong input")
}
}
res
}
pub fn reverse_rna_complement(dna: &[char]) -> Vec<char> {
let mut res: Vec<char> = Vec::new();
for base in dna {
match base {
'A' => res.push('U'),
'C' => res.push('G'),
'G' => res.push('C'),
'T' => res.push('A'),
_ => panic!("wrong input")
}
}
res.reverse();
res
}

Лог от изпълнението

Compiling solution v0.1.0 (/tmp/d20220112-2706256-8ivhs/solution)
    Finished test [unoptimized + debuginfo] target(s) in 5.57s
     Running tests/solution_test.rs (target/debug/deps/solution_test-4c880d3f0adaac34)

running 12 tests
test solution_test::test_counts_basic ... ok
test solution_test::test_counts_big ... ok
test solution_test::test_counts_panic1 - should panic ... ok
test solution_test::test_counts_panic2 - should panic ... ok
test solution_test::test_counts_zero ... ok
test solution_test::test_dna_complement_big ... ok
test solution_test::test_dna_complement_empty ... ok
test solution_test::test_dna_complement_panic - should panic ... ok
test solution_test::test_reverse_rna_complement_big ... ok
test solution_test::test_reverse_rna_complement_empty ... ok
test solution_test::test_reverse_rna_complement_panic1 - should panic ... ok
test solution_test::test_reverse_rna_complement_panic2 - should panic ... ok

test result: ok. 12 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.01s

История (1 версия и 1 коментар)

Теодора качи първо решение на 24.10.2021 16:35 (преди почти 4 години)

Добро решение 👍. Допълнителната new функция има смисъл. За да избегнеш изцяло инициализирането, може вместо нея да сложиш #[derive(Default)] на структурата, за да получиш NucleotideCounter::default(), което ще инициализира всички полета с 0.