summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2013-03-08 02:08:17 -0600
committerJesse Luehrs <doy@tozt.net>2013-03-08 02:08:17 -0600
commit91aa0faa200421e5a95fc52d20e492288dc94f31 (patch)
tree8f58b450cc0cd477b99ad67e75c7d4de554be010
parent285740580fde9d451f8f5589484cb94fab5596c2 (diff)
downloadrosalind-91aa0faa200421e5a95fc52d20e492288dc94f31.tar.gz
rosalind-91aa0faa200421e5a95fc52d20e492288dc94f31.zip
rust implementation
-rw-r--r--DNA.rs35
1 files changed, 35 insertions, 0 deletions
diff --git a/DNA.rs b/DNA.rs
new file mode 100644
index 0000000..dc572e2
--- /dev/null
+++ b/DNA.rs
@@ -0,0 +1,35 @@
+use io::{stdin,println,ReaderUtil};
+
+fn count_nucleotides(dna: &str) -> (int, int, int, int) {
+ let mut (a, c, g, t) = (0, 0, 0, 0);
+ for str::each_char(dna) |ch| {
+ match ch {
+ 'A' => a += 1,
+ 'C' => c += 1,
+ 'G' => g += 1,
+ 'T' => t += 1,
+ _ => fail ~"Unexpected character found"
+ }
+ }
+ (a, c, g, t)
+}
+
+fn count_nucleotides_2(dna: &str) -> (int, int, int, int) {
+ do iter::foldl(&str::chars(dna), (0, 0, 0, 0)) |res, &ch| {
+ let (a, c, g, t) = *res;
+ match ch {
+ 'A' => (a + 1, c, g, t),
+ 'C' => (a, c + 1, g, t),
+ 'G' => (a, c, g + 1, t),
+ 'T' => (a, c, g, t + 1),
+ _ => fail ~"Unexpected character found"
+ }
+ }
+}
+
+fn main() {
+ let dna = stdin().read_line();
+ let (a, c, g, t) = count_nucleotides(dna);
+ /*let (a, c, g, t) = count_nucleotides_2(dna);*/
+ println(fmt!("%d %d %d %d", a, c, g, t));
+}