Sha256: ab6bca07e65ec72e66f7a5984dcde9fc8b525ff15a1959b638464f0e64ad002c

Contents?: true

Size: 1.28 KB

Versions: 67

Compression:

Stored size: 1.28 KB

Contents

#import "SeriesExample.h"

@interface Series()

@property (nonatomic, strong) NSString *numberString;

@end

@implementation Series

- (instancetype)initWithNumberString:(NSString *)numberString {
    if (self = [super init]) {
        self.numberString = numberString;
    }
    
    return self;
}

- (nullable NSArray<NSArray<NSNumber *> *> *)slicesWithSize:(NSInteger)size {
    NSUInteger length = [self.numberString length];
    
    if (length < size) {
        return nil;
    }
    
    NSMutableArray<NSNumber *> *numberArray = [NSMutableArray arrayWithCapacity:length];
    
    for (NSUInteger i = 0; i < length; i++) {
        unichar digit = [self.numberString characterAtIndex:i] - '0';
        if (digit < 0 || digit > 10) {
            // Invalid character
            return nil;
        }
        
        [numberArray addObject:[NSNumber numberWithUnsignedShort:digit]];
    }
    
    NSInteger numberOfSlices = length - size + 1;
    NSMutableArray<NSArray<NSNumber *> *> *result = [NSMutableArray arrayWithCapacity:numberOfSlices];
    
    for (NSUInteger start = 0; start < numberOfSlices; start++) {
        NSRange range = NSMakeRange(start, size);
        NSArray *slice = [numberArray subarrayWithRange:range];
        [result addObject:slice];
    }
    
    return result;
}

@end

Version data entries

67 entries across 67 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/objective-c/exercises/series/SeriesExample.m
trackler-2.2.1.179 tracks/objective-c/exercises/series/SeriesExample.m
trackler-2.2.1.178 tracks/objective-c/exercises/series/SeriesExample.m
trackler-2.2.1.177 tracks/objective-c/exercises/series/SeriesExample.m
trackler-2.2.1.176 tracks/objective-c/exercises/series/SeriesExample.m
trackler-2.2.1.175 tracks/objective-c/exercises/series/SeriesExample.m
trackler-2.2.1.174 tracks/objective-c/exercises/series/SeriesExample.m
trackler-2.2.1.173 tracks/objective-c/exercises/series/SeriesExample.m
trackler-2.2.1.172 tracks/objective-c/exercises/series/SeriesExample.m
trackler-2.2.1.171 tracks/objective-c/exercises/series/SeriesExample.m
trackler-2.2.1.170 tracks/objective-c/exercises/series/SeriesExample.m
trackler-2.2.1.169 tracks/objective-c/exercises/series/SeriesExample.m
trackler-2.2.1.167 tracks/objective-c/exercises/series/SeriesExample.m
trackler-2.2.1.166 tracks/objective-c/exercises/series/SeriesExample.m
trackler-2.2.1.165 tracks/objective-c/exercises/series/SeriesExample.m
trackler-2.2.1.164 tracks/objective-c/exercises/series/SeriesExample.m
trackler-2.2.1.163 tracks/objective-c/exercises/series/SeriesExample.m
trackler-2.2.1.162 tracks/objective-c/exercises/series/SeriesExample.m
trackler-2.2.1.161 tracks/objective-c/exercises/series/SeriesExample.m
trackler-2.2.1.160 tracks/objective-c/exercises/series/SeriesExample.m