summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2016-08-28 02:29:13 +0200
committerDimitri Sokolyuk <demon@dim13.org>2016-08-28 02:29:13 +0200
commitf43656022928caaf5a9882d93ffdf64eb069fbab (patch)
tree13c4ee48c2547a2ffa8cb848646fa0190ee84110
parentc81284ad40511252401023ef238e3a6f47120aa2 (diff)
Solve RNA
-rw-r--r--go/rna-transcription/README.md37
-rw-r--r--go/rna-transcription/cases_test.go24
-rw-r--r--go/rna-transcription/rna_transcription.go23
-rw-r--r--go/rna-transcription/rna_transcription_test.go25
4 files changed, 109 insertions, 0 deletions
diff --git a/go/rna-transcription/README.md b/go/rna-transcription/README.md
new file mode 100644
index 0000000..9bec30c
--- /dev/null
+++ b/go/rna-transcription/README.md
@@ -0,0 +1,37 @@
+# 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 thymine (**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`
+
+To run the tests simply run the command `go test` in the exercise directory.
+
+If the test suite contains benchmarks, you can run these with the `-bench`
+flag:
+
+ go test -bench .
+
+For more detailed info about the Go track see the [help
+page](http://exercism.io/languages/go).
+
+## Source
+
+Rosalind [http://rosalind.info/problems/rna](http://rosalind.info/problems/rna)
+
+## Submitting Incomplete Problems
+It's possible to submit an incomplete solution so you can see how others have completed the exercise.
+
diff --git a/go/rna-transcription/cases_test.go b/go/rna-transcription/cases_test.go
new file mode 100644
index 0000000..156f70d
--- /dev/null
+++ b/go/rna-transcription/cases_test.go
@@ -0,0 +1,24 @@
+package strand
+
+// Source: exercism/x-common
+// Commit: 6985644 Merge pull request #121 from mikeyjcat/add-roman-numerals-test-definition
+
+var rnaTests = []struct {
+ input string
+ expected string
+}{
+ // rna complement of cytosine is guanine
+ {"C", "G"},
+
+ // rna complement of guanine is cytosine
+ {"G", "C"},
+
+ // rna complement of thymine is adenine
+ {"T", "A"},
+
+ // rna complement of adenine is uracil
+ {"A", "U"},
+
+ // rna complement
+ {"ACGTGGTCTTAA", "UGCACCAGAAUU"},
+}
diff --git a/go/rna-transcription/rna_transcription.go b/go/rna-transcription/rna_transcription.go
new file mode 100644
index 0000000..410b487
--- /dev/null
+++ b/go/rna-transcription/rna_transcription.go
@@ -0,0 +1,23 @@
+package strand
+
+const testVersion = 3
+
+// ToRNA a little bit faster than map solution
+func ToRNA(s string) string {
+ r := make([]rune, len(s))
+ for i, v := range s {
+ switch v {
+ case 'G':
+ r[i] = 'C'
+ case 'C':
+ r[i] = 'G'
+ case 'T':
+ r[i] = 'A'
+ case 'A':
+ r[i] = 'U'
+ default:
+ r[i] = v
+ }
+ }
+ return string(r)
+}
diff --git a/go/rna-transcription/rna_transcription_test.go b/go/rna-transcription/rna_transcription_test.go
new file mode 100644
index 0000000..4d4ca8f
--- /dev/null
+++ b/go/rna-transcription/rna_transcription_test.go
@@ -0,0 +1,25 @@
+package strand
+
+import "testing"
+
+const targetTestVersion = 3
+
+func TestRNATranscription(t *testing.T) {
+ if testVersion != targetTestVersion {
+ t.Fatalf("Found testVersion = %v, want %v", testVersion, targetTestVersion)
+ }
+ for _, test := range rnaTests {
+ if actual := ToRNA(test.input); actual != test.expected {
+ t.Errorf("ToRNA(%s): %s, expected %s",
+ test.input, actual, test.expected)
+ }
+ }
+}
+
+func BenchmarkRNATranscription(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ for _, test := range rnaTests {
+ ToRNA(test.input)
+ }
+ }
+}