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.1.19 tracks/objective-c/exercises/rna-transcription/RNATranscriptionExample.m
trackler-2.2.1.18 tracks/objective-c/exercises/rna-transcription/RNATranscriptionExample.m
trackler-2.2.1.17 tracks/objective-c/exercises/rna-transcription/RNATranscriptionExample.m
trackler-2.2.1.16 tracks/objective-c/exercises/rna-transcription/RNATranscriptionExample.m
trackler-2.2.1.15 tracks/objective-c/exercises/rna-transcription/RNATranscriptionExample.m
trackler-2.2.1.14 tracks/objective-c/exercises/rna-transcription/RNATranscriptionExample.m
trackler-2.2.1.13 tracks/objective-c/exercises/rna-transcription/RNATranscriptionExample.m
trackler-2.2.1.12 tracks/objective-c/exercises/rna-transcription/RNATranscriptionExample.m
trackler-2.2.1.11 tracks/objective-c/exercises/rna-transcription/RNATranscriptionExample.m
trackler-2.2.1.10 tracks/objective-c/exercises/rna-transcription/RNATranscriptionExample.m
trackler-2.2.1.9 tracks/objective-c/exercises/rna-transcription/RNATranscriptionExample.m
trackler-2.2.1.8 tracks/objective-c/exercises/rna-transcription/RNATranscriptionExample.m
trackler-2.2.1.7 tracks/objective-c/exercises/rna-transcription/RNATranscriptionExample.m
trackler-2.2.1.6 tracks/objective-c/exercises/rna-transcription/RNATranscriptionExample.m
trackler-2.2.1.5 tracks/objective-c/exercises/rna-transcription/RNATranscriptionExample.m
trackler-2.2.1.4 tracks/objective-c/exercises/rna-transcription/RNATranscriptionExample.m
trackler-2.2.1.3 tracks/objective-c/exercises/rna-transcription/RNATranscriptionExample.m
trackler-2.2.1.2 tracks/objective-c/exercises/rna-transcription/RNATranscriptionExample.m
trackler-2.2.1.1 tracks/objective-c/exercises/rna-transcription/RNATranscriptionExample.m
trackler-2.2.1.0 tracks/objective-c/exercises/rna-transcription/RNATranscriptionExample.m