Решение на Сметки с ДНК от Атанас Тумбалев
Към профила на Атанас Тумбалев
Резултати
- 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 counter = NucleotideCounter {
a: 0,
c: 0,
g: 0,
t: 0
};
for nucleotide in dna {
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!("Bad")
}
}
counter
}
pub fn dna_complement(dna: &[char]) -> Vec<char> {
dna.iter().map(|x| match x {
'A' => 'T',
'C' => 'G',
'T' => 'A',
'G' => 'C',
_ => panic!("No such nucleotide!"),
}).collect()
}
pub fn reverse_rna_complement(dna: &[char]) -> Vec<char> {
dna.iter().map(|x| match x {
'A' => 'U',
'C' => 'G',
'T' => 'A',
'G' => 'C',
_ => panic!("No such nucleotide!"),
}).rev().collect()
}
Лог от изпълнението
Compiling solution v0.1.0 (/tmp/d20220112-2706256-ru20pj/solution) Finished test [unoptimized + debuginfo] target(s) in 5.64s 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
История (2 версии и 1 коментар)
Атанас качи решение на 25.10.2021 19:35 (преди почти 4 години)
pub struct NucleotideCounter {
pub a: usize,
pub c: usize,
pub g: usize,
pub t: usize,
}
pub fn counts(dna: &[char]) -> NucleotideCounter {
let mut counter = NucleotideCounter {
a: 0,
c: 0,
g: 0,
t: 0
};
for nucleotide in dna {
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!("Bad")
}
}
counter
}
pub fn dna_complement(dna: &[char]) -> Vec<char> {
- let mut complimentory: Vec<char> = Vec::with_capacity(dna.len());
-
- for nucleotide in dna {
- match nucleotide {
- 'A' => complimentory.push('T'),
- 'C' => complimentory.push('G'),
- 'T' => complimentory.push('A'),
- 'G' => complimentory.push('C'),
- _ => panic!("No such nucleotide!"),
- }
- }
-
- complimentory
+ dna.iter().map(|x| match x {
+ 'A' => 'T',
+ 'C' => 'G',
+ 'T' => 'A',
+ 'G' => 'C',
+ _ => panic!("No such nucleotide!"),
+ }).collect()
}
pub fn reverse_rna_complement(dna: &[char]) -> Vec<char> {
- let mut complimentory: Vec<char> = Vec::with_capacity(dna.len());
-
- for nucleotide in dna {
- match nucleotide {
- 'A' => complimentory.push('U'),
- 'C' => complimentory.push('G'),
- 'T' => complimentory.push('A'),
- 'G' => complimentory.push('C'),
- _ => panic!("No such nucleotide!"),
- }
- }
-
- complimentory.reverse();
- complimentory
+ dna.iter().map(|x| match x {
+ 'A' => 'U',
+ 'C' => 'G',
+ 'T' => 'A',
+ 'G' => 'C',
+ _ => panic!("No such nucleotide!"),
+ }).rev().collect()
}
Добра употреба на функционален стил в complement функциите 👍