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 14, 2015
1 parent 229a15b commit d6f9d6e
Show file tree
Hide file tree
Showing 3 changed files with 49 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"
]
}
12 changes: 12 additions & 0 deletions hamming/example.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#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))
(for/fold ([distance 0])
([c1 (in-string dna1)]
[c2 (in-string dna2)]
#:unless (eq? c1 c2))
(add1 distance)))

(provide hamming-distance)
35 changes: 35 additions & 0 deletions hamming/hamming-test.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#lang racket

(require rackunit
"hamming.rkt")

(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")

0 comments on commit d6f9d6e

Please sign in to comment.