Skip to content

Commit

Permalink
Add hamming exercise (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
keiravillekode authored Oct 20, 2024
1 parent 3bb14ee commit 783616c
Show file tree
Hide file tree
Showing 12 changed files with 3,579 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@
"prerequisites": [],
"difficulty": 3
},
{
"slug": "hamming",
"name": "Hamming",
"uuid": "69a68463-3546-45eb-9560-d4c859422cbe",
"practices": [],
"prerequisites": [],
"difficulty": 3
},
{
"slug": "isogram",
"name": "Isogram",
Expand Down
27 changes: 27 additions & 0 deletions exercises/practice/hamming/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Instructions

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.
19 changes: 19 additions & 0 deletions exercises/practice/hamming/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"keiravillekode"
],
"files": {
"solution": [
"hamming.s"
],
"test": [
"hamming_test.c"
],
"example": [
".meta/example.s"
]
},
"blurb": "Calculate the Hamming difference between two DNA strands.",
"source": "The Calculating Point Mutations problem at Rosalind",
"source_url": "https://rosalind.info/problems/hamm/"
}
29 changes: 29 additions & 0 deletions exercises/practice/hamming/.meta/example.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
.equ UNEQUAL_LENGTHS, -1

.text
.globl distance

/* extern int distance(const char *strand1, const char *strand2); */
distance:
mov x2, x0
mov x0, #0 /* distance */

.read:
ldrb w3, [x1], #1 /* load byte, with post-increment */
ldrb w4, [x2], #1 /* load byte, with post-increment */

cbz w3, .exit /* check for null terminator */
cbz w4, .exit

cmp w3, w4
cinc x0, x0, ne /* increment distance if characters differ */
b .read

.exit:
cmp w3, w4
beq .return

mov x0, UNEQUAL_LENGTHS

.return:
ret
67 changes: 67 additions & 0 deletions exercises/practice/hamming/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[f6dcb64f-03b0-4b60-81b1-3c9dbf47e887]
description = "empty strands"

[54681314-eee2-439a-9db0-b0636c656156]
description = "single letter identical strands"

[294479a3-a4c8-478f-8d63-6209815a827b]
description = "single letter different strands"

[9aed5f34-5693-4344-9b31-40c692fb5592]
description = "long identical strands"

[cd2273a5-c576-46c8-a52b-dee251c3e6e5]
description = "long different strands"

[919f8ef0-b767-4d1b-8516-6379d07fcb28]
description = "disallow first strand longer"
include = false

[b9228bb1-465f-4141-b40f-1f99812de5a8]
description = "disallow first strand longer"
reimplements = "919f8ef0-b767-4d1b-8516-6379d07fcb28"

[8a2d4ed0-ead5-4fdd-924d-27c4cf56e60e]
description = "disallow second strand longer"
include = false

[dab38838-26bb-4fff-acbe-3b0a9bfeba2d]
description = "disallow second strand longer"
reimplements = "8a2d4ed0-ead5-4fdd-924d-27c4cf56e60e"

[5dce058b-28d4-4ca7-aa64-adfe4e17784c]
description = "disallow left empty strand"
include = false

[db92e77e-7c72-499d-8fe6-9354d2bfd504]
description = "disallow left empty strand"
include = false
reimplements = "5dce058b-28d4-4ca7-aa64-adfe4e17784c"

[b764d47c-83ff-4de2-ab10-6cfe4b15c0f3]
description = "disallow empty first strand"
reimplements = "db92e77e-7c72-499d-8fe6-9354d2bfd504"

[38826d4b-16fb-4639-ac3e-ba027dec8b5f]
description = "disallow right empty strand"
include = false

[920cd6e3-18f4-4143-b6b8-74270bb8f8a3]
description = "disallow right empty strand"
include = false
reimplements = "38826d4b-16fb-4639-ac3e-ba027dec8b5f"

[9ab9262f-3521-4191-81f5-0ed184a5aa89]
description = "disallow empty second strand"
reimplements = "920cd6e3-18f4-4143-b6b8-74270bb8f8a3"
36 changes: 36 additions & 0 deletions exercises/practice/hamming/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
AS = aarch64-linux-gnu-as
CC = aarch64-linux-gnu-gcc

