Skip to content

Commit 63b60e4

Browse files
sync hamming (#569)
1 parent 4136665 commit 63b60e4

File tree

7 files changed

+101
-64
lines changed

7 files changed

+101
-64
lines changed

exercises/practice/hamming/.docs/instructions.md

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,15 @@
11
# Instructions
22

3-
Calculate the Hamming Distance between two DNA strands.
3+
Calculate the Hamming distance between two DNA strands.
44

5-
Your body is made up of cells that contain DNA.
6-
Those cells regularly wear out and need replacing, which they achieve by dividing into daughter cells.
7-
In fact, the average human body experiences about 10 quadrillion cell divisions in a lifetime!
8-
9-
When cells divide, their DNA replicates too.
10-
Sometimes during this process mistakes happen and single pieces of DNA get encoded with the incorrect information.
11-
If we compare two strands of DNA and count the differences between them we can see how many mistakes occurred.
12-
This is known as the "Hamming Distance".
13-
14-
We read DNA using the letters C,A,G and T.
5+
We read DNA using the letters C, A, G and T.
156
Two strands might look like this:
167

178
GAGCCTACTAACGGGAT
189
CATCGTAATGACGGCCT
1910
^ ^ ^ ^ ^ ^^
2011

21-
They have 7 differences, and therefore the Hamming Distance is 7.
22-
23-
The Hamming Distance is useful for lots of things in science, not just biology, so it's a nice phrase to be familiar with :)
12+
They have 7 differences, and therefore the Hamming distance is 7.
2413

2514
## Implementation notes
2615

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Introduction
2+
3+
Your body is made up of cells that contain DNA.
4+
Those cells regularly wear out and need replacing, which they achieve by dividing into daughter cells.
5+
In fact, the average human body experiences about 10 quadrillion cell divisions in a lifetime!
6+
7+
When cells divide, their DNA replicates too.
8+
Sometimes during this process mistakes happen and single pieces of DNA get encoded with the incorrect information.
9+
If we compare two strands of DNA and count the differences between them, we can see how many mistakes occurred.
10+
This is known as the "Hamming distance".
11+
12+
The Hamming distance is useful in many areas of science, not just biology, so it's a nice phrase to be familiar with :)

exercises/practice/hamming/.meta/config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
".meta/example.lua"
2121
]
2222
},
23-
"blurb": "Calculate the Hamming difference between two DNA strands.",
23+
"blurb": "Calculate the Hamming distance between two DNA strands.",
2424
"source": "The Calculating Point Mutations problem at Rosalind",
2525
"source_url": "https://rosalind.info/problems/hamm/"
2626
}

exercises/practice/hamming/.meta/example.lua

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
local Hamming = {}
22

