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.159 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.158 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.157 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.156 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.155 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.154 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.153 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.152 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.151 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.150 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.149 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.148 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.147 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.146 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.145 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.144 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.143 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.142 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.141 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb
trackler-2.2.1.140 tracks/ruby/exercises/protein-translation/.meta/solutions/protein_translation.rb