Решение на Сметки с ДНК от Павел Атанасов
Резултати
- 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 nucleotide_counter = NucleotideCounter {
a: 0,
c: 0,
g: 0,
t: 0,
};
dna.iter()
.for_each(|nucleotide| match nucleotide {
'A' => nucleotide_counter.a += 1,
'C' => nucleotide_counter.c += 1,
'G' => nucleotide_counter.g += 1,
'T' => nucleotide_counter.t += 1,
_ => panic!("Invalid nucleotide!"),
});
nucleotide_counter
}
pub fn dna_complement(dna: &[char]) -> Vec<char> {
dna.iter()
.map(|nucleotide| match nucleotide {
'A' => 'T',
'C' => 'G',
'G' => 'C',
'T' => 'A',
_ => panic!("Invalid nucleotide!"),
})
.collect()
}
pub fn reverse_rna_complement(dna: &[char]) -> Vec<char> {
dna.iter()
.map(|nucleotide| match nucleotide {
'A' => 'U',
'C' => 'G',
'G' => 'C',
'T' => 'A',
_ => panic!("Invalid nucleotide!"),
})
.rev()
.collect()
}
Лог от изпълнението
Compiling solution v0.1.0 (/tmp/d20220112-2706256-1wdvr1m/solution) Finished test [unoptimized + debuginfo] target(s) in 5.66s 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
История (2 версии и 1 коментар)
Павел качи решение на 23.10.2021 21:45 (преди почти 4 години)
pub struct NucleotideCounter {
pub a: usize,
pub c: usize,
pub g: usize,
pub t: usize,
}
pub fn counts(dna: &[char]) -> NucleotideCounter {
let mut nucleotide_counter = NucleotideCounter {
a: 0,
c: 0,
g: 0,
t: 0,
};
dna.iter()
.for_each(|nucleotide| match nucleotide {
- 'a' | 'A' => nucleotide_counter.a += 1,
- 'c' | 'C' => nucleotide_counter.c += 1,
- 'g' | 'G' => nucleotide_counter.g += 1,
- 't' | 'T' => nucleotide_counter.t += 1,
+ 'A' => nucleotide_counter.a += 1,
+ 'C' => nucleotide_counter.c += 1,
+ 'G' => nucleotide_counter.g += 1,
+ 'T' => nucleotide_counter.t += 1,
_ => panic!("Invalid nucleotide!"),
});
nucleotide_counter
}
pub fn dna_complement(dna: &[char]) -> Vec<char> {
dna.iter()
.map(|nucleotide| match nucleotide {
- 'a' | 'A' => 'T',
- 'c' | 'C' => 'G',
- 'g' | 'G' => 'C',
- 't' | 'T' => 'A',
+ 'A' => 'T',
+ 'C' => 'G',
+ 'G' => 'C',
+ 'T' => 'A',
_ => panic!("Invalid nucleotide!"),
})
.collect()
}
pub fn reverse_rna_complement(dna: &[char]) -> Vec<char> {
dna.iter()
.map(|nucleotide| match nucleotide {
- 'a' | 'A' => 'U',
- 'c' | 'C' => 'G',
- 'g' | 'G' => 'C',
- 't' | 'T' => 'A',
+ 'A' => 'U',
+ 'C' => 'G',
+ 'G' => 'C',
+ 'T' => 'A',
_ => panic!("Invalid nucleotide!"),
})
.rev()
.collect()
}
Добро функционално решение, добре си се усетил да махнеш малките букви в един момент :)