 # calebpollman's solution

## to Hamming in the TypeScript Track

Published at Sep 18 2019 · 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.

## Setup

Go through the setup instructions for TypeScript to install the necessary dependencies:

https://exercism.io/tracks/typescript/installation

## Requirements

Install assignment dependencies:

``````\$ yarn install
``````

## Making the test suite pass

Execute the tests with:

``````\$ yarn test
``````

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by changing `xit` to `it`.

## 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 can see how others have completed the exercise.

### hamming.test.ts

``````import Hamming from './hamming'

describe('Hamming', () => {
const hamming = new Hamming()

it('no difference between identical strands', () => {
expect(hamming.compute('A', 'A')).toEqual(0)
})

xit('complete hamming distance for single nucleotide strand', () => {
expect(hamming.compute('A', 'G')).toEqual(1)
})

xit('complete hamming distance for small strand', () => {
expect(hamming.compute('AG', 'CT')).toEqual(2)
})

xit('small hamming distance', () => {
expect(hamming.compute('AT', 'CT')).toEqual(1)
})

xit('small hamming distance in longer strand', () => {
expect(hamming.compute('GGACG', 'GGTCG')).toEqual(1)
})

xit('large hamming distance', () => {
expect(hamming.compute('GATACA', 'GCATAA')).toEqual(4)
})

xit('hamming distance in very long strand', () => {
expect(hamming.compute('GGACGGATTCTG', 'AGGACGGATTCT')).toEqual(9)
})

xit('throws error when strands are not equal length', () => {
expect(() => { hamming.compute('GGACGGATTCTG', 'AGGAC') }
).toThrowError('DNA strands must be of equal length.' )
})

})``````
``````class Hamming {
public compute(oldDnaStrand: string, newDnaStrand: string): number {
if (oldDnaStrand.length !== newDnaStrand.length) {
throw new Error('DNA strands must be of equal length.');
}

let diff = 0;
if (oldDnaStrand === newDnaStrand) {
return diff;
}

[...oldDnaStrand].forEach((oldDnaValue, oldDnaIndex) => {
if (oldDnaValue !== newDnaStrand[oldDnaIndex]) {
diff++;
}
});
return diff;
}
}

export default Hamming;``````