Calculate the Hamming Distance between two DNA strands.
Your body is made up of cells that contain DNA. Those cells regularly wear out and need replacing, which they achieve by dividing into daughter cells. In fact, the average human body experiences about 10 quadrillion cell divisions in a lifetime!
When cells divide, their DNA replicates too. Sometimes during this process mistakes happen and single pieces of DNA get encoded with the incorrect information. If we compare two strands of DNA and count the differences between them we can see how many mistakes occurred. This is known as the "Hamming Distance".
We read DNA using the letters C,A,G and T. Two strands might look like this:
GAGCCTACTAACGGGAT CATCGTAATGACGGCCT ^ ^ ^ ^ ^ ^^
They have 7 differences, and therefore the Hamming Distance is 7.
The Hamming Distance is useful for lots of things in science, not just biology, so it's a nice phrase to be familiar with :)
The Hamming distance is only defined for sequences of equal length, so an attempt to calculate it between sequences of different lengths should not work. The general handling of this situation (e.g., raising an exception vs returning a special value) may differ between languages.
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.
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 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
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.
The Calculating Point Mutations problem at Rosalind http://rosalind.info/problems/hamm/
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
(ql:quickload "lisp-unit") #-xlisp-test (load "hamming") (defpackage #:hamming-test (:use #:common-lisp #:lisp-unit)) (in-package #:hamming-test) (define-test no-difference-between-empty-strands (assert-equal 0 (hamming:distance "" ""))) (define-test no-difference-between-identical-strands (assert-equal 0 (hamming:distance "GGACTGA" "GGACTGA"))) (define-test complete-hamming-distance-in-small-strand (assert-equal 3 (hamming:distance "ACT" "GGA"))) (define-test small-hamming-distance-in-middle-somewhere (assert-equal 1 (hamming:distance "GGACG" "GGTCG"))) (define-test larger-distance (assert-equal 2 (hamming:distance "ACCAGGG" "ACTATGG"))) (define-test invalid-to-get-distance-for-different-length-strings (assert-equal nil (hamming:distance "AGACAACAGCCAGCCGCCGGATT" "AGGCAA")) (assert-equal nil (hamming:distance "AGACAACAGCCAGCCGCCGGATT" "AGACATCTTTCAGCCGCCGGATTAGGCAA")) (assert-equal nil (hamming:distance "AGG" "AGACAACAGCCAGCCGCCGGATT"))) #-xlisp-test (let ((*print-errors* t) (*print-failures* t)) (run-tests :all :hamming-test))
(defpackage #:hamming (:use #:cl) (:export #:distance)) (in-package #:hamming) (defun distance (dna1 dna2) "Number of positional differences in two equal length dna strands." (when (equal (length dna1) (length dna2)) (let ((x 0)) (dotimes (i (length dna1)) (unless (equal (aref dna1 i) (aref dna2 i)) (incf x))) x)))
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.