Skip to content
This repository was archived by the owner on Feb 11, 2018. It is now read-only.

Commit

Permalink
Add hamming exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
mbertheau committed May 22, 2015
1 parent b89e1b9 commit 4a013aa
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
3 changes: 2 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"repository": "https://github.com/exercism/xracket",
"active": false,
"problems": [
"bob"
"bob",
"hamming"
]
}
8 changes: 8 additions & 0 deletions hamming/example.rkt
Original file line number Diff line number Diff line change
@@ -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)
41 changes: 41 additions & 0 deletions hamming/hamming-test.rkt
Original file line number Diff line number Diff line change
@@ -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))

0 comments on commit 4a013aa

Please sign in to comment.