Avatar of einheitsvektor

einheitsvektor's solution

to Hamming in the C Track

Published at Aug 30 2019 · 0 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution was written.

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.

GAGCCTACTAACGGGAT
CATCGTAATGACGGCCT
^ ^ ^  ^ ^    ^^

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.

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

src/hamming.c

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

int compute(char *a, char *b) {
    if ((a == NULL && b != NULL) || (a != NULL && b == NULL))
        return -1;

    //compute strlen of both strings
    int len_a = 0, len_b = 0;
    for (int i = 0; a[i] != '\0'; i++)
        len_a++;
    for (int i = 0; b[i] != '\0'; i++)
        len_b++;

    // both strings must have equal length
    if (len_a != len_b)
        return -1;

    int count = 0;
    //at this point it doesn't matter which boundary check
    for (int i = 0; i < len_a; i++)
        if (a[i] != b[i])
            count++;
    return count;
}

src/hamming.h

#ifndef HAMMING_H
#define HAMMING_H

int compute(char *astrand, char *bstrand);

#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?