Sha256: 5f94a4c8527cfc03e036ba143a05a7694f0070b5c5735d7cf9886bc1512376cf

Contents?: true

Size: 741 Bytes

Versions: 336

Compression:

Stored size: 741 Bytes

Contents

use std::collections::HashMap;

static VALID_NUCLEOTIDES: &'static str = "ACGT";

pub fn count(nucleotide: char, input: &str) -> Result<usize, char> {
    let valid = |x: char| { VALID_NUCLEOTIDES.contains(x) };
    if valid(nucleotide) && input.chars().all(valid) {
        Ok(input.chars().filter(|&c| c == nucleotide).count())
    } else {
        Err(nucleotide)
    }
}

pub fn nucleotide_counts(input: &str) -> Result<HashMap<char, usize>, char> {
    let mut map: HashMap<char, usize> = VALID_NUCLEOTIDES.chars().map(|c| (c, 0)).collect();
    for nucleotide in input.chars() {
        if let Some(n) = map.get_mut(&nucleotide) {
            *n += 1;
        } else {
            return Err(nucleotide);
        }
    }
    Ok(map)
}

Version data entries

336 entries across 336 versions & 1 rubygems

Version Path
trackler-2.2.1.158 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.157 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.156 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.155 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.154 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.153 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.152 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.151 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.150 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.149 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.148 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.147 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.146 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.145 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.144 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.143 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.142 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.141 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.140 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.139 tracks/rust/exercises/nucleotide-count/example.rs