🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of zradke

zradke's solution

to Nucleotide Count in the Objective-C Track

Published at Jul 13 2018 · 1 comment
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...


There are two different methods of getting set up to run the tests with Objective-C:

  • Create an Xcode project with a test target which will run the tests.
  • Use the ruby gem objc as a test runner utility.

Both are described in more detail here: http://exercism.io/languages/objective-c

Submitting Exercises

When submitting an exercise, make sure your solution file is in the same directory as the test code.

The submit command will look something like:

exercism submit <path-to-exercism-workspace>/objective-c/nucleotide-count/NucleotideCount.m

You can find the Exercism workspace by running exercism debug and looking for the line beginning with Workspace.


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 <XCTest/XCTest.h>

#if __has_include("NucleotideCountExample.h")
# import "NucleotideCountExample.h"
# else
# import "NucleotideCount.h"


@interface NucleotideCountTest : XCTestCase


@implementation NucleotideCountTest

- (void)testEmptyDNAStringHasNoAdenosine {
  NucleotideCount *dna = [[NucleotideCount alloc] initWithStrand:@""];
  NSUInteger result = [dna count:@"A"];
  NSUInteger expected = 0;

- (void)testEmptyNucleotideCountStringHasNoNucleotides {
  NucleotideCount *dna = [[NucleotideCount alloc] initWithStrand:@""];
  NSDictionary<NSString *, NSNumber *> *results = [dna nucleotideCounts];
  NSDictionary<NSString *, NSNumber *> *expected = @{ @"A": @0, @"T" : @0, @"C" : @0, @"G" : @0 };
  XCTAssertEqualObjects(results, expected);

- (void)testRepetitiveCytidineGetsCounted {
  NucleotideCount *dna = [[NucleotideCount alloc] initWithStrand:@"CCCCC"];
  NSUInteger result = [dna count:@"C"];
  NSUInteger expected = 5;

- (void)testRepetitiveSequenceHasOnlyGuanosine {
  NucleotideCount *dna = [[NucleotideCount alloc] initWithStrand:@"GGGGGGGG"];
  NSDictionary<NSString *, NSNumber *> *results = [dna nucleotideCounts];
  NSDictionary<NSString *, NSNumber *> *expected = @{ @"A": @0, @"T" : @0, @"C" : @0, @"G" : @8 };
  XCTAssertEqualObjects(results, expected);

- (void)testCountsByThymidine {
  NucleotideCount *dna = [[NucleotideCount alloc] initWithStrand:@"GGGGGTAACCCGG"];
  NSUInteger result = [dna count:@"T"];
  NSUInteger expected = 1;

- (void)testCountsANucleotideOnlyOnce {
  NucleotideCount *dna = [[NucleotideCount alloc] initWithStrand:@"CGATTGGG"];
  NSUInteger result = [dna count:@"T"];
  NSUInteger expected = 2;

- (void)testValidatesNucleotideCount {
  XCTAssertThrows([[NucleotideCount alloc] initWithStrand:@"John"]);

- (void)testCountsAllNucleotides {
  NucleotideCount *dna = [[NucleotideCount alloc] initWithStrand:longStrand];
  NSDictionary<NSString *, NSNumber *> *results = [dna nucleotideCounts];
  NSDictionary<NSString *, NSNumber *> *expected = @{ @"A": @20, @"T" : @21, @"C" : @12, @"G" : @17 };
  XCTAssertEqualObjects(results, expected);

//  DNA.m
//  NucleotideCount
//  Created by Zachary Radke on 9/26/14.
//  Copyright (c) 2014 Zach Radke. All rights reserved.

#import "NucleotideCount.h"

static NSString *const ValidNucleotides = @"ATCGU"; // Includes uracil
static NSString *const ValidDNANucleotides = @"ATCG";

static NSDictionary *NucleotideCountsForDNAStrand(NSString *dnaStrand) {
    NSMutableDictionary *nucleotideCounts = [NSMutableDictionary dictionary];
    NSMutableString *mutableStrand = [dnaStrand mutableCopy];
    NSRange range = NSMakeRange(0, ValidDNANucleotides.length);
    NSStringEnumerationOptions options = NSStringEnumerationByComposedCharacterSequences;
    // To ensure that we count all the DNA nucleotides, we enumerate those rather than the strand itself
    [ValidDNANucleotides enumerateSubstringsInRange:range options:options usingBlock:^(NSString *nucleotide, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
        NSUInteger count = 0;
        NSRange nucleotideRange = [mutableStrand rangeOfString:nucleotide];
        // While the current nucleotide still exists in the strand, we consume and count it
        while (nucleotideRange.location != NSNotFound) {
            [mutableStrand deleteCharactersInRange:nucleotideRange];
            nucleotideRange = [mutableStrand rangeOfString:nucleotide];
        nucleotideCounts[nucleotide] = [NSNumber numberWithUnsignedInteger:count];
    NSCAssert(mutableStrand.length == 0, @"DNA strand contains invalid nucleotides.");
    return [nucleotideCounts copy];

@implementation DNA

- (instancetype)initWithStrand:(NSString *)strand {
    if (self = [super init]) {
        _nucleotideCounts = NucleotideCountsForDNAStrand(strand);
    return self;

- (NSUInteger)count:(NSString *)nucleotide {
    NSParameterAssert([ValidNucleotides containsString:nucleotide]);
    return [self.nucleotideCounts[nucleotide] unsignedIntegerValue];


Community comments

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

I'm wondering, if this could be seperated into submethods for readability.

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?