# HG changeset patch # User Steve Losh # Date 1550881944 18000 # Node ID ca95211a2d98221f52a8d64275c47eee3e774c29 # Parent 7052ec0c6e1deb5c7ec5d309cf92e646d58b8ed9 RSTR diff -r 7052ec0c6e1d -r ca95211a2d98 src/problems/prob.lisp --- a/src/problems/prob.lisp Fri Feb 22 19:23:17 2019 -0500 +++ b/src/problems/prob.lisp Fri Feb 22 19:32:24 2019 -0500 @@ -13,17 +13,8 @@ *output-prob* (let ((dna (read-line data)) (gc-contents (read-all-from-string (read-line data)))) - (labels - ((gcp (base) - "Return whether `base` is G or C." - (or (char= #\G base) - (char= #\C base))) - (base-probability (gc-content base) - "Return the probability of `base` in DNA with the given `gc-content`." - (if (gcp base) - (/ gc-content 2) - (/ (- 1 gc-content) 2))) - (prob (gc-content) + (flet + ((prob (gc-content) (iterate (for base :in-string dna) (summing (log (base-probability gc-content base) 10))))) diff -r 7052ec0c6e1d -r ca95211a2d98 src/problems/rstr.lisp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/problems/rstr.lisp Fri Feb 22 19:32:24 2019 -0500 @@ -0,0 +1,16 @@ +(in-package :rosalind) + +(defparameter *input-rstr* "90000 0.6 +ATAGCCGA") + +(defparameter *output-rstr* "0.689") + + +(define-problem rstr (data stream) + *input-rstr* + *output-rstr* + (let* ((n (read data)) + (gc (coerce (read data) 'double-float)) + (dna (read-line data)) + (prob (product dna :key (curry #'base-probability gc)))) + (format nil "~,3F" (- 1 (expt (- 1 prob) n))))) diff -r 7052ec0c6e1d -r ca95211a2d98 src/utils.lisp --- a/src/utils.lisp Fri Feb 22 19:23:17 2019 -0500 +++ b/src/utils.lisp Fri Feb 22 19:32:24 2019 -0500 @@ -84,6 +84,18 @@ ((#\C #\T #\U) 1))) +(defun-inline gcp (base) + "Return whether `base` is G or C." + (or (char= #\G base) + (char= #\C base))) + +(defun-inline base-probability (gc-content base) + "Return the probability of `base` in DNA with the given `gc-content`." + (if (gcp base) + (/ gc-content 2) + (/ (- 1 gc-content) 2))) + + (defun mapcount (predicate sequence &rest more-sequences) "Map `predicate` across sequences, counting satisfactory applications." (let ((result 0))