Avatar of fmozturk

fmozturk's solution

to RNA Transcription in the Common Lisp Track

Published at Apr 01 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

Setup

Check out Installing Common Lisp for instructions to get started or take a look at the guides available in the track's side bar.

Formatting

While Common Lisp doesn't care about indentation and layout of code, nor whether you use spaces or tabs, this is an important consideration for submissions to exercism.io. Excercism.io's code widget cannot handle mixing of tab and space characters well so using only spaces is recommended to make the code more readable to the human reviewers. Please review your editors settings on how to accomplish this. Below are instructions for popular editors for Common Lisp.

VIM

Use the following commands to ensure VIM uses only spaces for indentation:

:set tabstop=2
:set shiftwidth=2
:set expandtab

(or as a oneliner :set tabstop=2 shiftwidth=2 expandtab). This can be added to your ~/.vimrc file to use it all the time.

Emacs

Emacs is very well suited for editing Common Lisp and has many powerful add-on packages available. The only thing that one needs to do with a stock emacs to make it work well with exercism.io is to evaluate the following code:

(setq-default indent-tabs-mode nil)

This can be placed in your ~/.emacs (or ~/.emacs.d/init.el) in order to have it set whenever Emacs is launched.

One suggested add-on for Emacs and Common Lisp is SLIME which offers tight integration with the REPL; making iterative coding and testing very easy.

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-test.lisp

(ql:quickload "lisp-unit")
#-xlisp-test (load "rna-transcription")

(defpackage :rna-transcription-test
  (:use #:common-lisp #:lisp-unit))

(in-package #:rna-transcription-test)

(define-test transcribes-cytidine-to-guanosine
  (assert-equal "G" (rna-transcription:to-rna "C")))

(define-test transcribes-guanosine-to-cytidine
  (assert-equal "C" (rna-transcription:to-rna "G")))

(define-test transcribes-adenosine-to-uracile
  (assert-equal "U" (rna-transcription:to-rna "A")))

(define-test it-transcribes-thymidine-to-adenosine
  (assert-equal "A" (rna-transcription:to-rna "T")))

(define-test it-transcribes-all-nucleotides
  (assert-equal "UGCACCAGAAUU" (rna-transcription:to-rna "ACGTGGTCTTAA")))

(define-test it-validates-dna-strands
  (assert-error 'error (rna-transcription:to-rna "XCGFGGTDTTAA")))

#-xlisp-test
(let ((*print-errors* t)
      (*print-failures* t))
  (run-tests :all :rna-transcription-test))
(in-package #:cl-user)
(defpackage #:rna-transcription
  (:use #:cl)
  (:export #:to-rna))
(in-package #:rna-transcription)

(defun replace-nucleotide (nucleotide)
  "Convert G -> C, C -> G, T -> A, A -> U"
  (cond ((string= nucleotide "G") "C")
        ((string= nucleotide "C") "G")
        ((string= nucleotide "T") "A")
        ((string= nucleotide "A") "U")
        (t 'error)))

(defun to-rna (str)
  "Transcribe a string representing DNA nucleotides to RNA."
  (let ((result ""))
    (loop for index from 0 to (- (length str) 1)
       do
         (setf result
               (concatenate 'string
                            result
                            (replace-nucleotide (aref str index)))))
    result)
  )

Community comments

Find this solution interesting? Ask the author a question to learn more.

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?