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

KiligWY's solution

to RNA Transcription in the Objective-C Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Given a DNA strand, return its RNA complement (per RNA transcription).

Both DNA and RNA strands are a sequence of nucleotides.

The four nucleotides found in DNA are adenine (A), cytosine (C), guanine (G) and thymine (T).

The four nucleotides found in RNA are adenine (A), cytosine (C), guanine (G) and uracil (U).

Given a DNA strand, its transcribed RNA strand is formed by replacing each nucleotide with its complement:

  • G -> C
  • C -> G
  • T -> A
  • A -> U

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/rna-transcription/RnaTranscription.m

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

Source

Hyperphysics http://hyperphysics.phy-astr.gsu.edu/hbase/Organic/transcription.html

Submitting Incomplete Solutions

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

RNATranscriptionTest.m

#import <XCTest/XCTest.h>

#if __has_include("RNATranscriptionExample.h")
# import "RNATranscriptionExample.h"
# else
# import "RNATranscription.h"
#endif

@interface RNATranscriptionTest : XCTestCase

@end

@implementation RNATranscriptionTest

- (void)test1 {
    XCTAssertEqualObjects(@"G", [[[RNATranscription alloc] init] rnaFromDNAStrand:@"C"]);
}

- (void)test2 {
    XCTAssertEqualObjects(@"C", [[[RNATranscription alloc] init] rnaFromDNAStrand:@"G"]);
}

- (void)test3 {
    XCTAssertEqualObjects(@"A", [[[RNATranscription alloc] init] rnaFromDNAStrand:@"T"]);
}

- (void)test4 {
    XCTAssertEqualObjects(@"U", [[[RNATranscription alloc] init] rnaFromDNAStrand:@"A"]);
}

- (void)test5 {
    XCTAssertEqualObjects(@"UGCACCAGAAUU", [[[RNATranscription alloc] init] rnaFromDNAStrand:@"ACGTGGTCTTAA"]);
}

- (void)test6 {
    XCTAssertEqualObjects(nil, [[[RNATranscription alloc] init] rnaFromDNAStrand:@"U"]);
}

- (void)test7 {
    XCTAssertEqualObjects(nil, [[[RNATranscription alloc] init] rnaFromDNAStrand:@"XXX"]);
}

- (void)test8 {
    XCTAssertEqualObjects(nil, [[[RNATranscription alloc] init] rnaFromDNAStrand:@"ACGTXXXCTTAA"]);
}

@end

RNATranscription.h

#import <Foundation/Foundation.h>

@interface RNATranscription : NSObject

- (NSString *)rnaFromDNAStrand:(NSString *)DNAStrand;

@end

RNATranscription.m

#import "RNATranscription.h"

@implementation RNATranscription

- (NSString *)rnaFromDNAStrand:(NSString *)DNAStrand {
    if ([[NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"^[ACGT]{1,}$"] evaluateWithObject:DNAStrand]) {
        NSMutableString *result = [NSMutableString string];
        [DNAStrand enumerateSubstringsInRange:NSMakeRange(0, DNAStrand.length) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString * _Nullable substring, NSRange substringRange, NSRange enclosingRange, BOOL * _Nonnull stop) {
            if ([substring isEqualToString:@"A"]) {
                [result appendString:@"U"];
            }
            if ([substring isEqualToString:@"C"]) {
                [result appendString:@"G"];
            }
            if ([substring isEqualToString:@"G"]) {
                [result appendString:@"C"];
            }
            if ([substring isEqualToString:@"T"]) {
                [result appendString:@"A"];
            }
        }];
        return result;
    } else {
        return nil;
    }
}

@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?