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.
In order to run the tests for this track, you will need to install DUnitX. Please see the installation instructions for more information.
If Delphi is properly installed, and
*.dpr file types have been associated with Delphi, then double clicking the supplied
*.dpr file will start Delphi and load the exercise/project.
control + F9 is the keyboard shortcut to compile the project or pressing
F9 will compile and run the project.
Alternatively you may opt to start Delphi and load your project via. the
File drop down menu.
Note that, when trying to submit an exercise, make sure the exercise file you're submitting is in the
For example, if you're submitting
ubob.pas for the Bob exercise, the submit command would be something like
exercism submit <path_to_exercism_dir>/delphi/bob/ubob.pas.
The Calculating Point Mutations problem at Rosalind http://rosalind.info/problems/hamm/
It's possible to submit an incomplete solution so you may request help from a mentor.
unit uHammingTests; interface uses DUnitX.TestFramework; const CanonicalVersion = '2.3.0'; type [TestFixture] HammingTests = class(TObject) public [Test] // [Ignore('Comment the "[Ignore]" statement to run the test')] procedure empty_strands; [Test] [Ignore] procedure single_letter_identical_strands; [Test] [Ignore] procedure single_letter_different_strands; [Test] [Ignore] procedure long_identical_strands; [Test] [Ignore] procedure long_different_strands; [Test] [Ignore] procedure disallow_first_strand_longer; [Test] [Ignore] procedure disallow_second_strand_longer; [Test] [Ignore] procedure disallow_left_empty_strand; [Test] [Ignore] procedure disallow_right_empty_strand; end; implementation uses System.SysUtils, uHamming; procedure HammingTests.empty_strands; begin Assert.AreEqual(0,THamming.Distance('','')); end; procedure HammingTests.single_letter_identical_strands; begin Assert.AreEqual(0,THamming.Distance('A','A')); end; procedure HammingTests.single_letter_different_strands; begin Assert.AreEqual(1, THamming.Distance('G', 'T')); end; procedure HammingTests.long_identical_strands; begin Assert.AreEqual( 0, THamming.Distance('GGACTGAAATCTG', 'GGACTGAAATCTG')); end; procedure HammingTests.long_different_strands; begin Assert.AreEqual(9, THamming.Distance('GGACGGATTCTG', 'AGGACGGATTCT')); end; procedure HammingTests.disallow_first_strand_longer; var MyProc: TTestLocalMethod; begin MyProc := procedure begin THamming.Distance('AATG', 'AAA'); end; Assert.WillRaiseWithMessage(MyProc, EArgumentException, 'error: left and right strands must be of equal length'); end; procedure HammingTests.disallow_left_empty_strand; var MyProc: TTestLocalMethod; begin MyProc := procedure begin THamming.Distance('', 'G'); end; Assert.WillRaiseWithMessage(MyProc, EArgumentException, 'error: left strand must not be empty'); end; procedure HammingTests.disallow_right_empty_strand; var MyProc: TTestLocalMethod; begin MyProc := procedure begin THamming.Distance('G', ''); end; Assert.WillRaiseWithMessage(MyProc, EArgumentException, 'error: right strand must not be empty'); end; procedure HammingTests.disallow_second_strand_longer; var MyProc: TTestLocalMethod; begin MyProc := procedure begin THamming.Distance('ATA', 'AGTG'); end; Assert.WillRaiseWithMessage(MyProc, EArgumentException, 'error: left and right strands must be of equal length'); end; initialization TDUnitX.RegisterTestFixture(HammingTests); end.
unit uHamming; interface type THamming = class(TInterfacedObject) public class function Distance(dnaSeq1: String; dnaSeq2: String): Integer; end; implementation uses SysUtils; const ERROR_MESSAGE = 'The DNA sequences must be the same length.'; class function THamming.Distance(dnaSeq1: string; dnaSeq2: string): Integer; var dnaIndex: Integer; hammingDistance: Integer; begin if dnaSeq1.Length <> dnaSeq2.Length then raise Exception.Create(ERROR_MESSAGE); hammingDistance := 0; for dnaIndex := Low(dnaSeq1) to High(dnaSeq1) do if dnaSeq1[dnaIndex] <> dnaSeq2[dnaIndex] then Inc(hammingDistance, 1); result := hammingDistance; end; end.
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.