summaryrefslogblamecommitdiffstats
path: root/DNA.rs
blob: 12e60d7c5ce727c65773fb90968322f6c094d82a (plain) (tree)
1
2

                             




























                                                               
                           

                                                    
                                                 
 
extern mod rosalind;
use rosalind::io::input_line;

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 = input_line();
    let (a, c, g, t) = count_nucleotides(dna);
    /*let (a, c, g, t) = count_nucleotides_2(dna);*/
    io::println(fmt!("%d %d %d %d", a, c, g, t));
}