CFLAGS = -g -Wall -Wextra -pedantic -Werror
LDFLAGS =

ALL_LDFLAGS = -pie -Wl,--fatal-warnings

ALL_CFLAGS = -std=c99 -fPIE $(CFLAGS)
ALL_LDFLAGS += $(LDFLAGS)

C_OBJS = $(patsubst %.c,%.o,$(wildcard *.c))
AS_OBJS = $(patsubst %.s,%.o,$(wildcard *.s))
ALL_OBJS = $(filter-out example.o,$(C_OBJS) $(AS_OBJS) vendor/unity.o)

CC_CMD = $(CC) $(ALL_CFLAGS) -c -o $@ $<

all: tests
qemu-aarch64 -L /usr/aarch64-linux-gnu ./$<

tests: $(ALL_OBJS)
@$(CC) $(ALL_CFLAGS) $(ALL_LDFLAGS) -o $@ $(ALL_OBJS)

%.o: %.s
@$(AS) -o $@ $<

%.o: %.c
@$(CC_CMD)

vendor/unity.o: vendor/unity.c vendor/unity.h vendor/unity_internals.h
@$(CC_CMD)

clean:
@rm -f *.o vendor/*.o tests

.PHONY: all clean
7 changes: 7 additions & 0 deletions exercises/practice/hamming/hamming.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.equ UNEQUAL_LENGTHS, -1

.text
.globl distance

distance:
ret
69 changes: 69 additions & 0 deletions exercises/practice/hamming/hamming_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include "vendor/unity.h"

#define UNEQUAL_LENGTHS -1

extern int distance(const char *strand1, const char *strand2);

void setUp(void) {
}

void tearDown(void) {
}

void test_empty_strands(void) {
TEST_ASSERT_EQUAL_INT(0, distance("", ""));
}

void test_single_letter_identical_strands(void) {
TEST_IGNORE();
TEST_ASSERT_EQUAL_INT(0, distance("A", "A"));
}

void test_single_letter_different_strands(void) {
TEST_IGNORE();
TEST_ASSERT_EQUAL_INT(1, distance("G", "T"));
}

void test_long_identical_strands(void) {
TEST_IGNORE();
TEST_ASSERT_EQUAL_INT(0, distance("GGACTGAAATCTG", "GGACTGAAATCTG"));
}

void test_long_different_strands(void) {
TEST_IGNORE();
TEST_ASSERT_EQUAL_INT(9, distance("GGACGGATTCTG", "AGGACGGATTCT"));
}

void test_disallow_first_strand_longer(void) {
TEST_IGNORE();
TEST_ASSERT_EQUAL_INT(UNEQUAL_LENGTHS, distance("AATG", "AAA"));
}

void test_disallow_second_strand_longer(void) {
TEST_IGNORE();
TEST_ASSERT_EQUAL_INT(UNEQUAL_LENGTHS, distance("ATA", "AGTG"));
}

void test_disallow_empty_first_strand(void) {
TEST_IGNORE();
TEST_ASSERT_EQUAL_INT(UNEQUAL_LENGTHS, distance("", "G"));
}

void test_disallow_empty_second_strand(void) {
TEST_IGNORE();
TEST_ASSERT_EQUAL_INT(UNEQUAL_LENGTHS, distance("G", ""));
}

int main(void) {
UNITY_BEGIN();
RUN_TEST(test_empty_strands);
RUN_TEST(test_single_letter_identical_strands);
RUN_TEST(test_single_letter_different_strands);
RUN_TEST(test_long_identical_strands);
RUN_TEST(test_long_different_strands);
RUN_TEST(test_disallow_first_strand_longer);
RUN_TEST(test_disallow_second_strand_longer);
RUN_TEST(test_disallow_empty_first_strand);
RUN_TEST(test_disallow_empty_second_strand);
return UNITY_END();
}
Loading

0 comments on commit 783616c

Please sign in to comment.