Skip to content

Commit

Permalink
ruby/protein-translation: 1st iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
vpayno committed Oct 26, 2023
1 parent 3549254 commit f2a4bef
Show file tree
Hide file tree
Showing 9 changed files with 731 additions and 82 deletions.
1 change: 1 addition & 0 deletions ruby/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,4 @@
- [microwave](./microwave/README.md)
- [bob](./bob/README.md)
- [isbn-verifier](./isbn-verifier/README.md)
- [protein-translation](./protein-translation/README.md)
7 changes: 6 additions & 1 deletion ruby/protein-translation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,9 @@ Learn more about [protein translation on Wikipedia][protein-translation].

### Based on

Tyler Long
Tyler Long

### My Solution

- [my solution](./protein_translation.rb)
- [run-tests](./run-tests-ruby.txt)
5 changes: 5 additions & 0 deletions ruby/protein-translation/coverage/.last_run.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"result": {
"line": 100.0
}
}
75 changes: 75 additions & 0 deletions ruby/protein-translation/coverage/.resultset.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{
"test:exercism": {
"coverage": {
"/home/vpayno/git_vpayno/exercism-workspace/ruby/protein-translation/protein_translation.rb": {
"lines": [
null,
null,
null,
null,
1,
1,
null,
1,
null,
32,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
32,
null,
30,
null,
null,
1,
30,
null,
29,
29,
29,
42,
null,
42,
9,
9,
null,
null,
33,
null,
null,
29,
null,
60,
null,
null,
null,
null,
1,
1,
null,
1,
3,
3,
null,
null
]
}
},
"timestamp": 1698299820
}
}
Empty file.
42 changes: 42 additions & 0 deletions ruby/protein-translation/coverage/coverage.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version='1.0'?>
<!DOCTYPE coverage SYSTEM "http://cobertura.sourceforge.net/xml/coverage-04.dtd">
<!-- Generated by simplecov-cobertura version 2.1.0 (https://github.com/dashingrocket/simplecov-cobertura) -->
<coverage line-rate="1.0" lines-covered="23" lines-valid="23" complexity="0" version="0" timestamp="1698299820">
<sources>
<source>/home/vpayno/git_vpayno/exercism-workspace/ruby/protein-translation</source>
</sources>
<packages>
<package name="protein-translation" line-rate="1.0" complexity="0">
<classes>
<class name="protein_translation" filename="protein_translation.rb" line-rate="1.0" complexity="0">
<methods/>
<lines>
<line number="5" hits="1"/>
<line number="6" hits="1"/>
<line number="8" hits="1"/>
<line number="10" hits="32"/>
<line number="29" hits="32"/>
<line number="31" hits="30"/>
<line number="34" hits="1"/>
<line number="35" hits="30"/>
<line number="37" hits="29"/>
<line number="38" hits="29"/>
<line number="39" hits="29"/>
<line number="40" hits="42"/>
<line number="42" hits="42"/>
<line number="43" hits="9"/>
<line number="44" hits="9"/>
<line number="47" hits="33"/>
<line number="50" hits="29"/>
<line number="52" hits="60"/>
<line number="57" hits="1"/>
<line number="58" hits="1"/>
<line number="60" hits="1"/>
<line number="61" hits="3"/>
<line number="62" hits="3"/>
</lines>
</class>
</classes>
</package>
</packages>
</coverage>
69 changes: 63 additions & 6 deletions ruby/protein-translation/protein_translation.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,64 @@
=begin
Write your code for the 'Protein Translation' exercise in this file. Make the tests in
`protein_translation_test.rb` pass.
# frozen_string_literal: false

To get started with TDD, see the `README.md` file in your
`ruby/protein-translation` directory.
=end
# https://exercism.org/tracks/ruby/exercises/protein-translation
# Protein Translation exercise
module Translation
CODON_LENGTH = 3

def self.to_protein(codon)
codon2protein = {
'AUG' => 'Methionine',
'UUU' => 'Phenylalanine',
'UUC' => 'Phenylalanine',
'UUA' => 'Leucine',
'UUG' => 'Leucine',
'UCU' => 'Serine',
'UCC' => 'Serine',
'UCA' => 'Serine',
'UCG' => 'Serine',
'UAU' => 'Tyrosine',
'UAC' => 'Tyrosine',
'UGU' => 'Cysteine',
'UGC' => 'Cysteine',
'UGG' => 'Tryptophan',
'UAA' => 'STOP',
'UAG' => 'STOP',
'UGA' => 'STOP'
}

raise InvalidCodonError, 'invalid codon' unless codon2protein.key?(codon)

codon2protein[codon]
end

def self.of_rna(rna_sequence)
return [] if rna_sequence.empty?

has_stop = false
codons = []
rna_sequence.scan(/([A-Z]{3})/) do |match|
codon = match[0]

if %w[UAA UAG UGA].include?(codon)
has_stop = true
break
end

codons.push(codon)
end

raise InvalidCodonError, 'invalid codon' if !has_stop && rna_sequence.length.modulo(CODON_LENGTH).positive?

codons.map { |codon| to_protein(codon) }
end
end

# InvalidCodonError exception
class InvalidCodonError < StandardError
attr_reader :data

def initialize(data)
super
@data = data
end
end
Loading

0 comments on commit f2a4bef

Please sign in to comment.