Skip to content

Commit 57b6a40

Browse files
committed
Sync Nucleotide Count exercise
Simplified tests and implementation.
1 parent 7460399 commit 57b6a40

File tree

3 files changed

+28
-54
lines changed

3 files changed

+28
-54
lines changed

exercises/practice/nucleotide-count/.meta/example.lisp

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,8 @@
44

55
(in-package :nucleotide-count)
66

7-
(define-condition invalid-nucleotide (error) ())
8-
9-
(defparameter +valid-nucleotides+ "ACGT")
10-
11-
(defun validate-nucleotide (nucleotide)
12-
(or (find nucleotide +valid-nucleotides+)
13-
(error 'invalid-nucleotide)))
14-
15-
(defun dna-count (nucleotide strand)
16-
(validate-nucleotide nucleotide)
17-
(count nucleotide strand))
7+
(defparameter +valid-nucleotides+ (list #\A #\C #\G #\T))
188

199
(defun nucleotide-counts (strand)
20-
(reduce #'(lambda (h c) (setf (gethash c h) (dna-count c strand)) h)
21-
+valid-nucleotides+
22-
:initial-value (make-hash-table)))
10+
(when (every #'(lambda (n) (member n +valid-nucleotides+ :test #'char=)) strand)
11+
(mapcar #'(lambda (n) (cons n (count n strand :test #'char=))) +valid-nucleotides+)))

exercises/practice/nucleotide-count/nucleotide-count-test.lisp

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,29 @@
1515
;; Define and enter a new FiveAM test-suite
1616
(def-suite* nucleotide-count-suite)
1717

18-
(defun make-hash (kvs)
19-
(reduce #'(lambda (h kv) (setf (gethash (first kv) h) (second kv)) h) kvs
20-
:initial-value (make-hash-table)))
21-
22-
(test empty-dna-strand-has-no-adenine
23-
(is (equal 0 (nucleotide-count:dna-count #\A ""))))
24-
25-
(test empty-dna-strand-has-no-nucleotides
26-
(is
27-
(equalp (make-hash '((#\A 0) (#\T 0) (#\C 0) (#\G 0)))
28-
(nucleotide-count:nucleotide-counts ""))))
29-
30-
(test repetitive-cytosine-gets-counted
31-
(is (equal 5 (nucleotide-count:dna-count #\C "CCCCC"))))
32-
33-
(test repetitive-sequence-has-only-guanine
34-
(is
35-
(equalp (make-hash '((#\A 0) (#\T 0) (#\C 0) (#\G 8)))
36-
(nucleotide-count:nucleotide-counts "GGGGGGGG"))))
37-
38-
(test counts-only-thymine
39-
(is (equal 1 (nucleotide-count:dna-count #\T "GGGGGTAACCCGG"))))
40-
41-
(test validates-nucleotides
42-
(signals nucleotide-count:invalid-nucleotide
43-
(nucleotide-count:dna-count #\X "GACT")))
44-
45-
(test counts-all-nucleotides
46-
(is
47-
(equalp (make-hash '((#\A 20) (#\T 21) (#\G 17) (#\C 12)))
48-
(nucleotide-count:nucleotide-counts
49-
"AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC"))))
18+
(test empty-strand
19+
(let ((strand "")
20+
(result '((#\A . 0) (#\C . 0) (#\G . 0) (#\T . 0))))
21+
(is (equal result (nucleotide-count:nucleotide-counts strand)))))
22+
23+
(test can-count-one-nucleotide-in-single-character-input
24+
(let ((strand "G")
25+
(result '((#\A . 0) (#\C . 0) (#\G . 1) (#\T . 0))))
26+
(is (equal result (nucleotide-count:nucleotide-counts strand)))))
27+
28+
(test strand-with-repeated-nucleotide
29+
(let ((strand "GGGGGGG")
30+
(result '((#\A . 0) (#\C . 0) (#\G . 7) (#\T . 0))))
31+
(is (equal result (nucleotide-count:nucleotide-counts strand)))))
32+
33+
(test strand-with-multiple-nucleotides
34+
(let ((strand "AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC")
35+
(result '((#\A . 20) (#\C . 12) (#\G . 17) (#\T . 21))))
36+
(is (equal result (nucleotide-count:nucleotide-counts strand)))))
37+
38+
(test strand-with-invalid-nucleotides
39+
(let ((strand "AGXXACT"))
40+
(is (equal NIL (nucleotide-count:nucleotide-counts strand)))))
5041

5142
(defun run-tests (&optional (test-or-suite 'nucleotide-count-suite))
5243
"Provides human readable results of test run. Default to entire suite."
Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
(defpackage :nucleotide-count
22
(:use :cl)
3-
(:export :dna-count :nucleotide-counts :invalid-nucleotide))
3+
(:export :nucleotide-counts))
44

55
(in-package :nucleotide-count)
66

7-
(defun dna-count (nucleotide strand)
8-
"Returns a count of the given nucleotide appearing in a DNA strand."
9-
)
10-
11-
(defun nucleotide-counts (strand)
12-
"Returns a hash of nucleotides and their counts in a given DNA strand."
13-
)
7+
(defun nucleotide-counts (strand))

0 commit comments

Comments
 (0)