Avatar of cmourglia

cmourglia's solution

to Hamming in the C++ Track

Published at Oct 19 2019 · 0 comments
Test suite

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:

^ ^ ^  ^ ^    ^^

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.

Getting Started

Make sure you have read the Installing and Running the Tests pages for C++ on exercism.io. This covers the basic information on setting up the development environment expected by the exercises.

Passing the Tests

Get the first test compiling, linking and passing by following the three rules of test-driven development. Create just enough structure by declaring namespaces, functions, classes, etc., to satisfy any compiler errors and get the test to fail. Then write just enough code to get the test to pass. Once you've done that, uncomment the next test by moving the following line past the next test.


This may result in compile errors as new constructs may be invoked that you haven't yet declared or defined. Again, fix the compile errors minimally to get a failing test, then change the code minimally to pass the test, refactor your implementation for readability and expressiveness and then go on to the next test.

Try to use standard C++14 facilities in preference to writing your own low-level algorithms or facilities by hand. CppReference is a wiki reference to the C++ language and standard library. If you are new to C++, but have programmed in C, beware of C traps and pitfalls.


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.


#include "hamming.h"
#include <stdexcept>
#include "test/catch.hpp"

    REQUIRE(0 == hamming::compute("A", "A"));

    REQUIRE(1 == hamming::compute("A", "G"));

    REQUIRE(2 == hamming::compute("AG", "CT"));

    REQUIRE(1 == hamming::compute("AT", "CT"));

    REQUIRE(1 == hamming::compute("GGACG", "GGTCG"));

    REQUIRE_THROWS_AS(hamming::compute("AAAG", "AAA"), std::domain_error);

    REQUIRE_THROWS_AS(hamming::compute("AAA", "AAAG"), std::domain_error);

    REQUIRE(4 == hamming::compute("GATACA", "GCATAA"));

    REQUIRE(9 == hamming::compute("GGACGGATTCTG", "AGGACGGATTCT"));


#include "hamming.h"

#include <exception>

namespace hamming {

int compute(const std::string& a, const std::string& b)
    if (a.size() != b.size()) {
        throw std::domain_error("Different sizes");

    int hamming_distance = 0;
    for (size_t i = 0; i < a.size(); ++i) {
        if (a[i] != b[i]) ++hamming_distance;

    return hamming_distance;

}  // namespace hamming


#if !defined(HAMMING_H)
#define HAMMING_H

#include <string>

namespace hamming {
    int compute(const std::string& a, const std::string& b);
}  // namespace hamming

#endif // HAMMING_H

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?