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.179 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.178 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.177 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.176 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.175 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.174 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.173 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.172 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.171 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.170 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.169 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.167 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.166 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.165 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.164 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.163 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.162 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.161 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.160 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.159 tracks/rust/exercises/nucleotide-count/example.rs