Skip to content

Commit 50f2967

Browse files
committed
exercism-71 Protein translation
1 parent f7c45b3 commit 50f2967

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ solution of many challenges of [Leetcode](https://leetcode.com/), [Exercism](htt
292292
68. [Run Length Encoding](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/run_length_encoding.rb)
293293
69. [Robot Simulator](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/robot_simulator.rb)
294294
70. [Beer Song](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/beer_song.rb)
295+
71. [Protein Translation](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/protein_translation.rb)
295296

296297
<a name="leetcode"/>
297298

exercism/protein_translation.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Problem: https://exercism.org/tracks/ruby/exercises/protein-translation
2+
3+
# Solution
4+
class InvalidCodonError < StandardError; end
5+
class Translation
6+
CODON_PROTEIN_MAP = {
7+
"AUG" => "Methionine",
8+
"UUU" => "Phenylalanine",
9+
"UUC" => "Phenylalanine",
10+
"UUA" => "Leucine",
11+
"UUG" => "Leucine",
12+
"UCU" => "Serine",
13+
"UCC" => "Serine",
14+
"UCA" => "Serine",
15+
"UCG" => "Serine",
16+
"UAU" => "Tyrosine",
17+
"UAC" => "Tyrosine",
18+
"UGU" => "Cysteine",
19+
"UGC" => "Cysteine",
20+
"UGG" => "Tryptophan"
21+
}
22+
STOP_CODONS = ["UAA", "UAG", "UGA"]
23+
24+
def self.of_rna(strand)
25+
return [] if strand.empty?
26+
raise InvalidCodonError if strand.length < 3
27+
codon = strand[0, 3]
28+
return [] if STOP_CODONS.include?(codon)
29+
raise InvalidCodonError if CODON_PROTEIN_MAP[codon].nil?
30+
[CODON_PROTEIN_MAP[codon]] + of_rna(strand[3..-1])
31+
end
32+
end

0 commit comments

Comments
 (0)