Permalink
Browse files

First passing code for complement.

There is duplication here, both of behavior and of knowledge.

Duplication of behavior is code approaches that look the same, like the
case statements in the `find_dna` and `find_rna` methods. Those look the
same, so extraction seems beneficial. But of the 4 case options, only 2
are shared between the methods. So extraction might make things more
difficult.

Duplication of knowledge is in the two public API methods. They both
expect a string and then they are going to return its complements. They
both know how clients are going to use this code. That duplicated
knowledge seems like a good thing to dry up
  • Loading branch information...
Ian Whitney
Ian Whitney committed Dec 24, 2014
1 parent 17021aa commit 41e206b405e242fd166b3ade7448385f8f34de52
Showing with 105 additions and 0 deletions.
  1. +22 −0 ruby/rna-transcription/README.md
  2. +39 −0 ruby/rna-transcription/complement.rb
  3. +44 −0 ruby/rna-transcription/complement_test.rb
@@ -0,0 +1,22 @@
# Rna Transcription
Write a program that, given a DNA strand, returns its RNA complement (per RNA transcription).
Both DNA and RNA strands are a sequence of nucleotides.
The four nucleotides found in DNA are adenine (**A**), cytosine (**C**), guanine (**G**) and thymidine (**T**).
The four nucleotides found in RNA are adenine (**A**), cytosine (**C**), guanine (**G**) and uracil (**U**).
Given a DNA strand, its transcribed RNA strand is formed by replacing each nucleotide with its complement:
* `G` -> `C`
* `C` -> `G`
* `T` -> `A`
* `A` -> `U`
## Source
Rosalind [view source](http://rosalind.info/problems/rna)
@@ -0,0 +1,39 @@
class Complement
def self.of_dna(strand)
ret = ""
strand.each_char { |x| ret << find_dna_complement_of(x) }
ret
end
def self.of_rna(strand)
ret = ""
strand.each_char { |x| ret << find_rna_complement_of(x) }
ret
end
def self.find_dna_complement_of(nucleotide)
case nucleotide
when 'C'
'G'
when 'G'
'C'
when 'T'
'A'
when 'A'
'U'
end
end
def self.find_rna_complement_of(nucleotide)
case nucleotide
when 'C'
'G'
when 'G'
'C'
when 'U'
'A'
when 'A'
'T'
end
end
end
@@ -0,0 +1,44 @@
require 'minitest/autorun'
require_relative 'complement'
class ComplementTest < MiniTest::Unit::TestCase
def test_rna_complement_of_cytosine_is_guanine
assert_equal 'G', Complement.of_dna('C')
end
def test_rna_complement_of_guanine_is_cytosine
assert_equal 'C', Complement.of_dna('G')
end
def test_rna_complement_of_thymine_is_adenine
assert_equal 'A', Complement.of_dna('T')
end
def test_rna_complement_of_adenine_is_uracil
assert_equal 'U', Complement.of_dna('A')
end
def test_rna_complement
assert_equal 'UGCACCAGAAUU', Complement.of_dna('ACGTGGTCTTAA')
end
def test_dna_complement_of_cytosine_is_guanine
assert_equal 'G', Complement.of_rna('C')
end
def test_dna_complement_of_guanine_is_cytosine
assert_equal 'C', Complement.of_rna('G')
end
def test_dna_complement_of_uracil_is_adenine
assert_equal 'A', Complement.of_rna('U')
end
def test_dna_complement_of_adenine_is_thymine
assert_equal 'T', Complement.of_rna('A')
end
def test_dna_complement
assert_equal 'ACTTGGGCTGTAC', Complement.of_rna('UGAACCCGACAUG')
end
end

0 comments on commit 41e206b

Please sign in to comment.