🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of danieljpost

danieljpost's solution

to Hamming in the PHP Track

Published at Nov 07 2019 · 0 comments
Test suite

Calculate the Hamming difference between two DNA strands.

A mutation is simply a mistake that occurs during the creation or copying of a nucleic acid, in particular DNA. Because nucleic acids are vital to cellular functions, mutations tend to cause a ripple effect throughout the cell. Although mutations are technically mistakes, a very rare mutation may equip the cell with a beneficial attribute. In fact, the macro effects of evolution are attributable by the accumulated result of beneficial microscopic mutations over many generations.

The simplest and most common type of nucleic acid mutation is a point mutation, which replaces one base with another at a single nucleotide.

By counting the number of differences between two homologous DNA strands taken from different genomes with a common ancestor, we get a measure of the minimum number of point mutations that could have occurred on the evolutionary path between the two strands.

This is called the 'Hamming distance'.

It is found by comparing two DNA strands and counting how many of the nucleotides are different from their equivalent in the other string.

^ ^ ^  ^ ^    ^^

The Hamming distance between these two DNA strands is 7.

Implementation notes

The Hamming distance is only defined for sequences of equal length. This means that based on the definition, each language could deal with getting sequences of equal length differently.


Don't solve this with the gmp_hamdist function! Solve this one yourself using other basic tools instead.

Running the tests

  1. Go to the root of your PHP exercise directory, which is <EXERCISM_WORKSPACE>/php. To find the Exercism workspace run

     % exercism debug | grep Workspace
  2. Get PHPUnit if you don't have it already.

     % wget --no-check-certificate https://phar.phpunit.de/phpunit.phar
     % chmod +x phpunit.phar
  3. Execute the tests:

     % ./phpunit.phar hamming/hamming_test.php


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.



class HammingComparatorTest extends PHPUnit\Framework\TestCase
    public static function setUpBeforeClass() : void
        require_once 'hamming.php';

    public function testNoDifferenceBetweenIdenticalStrands() : void
        $this->assertEquals(0, distance('A', 'A'));

    public function testCompleteHammingDistanceOfForSingleNucleotideStrand() : void
        $this->assertEquals(1, distance('A', 'G'));

    public function testCompleteHammingDistanceForSmallStrand() : void
        $this->assertEquals(2, distance('AG', 'CT'));

    public function testSmallHammingDistance() : void
        $this->assertEquals(1, distance('AT', 'CT'));

    public function testSmallHammingDistanceInLongerStrand() : void
        $this->assertEquals(1, distance('GGACG', 'GGTCG'));

    public function testLargeHammingDistance() : void
        $this->assertEquals(4, distance('GATACA', 'GCATAA'));

    public function testHammingDistanceInVeryLongStrand() : void
        $this->assertEquals(9, distance('GGACGGATTCTG', 'AGGACGGATTCT'));

    public function testExceptionThrownWhenStrandsAreDifferentLength() : void
        $this->expectExceptionMessage('DNA strands must be of equal length.');
        distance('GGACG', 'AGGACGTGG');

 * @param string $strandA
 * @param string $strandB
 * @throws InvalidArgumentException
function distance(string $strandA, string $strandB) : int
    if (strlen($strandA) !== strlen($strandB)) {
        throw new InvalidArgumentException('DNA strands must be of equal length.');
    $out = 0;
    for ($i=0, $n=strlen($strandA); $i<$n; $i++) {
        if ($strandA[$i] !== $strandB[$i]) {
    return $out;

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?