September 21, 2018

Exercism - Nucleotide Count

There are lots of different ways to approach every exercise on Exercism. Why not see how others have solved it?


Given a single stranded DNA string, compute how many times each nucleotide occurs in the string.

The genetic language of every living thing on the planet is DNA. DNA is a large molecule that is built from an extremely long sequence of individual elements called nucleotides. 4 types exist in DNA and these differ only slightly and can be represented as the following symbols:

'A' for adenine, 'C' for cytosine, 'G' for guanine, and 'T' thymine.


First version using regex and function composition!

(ns nucleotide-count)

(defn count [nucleotide strand]
  (let [matcher (re-matcher (re-pattern (str strand)) nucleotide)]
    (loop [m (re-find matcher)
           res []]
      (if-not m
        (clojure.core/count res)
        (recur (re-find matcher) (conj res m))))))

(defn nucleotide-counts [strand]
  (let [nuc [\A \C \G \T]]
    (->> (map (partial nucleotide-count/count strand) nuc)
         (zipmap nuc))))

Very happy to be able to learn some new dialects. I am exploring each solution of this season of exercices. if-let and the ->> operator was just good to practice.

Link to solution at Exercism: Link

Tags: clojure exercism