Решение на Сметки с ДНК от Йоан Цвятков

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

Към профила на Йоан Цвятков

Резултати

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

Код

pub struct NucleotideCounter {
pub a: usize,
pub c: usize,
pub g: usize,
pub t: usize,
}
pub fn counts(dna: &[char]) -> NucleotideCounter {
let mut result: NucleotideCounter = NucleotideCounter{
a: 0,
c: 0,
g: 0,
t: 0,
};
for ele in dna {
match ele {
'A' =>{
result.a += 1;
}
'C' => {
result.c += 1;
}
'G' => {
result.g += 1;
}
'T' => {
result.t += 1;
}
_ => {
panic!("Грешка");
}
};
};
result
}
pub fn dna_complement(dna: &[char]) -> Vec<char> {
let mut result: Vec<char> = Vec::new();
for ele in dna {
match ele {
'A' =>{
result.push('T');
}
'G' => {
result.push('C');
}
'C' => {
result.push('G');
}
'T' => {
result.push('A')
}
_ => {
panic!("Грешка");
}
};
}
result
}
pub fn reverse_rna_complement(dna: &[char]) -> Vec<char> {
let mut res: Vec<char> = Vec::new();
let mut compliment = dna_complement(dna);
compliment.reverse();
for elem in compliment{
if elem == 'T' {
res.push('U')
}else {
res.push(elem);
}
};
res
}

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

Compiling solution v0.1.0 (/tmp/d20220112-2706256-v4qoao/solution)
    Finished test [unoptimized + debuginfo] target(s) in 5.77s
     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.00s

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

Йоан качи първо решение на 23.10.2021 20:29 (преди почти 4 години)

Разумно решение, макар че може да се опрости чисто синтактично, ако махнеш обвиващите блокове в ръкавите на match statement-a.

match ele {
  'A' =>{
      result.a += 1;
  }

може да е:

match ele {
  'A' => result.a += 1,