ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# hl4's solution

## to Hamming in the Delphi Pascal Track

Published at Dec 15 2020 · 0 comments
Instructions
Test suite
Solution

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 :)

# Implementation notes

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.

## Testing

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.

### When Questions Come Up

We monitor the Pascal-Delphi support room on gitter.im to help you with any questions that might arise.

### Submitting Exercises

Note that, when trying to submit an exercise, make sure the exercise file you're submitting is in the `exercism/delphi/<exerciseName>` directory.

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`.

## Source

The Calculating Point Mutations problem at Rosalind http://rosalind.info/problems/hamm/

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you may request help from a mentor.

### uHammingTests.pas

``````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;

{\$mode objfpc}{\$H+}

interface

uses
Classes, SysUtils;

type
THamming = class
class function Distance(a, b: string): integer;
end;

implementation

class function THamming.Distance(A, B: string): integer;
var
Index: Integer;
begin
if (Length(A) = 0) and (Length(B) = 0) then
Exit(0);

if Length(A) <> Length(B) then
raise EArgumentException.Create('error');

Result := 0;
for Index := 1 to Length(A) do
if A[Index] <> B[Index] then
Inc(Result);
end;

end.``````