Skip to content

Instantly share code, notes, and snippets.

@gavinmcgimpsey
Created January 14, 2016 21:46
Show Gist options
  • Save gavinmcgimpsey/b18db122c091e80ca017 to your computer and use it in GitHub Desktop.
Save gavinmcgimpsey/b18db122c091e80ca017 to your computer and use it in GitHub Desktop.
RNA Translation Exercise
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