Решение на Сметки с ДНК от Мариян Момчилов

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

Към профила на Мариян Момчилов

Резултати

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

Код

pub struct NucleotideCounter {
pub a: usize,
pub c: usize,
pub g: usize,
pub t: usize,
}
fn update_on_match(counter: & mut NucleotideCounter, nucleotide: & char) {
match nucleotide {
'A' => counter.a = counter.a + 1,
'C' => counter.c = counter.c + 1,
'G' => counter.g = counter.g + 1,
'T' => counter.t = counter.t + 1,
_ => panic!("Error")
}
}
pub fn counts(dna: &[char]) -> NucleotideCounter {
let mut counter = NucleotideCounter { a: 0, c: 0, g: 0, t: 0 };
for nucleotide in dna.iter() {
update_on_match(& mut counter, nucleotide)
}
counter
}
pub fn dna_complement(dna: &[char]) -> Vec<char> {
let mut complement = Vec::<char>::with_capacity(dna.len());
for nucleotide in dna.iter() {
match nucleotide {
'A' => complement.push('T'),
'C' => complement.push('G'),
'G' => complement.push('C'),
'T' => complement.push('A'),
_ => panic!("Error")
}
}
complement
}
pub fn reverse_rna_complement(dna: &[char]) -> Vec<char> {
let mut rna = Vec::<char>::with_capacity(dna.len());
for nucleotide in dna.iter().rev() {
match nucleotide {
'A' => rna.push('U'),
'C' => rna.push('G'),
'G' => rna.push('C'),
'T' => rna.push('A'),
_ => panic!("Error")
}
}
rna
}

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

Compiling solution v0.1.0 (/tmp/d20220112-2706256-19c1tr3/solution)
    Finished test [unoptimized + debuginfo] target(s) in 5.49s
     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 коментар)

Мариян качи първо решение на 23.10.2021 14:55 (преди почти 4 години)

Разумно решение 👍. Не бих казал, че си заслужава да извадиш отделна функция за инкрементирането на counter-а. Extract-ването на помощни функции може да е удачно когато има нужда да се преизползват, или когато функцията стане голяма и трудна за разбиране. В случая не мисля, че печелиш много от индирекцията.