Sha256: 1c83dad6d18919ff3efacb0d02b2adf77679a95843cc6bf3b7d3d60c67ab00ca

Contents?: true

Size: 834 Bytes

Versions: 396

Compression:

Stored size: 834 Bytes

Contents

class InvalidCodonError < StandardError; end

class Translation
  def self.of_codon(codon)
    found_key = lookups.keys.find { |sequences| sequences.include?(codon) }
    fail InvalidCodonError if lookups[found_key].nil?
    lookups[found_key]
  end

  def self.of_rna(sequence)
    sequence.chars.each_slice(3).with_object([]) do |codon, output|
      return output if of_codon(codon.join) == 'STOP'
      output << of_codon(codon.join)
    end
  end

  private

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

Version data entries

396 entries across 396 versions & 1 rubygems

Version Path
trackler-2.2.1.139 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.138 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.137 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.136 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.135 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.134 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.133 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.132 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.131 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.130 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.129 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.128 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.127 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.126 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.125 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.124 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.123 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.122 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.121 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.120 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb