Avatar of terminator

terminator's solution

to Hamming in the C++ Track

Published at Oct 16 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.

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.

#if defined(EXERCISM_RUN_ALL_TESTS)

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.

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

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

TEST_CASE("no_difference_between_identical_strands")
{
    REQUIRE(0 == hamming::compute("A", "A"));
}

#if defined(EXERCISM_RUN_ALL_TESTS)
TEST_CASE("complete_hamming_distance_for_single_nucleotide_strand")
{
    REQUIRE(1 == hamming::compute("A", "G"));
}

TEST_CASE("complete_hamming_distance_for_small_strand")
{
    REQUIRE(2 == hamming::compute("AG", "CT"));
}

TEST_CASE("small_hamming_distance")
{
    REQUIRE(1 == hamming::compute("AT", "CT"));
}

TEST_CASE("small_hamming_distance_in_longer_strand")
{
    REQUIRE(1 == hamming::compute("GGACG", "GGTCG"));
}

TEST_CASE("domain_error_when_first_string_is_longer")
{
    REQUIRE_THROWS_AS(hamming::compute("AAAG", "AAA"), std::domain_error);
}

TEST_CASE("domain_error_when_second_string_is_longer")
{
    REQUIRE_THROWS_AS(hamming::compute("AAA", "AAAG"), std::domain_error);
}

TEST_CASE("large_hamming_distance")
{
    REQUIRE(4 == hamming::compute("GATACA", "GCATAA"));
}

TEST_CASE("hamming_distance_in_very_long_strand")
{
    REQUIRE(9 == hamming::compute("GGACGGATTCTG", "AGGACGGATTCT"));
}
#endif

hamming.h

#if !defined(HAMMING_H)
#define HAMMING_H

#include <string>

namespace hamming {

int compute(std::string s1, std::string s2);

}  // namespace hamming

#endif // HAMMING_H

hamming.cpp

#include "hamming.h"
#include <stdexcept>

namespace hamming {

int compute(std::string s1, std::string s2)
{
    if (s1.length() == s2.length())
    {
        int count = 0;

        for (std::string::size_type i = 0; i < s1.size(); ++i)
        {
            if (s1[i] != s2[i])
            {
                ++count;
            }
        }
        return count;
    }
    else
    {
        throw std::domain_error("Different length not comparable");
        return 0;
    }
    
}

}  // namespace hamming

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?