🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of ballan

ballan's solution

to RNA Transcription in the Vim script Track

Published at Mar 20 2020 · 0 comments
Instructions
Test suite
Solution

Given a DNA strand, return 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

How to run the tests

If you don't know how to run Vader tests, see: Tests.

For general information about the Vim script track, see: Help.

Source

Hyperphysics http://hyperphysics.phy-astr.gsu.edu/hbase/Organic/transcription.html

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

rna_transcription.vader

"
" Version: 1.0.1
"

Before:
  unlet! input expected

Execute (RNA complement of cytosine is guanine):
  let input = "C"
  let expected = "G"
  AssertEqual expected, ToRna(input)

Execute (RNA complement of guanine is cytosine):
  let input = "G"
  let expected = "C"
  AssertEqual expected, ToRna(input)

Execute (RNA complement of thymine is adenine):
  let input = "T"
  let expected = "A"
  AssertEqual expected, ToRna(input)

Execute (RNA complement of adenine is uracil):
  let input = "A"
  let expected = "U"
  AssertEqual expected, ToRna(input)

Execute (RNA complement):
  let input = "ACGTGGTCTTAA"
  let expected = "UGCACCAGAAUU"
  AssertEqual expected, ToRna(input)

Execute (correctly handles invalid input (RNA instead of DNA)):
  let input = "U"
  let expected = ''
  AssertEqual expected, ToRna(input)

Execute (correctly handles completely invalid DNA input):
  let input = "XXX"
  let expected = ''
  AssertEqual expected, ToRna(input)

Execute (correctly handles partially invalid DNA input):
  let input = "ACGTXXXCTTAA"
  let expected = ''
  AssertEqual expected, ToRna(input)
"
" This function takes a DNA strand and returns its RNA complement.
"
"   G -> C
"   C -> G
"   T -> A
"   A -> U
"
" If the input is invalid, return an empty string.
"
" Example:
"
"   :echo ToRna('ACGTGGTCTTAA')
"   UGCACCAGAAUU
"
function! ToRna(strand) abort
  " Return empty string for invalid input
  if ( a:strand =~# '[^GCTA]' )
    return ''
  endif

  return tr(a:strand, 'GCTA', 'CGAU')
endfunction

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?