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

atfelix's solution

to Nucleotide Count in the Objective-C Track

Published at Sep 17 2018 · 0 comments
Instructions
Test suite
Solution

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...

Setup

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.

Source

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.

NucleotideCountTest.m

#import <XCTest/XCTest.h>

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

NS_ASSUME_NONNULL_BEGIN

@interface NucleotideCountTest : XCTestCase

@end

@implementation NucleotideCountTest

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

- (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;
  XCTAssertEqual(expected,result);
}

- (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;
  XCTAssertEqual(expected,result);
}

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

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

- (void)testCountsAllNucleotides {
  NSString *longStrand = @"AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC";
  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);
}

@end
NS_ASSUME_NONNULL_END

NucleotideCount/NucleotideCount Tests/NSDictionary+Merge.h

@import Foundation;

@interface NSDictionary (Merge)

-(NSDictionary *)merge:(NSDictionary *)other block:(id (^_Nonnull)(id firstKey, id secondKey))block;

@end

NucleotideCount/NucleotideCount Tests/NSDictionary+Merge.m

#import "NSDictionary+Merge.h"

@implementation NSDictionary (Merge)

-(NSDictionary *)merge:(NSDictionary *)other block:(id (^)(id, id))block {
    __block NSMutableDictionary *copy = self.mutableCopy;
    [other enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        copy[key] = block(copy[key], obj);
    }];
    return copy.copy;
}

@end

NucleotideCount/NucleotideCount Tests/NSString+Counter.h

@import Foundation;

@interface NSString (Counter)

-(NSDictionary<NSString *, NSNumber *> *)characterCounter;

@end

NucleotideCount/NucleotideCount Tests/NSString+Counter.m

#import "NSString+Counter.h"

@implementation NSString (Counter)

-(NSDictionary<NSString *, NSNumber *> *)characterCounter {
    __block NSMutableDictionary<NSString *, NSNumber *> *counter = [[NSMutableDictionary alloc] init];
    [self enumerateSubstringsInRange:NSMakeRange(0, self.length)
                             options:NSStringEnumerationByComposedCharacterSequences
                          usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
                              counter[substring] = [NSNumber numberWithInteger:counter[substring].integerValue + 1];
                          }];
    return counter.copy;
}

@end

NucleotideCount/NucleotideCount Tests/NSString+Nucleotide.h

@import Foundation;

@interface NSString (Nucleotide)

@property (nonatomic, readonly) BOOL isValidNucleotide;

@end

NucleotideCount/NucleotideCount Tests/NSString+Nucleotide.m

#import "NSString+Nucleotide.h"

@implementation NSString (Nucleotide)

-(BOOL)isValidNucleotide {
    __block BOOL isValid = YES;

    [self enumerateSubstringsInRange:NSMakeRange(0, self.length)
                             options:NSStringEnumerationByComposedCharacterSequences
                          usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
                              if (![@[@"A", @"C", @"G", @"T"] containsObject:substring]) {
                                  isValid = NO;
                                  *stop = YES;
                              }
                          }];

    return isValid;
}

@end

NucleotideCount/NucleotideCount Tests/NucleotideCount.h

@import Foundation;

@interface NucleotideCount : NSObject

@property (nonatomic, readonly, nonnull) NSDictionary<NSString *, NSNumber *> *nucleotideCounts;

-(nonnull instancetype)initWithStrand:(nonnull NSString *)strand;
-(NSUInteger)count:(nonnull NSString *)singleLetter;

@end

NucleotideCount/NucleotideCount Tests/NucleotideCount.m

#import "NucleotideCount.h"
#import "NSDictionary+Merge.h"
#import "NSString+Counter.h"
#import "NSString+Nucleotide.h"

@implementation NucleotideCount

-(instancetype)initWithStrand:(NSString *)strand {
    if (!strand.isValidNucleotide) {
        [NSException raise:@"Invalid input" format:@"Strand must contain only 'A's, 'C's, 'G's, 'T's"];
    }
    self = [super init];
    if (self) {
        _nucleotideCounts = [strand.characterCounter merge:@{
                                                             @"A": @0,
                                                             @"C": @0,
                                                             @"G": @0,
                                                             @"T": @0
                                                             }
                                                     block:^id(id firstValue, id secondValue) {
                                                         return (firstValue) ?: secondValue;
                                                     }];
    }
    return self;
}

-(NSUInteger)count:(NSString *)singleLetter {
    if (singleLetter.length != 1) {
        [NSException raise:@"Invalid input" format:@"input must be exactly one letter"];
    }
    return self.nucleotideCounts[singleLetter].integerValue;
}

@end

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?