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.97 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.96 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.95 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.94 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.93 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.92 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.91 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.90 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.89 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.88 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.87 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.86 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.85 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.84 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.83 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.82 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.81 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.80 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.79 tracks/rust/exercises/nucleotide-count/example.rs
trackler-2.2.1.78 tracks/rust/exercises/nucleotide-count/example.rs