Created
January 14, 2016 21:46
-
-
Save gavinmcgimpsey/b18db122c091e80ca017 to your computer and use it in GitHub Desktop.
RNA Translation Exercise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class InvalidCodonError < ArgumentError; end | |
module Translation | |
def self.of_rna(string) | |
result = [] | |
tris = string.chars | |
.each_slice(3) | |
.to_a | |
.map(&:join) | |
tris.each do |tri| | |
next_codon = of_codon(tri) | |
break if next_codon == 'STOP' | |
result << next_codon | |
end | |
result | |
end | |
def self.of_codon(tri) | |
case tri | |
when 'AUG' | |
'Methionine' | |
when 'UUU', 'UUC' | |
'Phenylalanine' | |
when 'UUA', 'UUG' | |
'Leucine' | |
when 'UCU', 'UCC', 'UCA', 'UCG' | |
'Serine' | |
when 'UAU', 'UAC' | |
'Tyrosine' | |
when 'UGU', 'UGC' | |
'Cysteine' | |
when 'UGG' | |
'Tryptophan' | |
when 'UAA', 'UAG', 'UGA' | |
'STOP' | |
else | |
fail InvalidCodonError | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment