Sha256: 17274ea48128487aaa1d880675602e81a87fa3ad7e6ff72b7b50c4249b810a27

Contents?: true

Size: 2 KB

Versions: 29

Compression:

Stored size: 2 KB

Contents

#import "RNATranscriptionExample.h"

@implementation RNATranscription

- (NSString *)rnaFromDNAStrand:(NSString *)dnaStrand
{
    NSDictionary *dnaToRNADict = @{ @"G" : @"C",
                                    @"C" : @"G",
                                    @"T" : @"A",
                                    @"A" : @"U" };
    
    NSArray *dnaKeys = [dnaToRNADict allKeys];
    NSString *dnaKeyString = [dnaKeys componentsJoinedByString:@""];
    NSCharacterSet *invertedSet = [[NSCharacterSet characterSetWithCharactersInString:dnaKeyString] invertedSet];
    NSRange invertedSetRange = [dnaStrand rangeOfCharacterFromSet:invertedSet];
    if (invertedSetRange.location != NSNotFound) {
        return nil;
    }
    
    NSMutableString *rna = [NSMutableString stringWithString:dnaStrand];
    NSMutableDictionary *dnaToRnaRanges = [NSMutableDictionary dictionary];
    
    [[dnaToRNADict allKeys] enumerateObjectsUsingBlock:^(NSString *dnaKey, NSUInteger idx, BOOL * _Nonnull stop) {
        NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:dnaKey options:0 error:NULL];
        NSMutableArray *ranges = [NSMutableArray array];
        
        [regex enumerateMatchesInString:dnaStrand options:0 range:NSMakeRange(0, [dnaStrand length]) usingBlock:^(NSTextCheckingResult * _Nullable match, NSMatchingFlags flags, BOOL * _Nonnull stop) {
            NSRange range = [match rangeAtIndex:0];
            [ranges addObject:[NSValue valueWithRange:range]];
        }];
        
        dnaToRnaRanges[dnaKey] = [ranges copy];
    }];
    
    [dnaToRNADict enumerateKeysAndObjectsUsingBlock:^(NSString *dnaKey, NSString *rnaValue, BOOL * _Nonnull stop) {
        NSArray *dnaRanges = dnaToRnaRanges[dnaKey];
        
        [dnaRanges enumerateObjectsUsingBlock:^(NSValue *dnaRangeValue, NSUInteger idx, BOOL * _Nonnull stop) {
            [rna replaceOccurrencesOfString:dnaKey withString:rnaValue options:NSLiteralSearch range:[dnaRangeValue rangeValue]];
        }];
    }];
    
    return [rna copy];
}

@end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
trackler-2.2.0.6 tracks/objective-c/exercises/rna-transcription/RNATranscriptionExample.m
trackler-2.2.0.5 tracks/objective-c/exercises/rna-transcription/RNATranscriptionExample.m
trackler-2.2.0.4 tracks/objective-c/exercises/rna-transcription/RNATranscriptionExample.m
trackler-2.2.0.3 tracks/objective-c/exercises/rna-transcription/RNATranscriptionExample.m
trackler-2.2.0.2 tracks/objective-c/exercises/rna-transcription/RNATranscriptionExample.m
trackler-2.2.0.1 tracks/objective-c/exercises/rna-transcription/RNATranscriptionExample.m
trackler-2.2.0.0 tracks/objective-c/exercises/rna-transcription/RNATranscriptionExample.m
trackler-2.1.0.55 tracks/objective-c/exercises/rna-transcription/RNATranscriptionExample.m
trackler-2.1.0.54 tracks/objective-c/exercises/rna-transcription/RNATranscriptionExample.m