From 4a013aa17d8dbeca7f07d50e1f7d2dc0ad60d5d0 Mon Sep 17 00:00:00 2001 From: Markus Bertheau Date: Fri, 22 May 2015 23:27:43 +0200 Subject: [PATCH] Add hamming exercise --- config.json | 3 ++- hamming/example.rkt | 8 ++++++++ hamming/hamming-test.rkt | 41 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 hamming/example.rkt create mode 100644 hamming/hamming-test.rkt diff --git a/config.json b/config.json index 6dc26b3..099cf1a 100644 --- a/config.json +++ b/config.json @@ -4,6 +4,7 @@ "repository": "https://github.com/exercism/xracket", "active": false, "problems": [ - "bob" + "bob", + "hamming" ] } diff --git a/hamming/example.rkt b/hamming/example.rkt new file mode 100644 index 0000000..dcb6e71 --- /dev/null +++ b/hamming/example.rkt @@ -0,0 +1,8 @@ +#lang racket + +(define (hamming-distance dna1 dna2) + (unless (= (string-length dna1) (string-length dna2)) + (raise-argument-error 'hamming-distance "strings of equal length" 1 dna1 dna2)) + (count (compose not eqv?) (string->list dna1) (string->list dna2))) + +(provide hamming-distance) diff --git a/hamming/hamming-test.rkt b/hamming/hamming-test.rkt new file mode 100644 index 0000000..ffd5890 --- /dev/null +++ b/hamming/hamming-test.rkt @@ -0,0 +1,41 @@ +#lang racket + +(require rackunit + rackunit/text-ui + "hamming.rkt") + +(define suite + (test-suite + "Tests for the hamming exercise" + (check-equal? 0 (hamming-distance "A" "A") + "identical strands") + (check-equal? 0 (hamming-distance "GGACTGA" "GGACTGA") + "long identical strands") + (check-equal? 1 (hamming-distance "A" "G") + "complete distance in single nucleotide strands") + (check-equal? 2 (hamming-distance "AG" "CT") + "complete distance in small strands") + (check-equal? 1 (hamming-distance "AT" "CT") + "small distance in small strands") + (check-equal? 1 (hamming-distance "GGACG" "GGTCG") + "small distance") + (check-equal? 2 (hamming-distance "ACCAGGG" "ACTATGG") + "small distance in long strands") + (check-equal? 1 (hamming-distance "AGA" "AGG") + "non-unique character in first strand") + (check-equal? 1 (hamming-distance "AGG" "AGA") + "non-unique character in second strand") + (check-equal? 4 (hamming-distance "GATACA" "GCATAA") + "large distance") + (check-equal? 9 (hamming-distance "GGACGGATTCTG" "AGGACGGATTCT") + "large distance in off-by-one strand") + (check-equal? 0 (hamming-distance "" "") + "empty strands") + (check-exn #rx"strings of equal length" + (thunk (hamming-distance "AATG" "AAA")) + "disallow first strand longer") + (check-exn #rx"strings of equal length" + (thunk (hamming-distance "ATA" "AGTG")) + "disallow second string longer"))) + +(exit (if (zero? (run-tests suite)) 0 1))