Skip to content

Instantly share code, notes, and snippets.

@zzeroo
Forked from anonymous/playground.rs
Created March 17, 2016 07:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zzeroo/0a9365a4745ec6d5999e to your computer and use it in GitHub Desktop.
Save zzeroo/0a9365a4745ec6d5999e to your computer and use it in GitHub Desktop.
Shared via Rust Playground
use std::collections::HashMap;
fn create_hashmap(strand_a: &str, strand_b: &str) -> HashMap<char, char> {
let strand_a: Vec<_> = strand_a.chars().collect();
let strand_b: Vec<_> = strand_b.chars().collect();
strand_a.iter().zip(strand_b.iter()).map(|(&a, &b)| (a, b)).collect()
}
fn main() {
let strand_a = "ABCD";
let strand_b = "DCBA";
let hashmap = create_hashmap(strand_a, strand_b);
let foo: Vec<_> = "BCAD".chars().map(|c| hashmap.get(&c).unwrap()).collect();
println!("{:?}", foo);
}
@zzeroo
Copy link
Author

zzeroo commented Mar 17, 2016

use std::collections::HashMap;

const DNA_NUCLEOTIDES: &'static str = "GCTA";
const RNA_NUCLEOTIDES: &'static str = "CGAU";

#[derive(Debug, PartialEq)]
pub struct DeoxyribonucleicAcid<'a> {
    strand: &'a str,
    hashmap: HashMap<char, char>,
}

impl<'a> DeoxyribonucleicAcid<'a> {

    pub fn new(strand: &'a str) -> DeoxyribonucleicAcid {
        DeoxyribonucleicAcid {
            strand: strand,
            hashmap: create_hashmap(),
        }
    }

    pub fn to_rna(self) -> RibonucleicAcid<'a> {
        let translate = self.strand.chars().map(|c| hashmap.get(&c).unwrap())
        RibonucleicAcid::new(self.strand)
    }

}

#[derive(Debug, PartialEq)]
pub struct RibonucleicAcid<'a> {
    strand: &'a str,
}

impl<'a> RibonucleicAcid<'a> {
    pub fn new(strand: &'a str) -> RibonucleicAcid {
        RibonucleicAcid {
            strand: strand,
        }
    }
}


fn create_hashmap() -> HashMap<char, char> {
    let dna_strand: Vec<_> = DNA_NUCLEOTIDES.chars().collect();
    let rna_strand: Vec<_> = RNA_NUCLEOTIDES.chars().collect();

    dna_strand.iter().zip(rna_strand.iter()).map(|(&a, &b)| (a, b)).collect()
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment