Avatar of michaelkpfeifer

michaelkpfeifer's solution

to Hamming in the C Track

Published at Sep 15 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 "Guides" section of the C track on the Exercism site. 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.

The included makefile can be used to create and run the tests using the test task.

make test

Create just the functions you need 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, move onto the next test.

As you progress through the tests, take the time to refactor your implementation for readability and expressiveness and then go on to the next test.

Try to use standard C99 facilities in preference to writing your own low-level algorithms or facilities by hand.

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.

test_hamming.c

#include "vendor/unity.h"
#include "../src/hamming.h"

void setUp(void)
{
}

void tearDown(void)
{
}

static void test_empty_strands(void)
{
   TEST_ASSERT_EQUAL(0, compute("", ""));
}

static void test_rejects_null_strand(void)
{
   TEST_IGNORE();               // delete this line to run test
   TEST_ASSERT_EQUAL(-1, compute(NULL, "A"));
}

static void test_rejects_other_null_strand(void)
{
   TEST_IGNORE();
   TEST_ASSERT_EQUAL(-1, compute("A", NULL));
}

static void test_no_difference_between_identical_strands(void)
{
   TEST_IGNORE();
   TEST_ASSERT_EQUAL(0, compute("A", "A"));
}

static void test_identical_long_strands(void)
{
   TEST_IGNORE();
   TEST_ASSERT_EQUAL(0, compute("GGACTGA", "GGACTGA"));
}

static void test_hamming_distance_for_single_nucleotide_strand(void)
{
   TEST_IGNORE();
   TEST_ASSERT_EQUAL(1, compute("A", "G"));
}

static void test_complete_hamming_distance_for_small_strand(void)
{
   TEST_IGNORE();
   TEST_ASSERT_EQUAL(2, compute("AG", "CT"));
}

static void test_small_hamming_distance(void)
{
   TEST_IGNORE();
   TEST_ASSERT_EQUAL(1, compute("AT", "CT"));
}

static void test_small_hamming_distance_in_longer_strand(void)
{
   TEST_IGNORE();
   TEST_ASSERT_EQUAL(1, compute("GGACG", "GGTCG"));
}

static void test_rejects_extra_length_on_first_strand_when_longer(void)
{
   TEST_IGNORE();
   TEST_ASSERT_EQUAL(-1, compute("AAAG", "AAA"));
}

static void test_rejects_extra_length_on_other_strand_when_longer(void)
{
   TEST_IGNORE();
   TEST_ASSERT_EQUAL(-1, compute("AAA", "AAAG"));
}

static void test_large_hamming_distance(void)
{
   TEST_IGNORE();
   TEST_ASSERT_EQUAL(4, compute("GATACA", "GCATAA"));
}

static void test_hamming_distance_in_very_long_strand(void)
{
   TEST_IGNORE();
   TEST_ASSERT_EQUAL(9, compute("GGACGGATTCTG", "AGGACGGATTCT"));
}

int main(void)
{
   UnityBegin("test/test_hamming.c");

   RUN_TEST(test_empty_strands);
   RUN_TEST(test_no_difference_between_identical_strands);
   RUN_TEST(test_rejects_null_strand);
   RUN_TEST(test_rejects_other_null_strand);
   RUN_TEST(test_identical_long_strands);
   RUN_TEST(test_hamming_distance_for_single_nucleotide_strand);
   RUN_TEST(test_complete_hamming_distance_for_small_strand);
   RUN_TEST(test_small_hamming_distance);
   RUN_TEST(test_small_hamming_distance_in_longer_strand);
   RUN_TEST(test_rejects_extra_length_on_first_strand_when_longer);
   RUN_TEST(test_rejects_extra_length_on_other_strand_when_longer);
   RUN_TEST(test_large_hamming_distance);
   RUN_TEST(test_hamming_distance_in_very_long_strand);

   return UnityEnd();
}

src/hamming.c

#include "hamming.h"

int compute(const char *lhs, const char *rhs) {
  if (lhs == NULL || rhs == NULL) {
    return INVALID_INPUT;
  }

  int diffct = 0;

  while (*lhs != '\0' && *rhs != '\0') {
    if (*lhs != *rhs) {
      diffct++;
    }

    lhs++;
    rhs++;
  }

  if (*lhs != '\0' || *rhs != '\0') {
    return INVALID_INPUT;
  }

  return diffct;
}

src/hamming.h

#ifndef HAMMING_H
#define HAMMING_H

#include <string.h>

#define INVALID_INPUT -1

int compute(const char *lhs, const char *rhs);

#endif

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?