Sha256: 946eac6618f13253a52f14785d24b0da2ddf0adadf3f242ca6923d8cd23d12f9

Contents?: true

Size: 860 Bytes

Versions: 20

Compression:

Stored size: 860 Bytes

Contents

module hamming;


import std.string;
import std.algorithm.comparison : mismatch;
import std.exception;

int distance (string lhs, string rhs)
{
    if (lhs.length != rhs.length)
    {
	throw new Exception(format("DNA sequence with different lengths: %s - %s", lhs.length, rhs.length));
    }

    int count = 0;
    auto m = mismatch(lhs, rhs);

    while (!m[0].empty)
    {
	++count;
	m = mismatch(m[0][1 .. $], m[1][1 .. $]);
    }

    return count;
}

unittest
{
    assert(distance("A", "A") == 0);
    assert(distance("A", "G") == 1);
    assert(distance("AG", "CT") == 2);
    assert(distance("AT", "CT") == 1);
    assert(distance("GGACG", "GGTCG") == 1);
    assertThrown(distance("AAAG", "AAA"));
    assertThrown(distance("AAA", "AAAG"));
    assert(distance("GATACA", "GCATAA") == 4);
    assert(distance("GGACGGATTCTG", "AGGACGGATTCT") == 9);
}

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
trackler-2.1.0.48 tracks/dlang/exercises/hamming/example/hamming.d
trackler-2.1.0.47 tracks/dlang/exercises/hamming/example/hamming.d
trackler-2.1.0.46 tracks/dlang/exercises/hamming/example/hamming.d
trackler-2.1.0.45 tracks/dlang/exercises/hamming/example/hamming.d
trackler-2.1.0.44 tracks/dlang/exercises/hamming/example/hamming.d
trackler-2.1.0.43 tracks/dlang/exercises/hamming/example/hamming.d
trackler-2.1.0.42 tracks/dlang/exercises/hamming/example/hamming.d
trackler-2.1.0.41 tracks/dlang/exercises/hamming/example/hamming.d
trackler-2.1.0.40 tracks/dlang/exercises/hamming/example/hamming.d
trackler-2.1.0.39 tracks/dlang/exercises/hamming/example/hamming.d
trackler-2.1.0.38 tracks/dlang/exercises/hamming/example/hamming.d
trackler-2.1.0.37 tracks/dlang/exercises/hamming/example/hamming.d
trackler-2.1.0.36 tracks/dlang/exercises/hamming/example/hamming.d
trackler-2.1.0.34 tracks/dlang/exercises/hamming/example/hamming.d
trackler-2.1.0.33 tracks/dlang/exercises/hamming/example/hamming.d
trackler-2.1.0.32 tracks/dlang/exercises/hamming/example/hamming.d
trackler-2.1.0.31 tracks/dlang/exercises/hamming/example/hamming.d
trackler-2.1.0.30 tracks/dlang/exercises/hamming/example/hamming.d
trackler-2.1.0.29 tracks/dlang/exercises/hamming/example/hamming.d
trackler-2.1.0.28 tracks/dlang/exercises/hamming/example/hamming.d