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.180 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.179 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.178 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.177 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.176 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.175 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.174 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.173 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.172 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.171 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.170 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.169 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.167 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.166 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.165 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.164 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.163 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.162 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.161 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.160 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb