Avatar of eitansuez

eitansuez's solution

to Nucleotide Count in the Kotlin Track

Published at Oct 09 2019 · 0 comments
Test suite

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.

Here is an analogy:

  • twigs are to birds nests as
  • nucleotides are to DNA as
  • legos are to lego houses as
  • words are to sentences as...


Go through the setup instructions for Kotlin to install the necessary dependencies:


Making the test suite pass

Execute the tests with:

$ gradlew test

Use gradlew.bat if you're on Windows

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by removing the @Ignore annotation.


The Calculating DNA Nucleotides_problem at Rosalind http://rosalind.info/problems/dna/

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.


import org.junit.Test
import org.junit.Ignore
import kotlin.test.assertEquals

class DnaTest {

    fun emptyDnaStringHasNoNucleotides() {
        val dna = Dna("")
        val expected = mapOf('A' to 0, 'C' to 0, 'G' to 0, 'T' to 0)

        assertEquals(expected, dna.nucleotideCounts)

    fun canCountOneNucleotideInSingleCharacterInput() {
        val dna = Dna("G")
        val expected = mapOf('A' to 0, 'C' to 0, 'G' to 1, 'T' to 0)

        assertEquals(expected, dna.nucleotideCounts)

    fun canCountRepeatedNucleotide() {
        val dna = Dna("GGGGGGG")
        val expected = mapOf('A' to 0, 'C' to 0, 'G' to 7, 'T' to 0)

        assertEquals(expected, dna.nucleotideCounts)

    fun canCountMultipleNucleotides() {
        val expected = mapOf('A' to 20, 'C' to 12, 'G' to 17, 'T' to 21)

        assertEquals(expected, dna.nucleotideCounts)

    @Test(expected = IllegalArgumentException::class)
    fun validatesNucleotides() {

class Dna(s: String) {

  val nucleotideCounts = mutableMapOf('A' to 0, 'C' to 0, 'G' to 0, 'T' to 0)

  init {
    require(s.all { c -> c in nucleotideCounts.keys })
    s.forEach { c ->
      nucleotideCounts[c] = (nucleotideCounts[c] ?: 0) + 1


Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?