33
function Hamming.compute(a, b)
4-
if #a ~= #b then
5-
return -1
6-
end
4+
assert(#a == #b, 'strands must be of equal length')
75
local distance = 0
86
for i = 1, a:len() do
97
if (a:sub(i, i) ~= b:sub(i, i)) then
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
return {
2+
module_name = 'hamming',
3+
4+
generate_test = function(case)
5+
if type(case.expected) == 'number' then
6+
local template = [[
7+
assert.are.equal(%s, hamming.compute("%s", "%s"))]]
8+
return template:format(case.expected, case.input.strand1, case.input.strand2)
9+
else
10+
local template = [[
11+
assert.has_error(function()
12+
hamming.compute("%s", "%s")
13+
end, "%s")]]
14+
return template:format(case.input.strand1, case.input.strand2, case.expected.error)
15+
end
16+
end
17+
}

exercises/practice/hamming/.meta/tests.toml

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
# This is an auto-generated file. Regular comments will be removed when this
2-
# file is regenerated. Regenerating will not touch any manually added keys,
3-
# so comments can be added in a "comment" key.
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
411

512
[f6dcb64f-03b0-4b60-81b1-3c9dbf47e887]
613
description = "empty strands"
@@ -19,12 +26,42 @@ description = "long different strands"
1926

2027
[919f8ef0-b767-4d1b-8516-6379d07fcb28]
2128
description = "disallow first strand longer"
29+
include = false
30+
31+
[b9228bb1-465f-4141-b40f-1f99812de5a8]
32+
description = "disallow first strand longer"
33+
reimplements = "919f8ef0-b767-4d1b-8516-6379d07fcb28"
2234

2335
[8a2d4ed0-ead5-4fdd-924d-27c4cf56e60e]
2436
description = "disallow second strand longer"
37+
include = false
38+
39+
[dab38838-26bb-4fff-acbe-3b0a9bfeba2d]
40+
description = "disallow second strand longer"
41+
reimplements = "8a2d4ed0-ead5-4fdd-924d-27c4cf56e60e"
2542

2643
[5dce058b-28d4-4ca7-aa64-adfe4e17784c]
2744
description = "disallow left empty strand"
45+
include = false
46+
47+
[db92e77e-7c72-499d-8fe6-9354d2bfd504]
48+
description = "disallow left empty strand"
49+
include = false
50+
reimplements = "5dce058b-28d4-4ca7-aa64-adfe4e17784c"
51+
52+
[b764d47c-83ff-4de2-ab10-6cfe4b15c0f3]
53+
description = "disallow empty first strand"
54+
reimplements = "db92e77e-7c72-499d-8fe6-9354d2bfd504"
2855

2956
[38826d4b-16fb-4639-ac3e-ba027dec8b5f]
3057
description = "disallow right empty strand"
58+
include = false
59+
60+
[920cd6e3-18f4-4143-b6b8-74270bb8f8a3]
61+
description = "disallow right empty strand"
62+
include = false
63+
reimplements = "38826d4b-16fb-4639-ac3e-ba027dec8b5f"
64+
65+
[9ab9262f-3521-4191-81f5-0ed184a5aa89]
66+
description = "disallow empty second strand"
67+
reimplements = "920cd6e3-18f4-4143-b6b8-74270bb8f8a3"
Lines changed: 27 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,47 @@
1-
local compute = require('hamming').compute
1+
local hamming = require('hamming')
22

33
describe('hamming', function()
4-
it('identical strands', function()
5-
assert.are.equal(0, compute('A', 'A'))
6-
end)
7-
8-
it('long identical strands', function()
9-
assert.are.equal(0, compute('GGACTGA', 'GGACTGA'))
10-
end)
11-
12-
it('complete distance in single nucleotide strands', function()
13-
assert.are.equal(1, compute('A', 'G'))
14-
end)
15-
16-
it('complete distance in small strands', function()
17-
assert.are.equal(2, compute('AG', 'CT'))
18-
end)
19-
20-
it('small distance in small strands', function()
21-
assert.are.equal(1, compute('AT', 'CT'))
22-
end)
23-
24-
it('small distance', function()
25-
assert.are.equal(1, compute('GGACG', 'GGTCG'))
26-
end)
27-
28-
it('small distance in long strands', function()
29-
assert.are.equal(2, compute('ACCAGGG', 'ACTATGG'))
4+
it('empty strands', function()
5+
assert.are.equal(0, hamming.compute("", ""))
306
end)
317

32-
it('non unique character in first strand', function()
33-
assert.are.equal(1, compute('AGA', 'AGG'))
8+
it('single letter identical strands', function()
9+
assert.are.equal(0, hamming.compute("A", "A"))
3410
end)
3511

36-
it('non unique character in second strand', function()
37-
assert.are.equal(1, compute('AGG', 'AGA'))
12+
it('single letter different strands', function()
13+
assert.are.equal(1, hamming.compute("G", "T"))
3814
end)
3915

40-
it('same nucleotides in different positions', function()
41-
assert.are.equal(2, compute('TAG', 'GAT'))
16+
it('long identical strands', function()
17+
assert.are.equal(0, hamming.compute("GGACTGAAATCTG", "GGACTGAAATCTG"))
4218
end)
4319

44-
it('large distance', function()
45-
assert.are.equal(4, compute('GATACA', 'GCATAA'))
20+
it('long different strands', function()
21+
assert.are.equal(9, hamming.compute("GGACGGATTCTG", "AGGACGGATTCT"))
4622
end)
4723

48-
it('large distance in off-by-one strand', function()
49-
assert.are.equal(9, compute('GGACGGATTCTG', 'AGGACGGATTCT'))
24+
it('disallow first strand longer', function()
25+
assert.has_error(function()
26+
hamming.compute("AATG", "AAA")
27+
end, "strands must be of equal length")
5028
end)
5129

52-
it('empty strands', function()
53-
assert.are.equal(0, compute('', ''))
30+
it('disallow second strand longer', function()
31+
assert.has_error(function()
32+
hamming.compute("ATA", "AGTG")
33+
end, "strands must be of equal length")
5434
end)
5535

56-
it('disallow first strand longer', function()
57-
assert.are.equal(-1, compute('AATG', 'AAA'))
36+
it('disallow empty first strand', function()
37+
assert.has_error(function()
38+
hamming.compute("", "G")
39+
end, "strands must be of equal length")
5840
end)
5941

60-
it('disallow second strand longer', function()
61-
assert.are.equal(-1, compute('ATA', 'AGTG'))
42+
it('disallow empty second strand', function()
43+
assert.has_error(function()
44+
hamming.compute("G", "")
45+
end, "strands must be of equal length")
6246
end)
6347
end)

0 commit comments

Comments
 (0)