Avatar of shmibs

shmibs's solution

to Hamming in the C Track

Published at Jul 13 2018 · 1 comment
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

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)
{
}

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

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

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

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

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

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

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

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

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

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

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

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

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);

   UnityEnd();
   return 0;
}
#include <stdlib.h>
#include <stdint.h>

#include "hamming.h"

#define ACGT 'A': case 'C': case 'G': case 'T'

int64_t compute(const char *s1,  const char *s2)
{
	char c1, c2;
	uint32_t i = 0;
	int64_t d = 0;

	if (s1 == NULL || s2 == NULL)
		return -1;

	while(1) {
		if (i == UINT32_MAX)
			return -1;

		if (d < 0)
			return -1;

		c1 = s1[i];
		c2 = s2[i];

		switch (c1) {
		case ACGT:
			if (c1 == c2) {
				i++;
				continue;
			}

			switch (c2) {
			case ACGT:
				d++;
				i++;
				continue;
			default:
				return -1;
			}
		case '\0':
			return (c2 == '\0' ? d : -1);
		default:
			return -1;
		}
	}
}

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of shmibs

little macro for a bit more code density

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?